-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpentaread.js
131 lines (112 loc) · 3.24 KB
/
pentaread.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* pentaread -- Pentadactyl plugin interfacing Firefox's reader view. See
* embedded documentation.
*
* Installation:
* Copy the .js and .penta (optional) files to ~/.pentadactyl/plugins/
* The JavaScript file implements the :reader and :unreader commands,
* and the command :togglereader to swith between the modes.
* The .penta file creates key mappings in normal mode.
*
* Copyright:
* Copyright (c) 2017, Cong Ma <cma@pmo.ac.cn>
* All rights reserved.
*
* License:
* BSD license <http://opensource.org/licenses/BSD-2-Clause>
*/
"use strict";
/* Plugin manifest */
var INFO =
["plugin", {
"name": "pentaread",
"version": "0.1",
"href": "https://github.com/congma/pentaread/",
"summary": "pentaread - access Firefox reader mode from Pentadactyl",
"xmlns": "dactyl"},
["author", {
"email": "cma@pmo.ac.cn",
"href": "https://cma.lamost.org/"}, "Cong Ma"],
["license", {"href": "http://opensource.org/licenses/BSD-2-Clause"},
"BSD License"],
["project", {"name": "Pentadactyl", "min-version": "1.0" }],
["p", {},
"The plugin ", ["str", {}, "pentaread"], " interfaes Firefox's ",
" Reader View feature."],
["item", {},
["tags", {}, ":reader"],
["strut", {}],
["spec", {}, ":reader"],
["description", {},
["p", {}, "Go to reader view of current web page."]]],
["item", {},
["tags", {}, ":unreader"],
["strut", {}],
["spec", {}, ":unreader"],
["description", {},
["p", {}, "Go from reader view to web page."]]],
["item", {},
["tags", {}, ":tr :togglereader"],
["strut", {}],
["spec", {}, ":togglereader"],
["description", {},
["p", {}, "Toggle reader view."],
["p", {},
"By default, this command is mapped to",
" the key combo ", ["str", {}, ["k", {"name": "Leader"}], "r"],
":"],
["code", {},
["ex", {}, ":nmap"], " ", ["k", {"name": "Leader"}],
"r ", ["ex", {}, ":togglereader"], ["k", {"name": "CR"}]]]]];
let readerhead = "about:reader?url=";
/* Return boolean: are we in reader mode? */
function reader_mode_p()
{
return buffer.documentURI.spec.startsWith(readerhead);
}
/* Go to reader view of current buffer (web page) */
function to_reader()
{
if ( buffer.documentURI.schemeIs("http") ||
buffer.documentURI.schemeIs("https") )
{
return dactyl.open(readerhead +
encodeURIComponent(buffer.documentURI.spec));
} else {
return dactyl.echoerr("pentaread: error: URI scheme not applicable.");
}
}
/* Go from reader view to normal web page view */
function from_reader()
{
if ( reader_mode_p() )
{
return dactyl.open(decodeURIComponent(
buffer.documentURI.spec.replace(readerhead, "")));
} else {
return dactyl.echoerr("pentaread: error: buffer not in reader mode.");
}
}
/* Toggle reader mode */
function toggle_reader()
{
if ( reader_mode_p() )
return from_reader();
else
return to_reader();
}
/* Pentadactyl commands */
group.commands.add(["reader"],
"Go to reader view of current page.",
to_reader,
{"argcount": 0},
true);
group.commands.add(["unreader"],
"Go from reader view to web page.",
from_reader,
{"argcount": 0},
true);
group.commands.add(["togglereader", "tr"],
"Toggle reader view.",
toggle_reader,
{"argcount": 0},
true);