-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathbrowser.js
249 lines (215 loc) · 9.68 KB
/
browser.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Copyright (c) 2006-2009 by Martin Stubenschrott <[email protected]>
// Copyright (c) 2007-2009 by Doug Kearns <[email protected]>
// Copyright (c) 2008-2009 by Kris Maglione <maglione.k at Gmail>
//
// This work is licensed for reuse under an MIT license. Details are
// given in the License.txt file included with this file.
/** @scope modules */
/**
* @instance browser
*/
const Browser = Module("browser", {
}, {
// TODO: support 'nrformats'? -> probably not worth it --mst
incrementURL: function (count) {
let matches = buffer.URL.match(/(.*?)(\d+)(\D*)$/);
liberator.assert(matches);
let [, pre, number, post] = matches;
let newNumber = parseInt(number, 10) + count;
let newNumberStr = String(newNumber > 0 ? newNumber : 0);
if (number.match(/^0/)) { // add 0009<C-a> should become 0010
while (newNumberStr.length < number.length)
newNumberStr = "0" + newNumberStr;
}
liberator.open(pre + newNumberStr + post);
}
}, {
options: function () {
options.add(["encoding", "enc"],
"Sets the current buffer's character encoding",
"string", "UTF-8",
{
scope: Option.SCOPE_LOCAL,
getter: function () config.browser.docShell.QueryInterface(Ci.nsIDocCharset).charset,
setter: function (val) {
if (options.encoding == val)
return val;
// Stolen from browser.jar/content/browser/browser.js, more or less.
try {
config.browser.docShell.QueryInterface(Ci.nsIDocCharset).charset = val;
if (PlacesUtils.history.setCharsetForURI)
PlacesUtils.history.setCharsetForURI(getWebNavigation().currentURI, val);
else
PlacesUtils.setCharsetForURI(getWebNavigation().currentURI, val);
getWebNavigation().reload(Ci.nsIWebNavigation.LOAD_FLAGS_CHARSET_CHANGE);
}
catch (e) { liberator.echoerr(e); }
return null;
},
completer: function (context) completion.charset(context)
});
options.add(["urlseparator"],
"Set the separator regex used to separate multiple URL args",
"string", "");
options.add(["yankencodedurl"],
"Set the yank mode copying encoded URL",
"boolean", false);
},
mappings: function () {
mappings.add([modes.NORMAL],
["y"], "Yank current location to the clipboard",
function () {
var url = buffer.URL;
if (options.get("yankencodedurl").value)
url = services.get("io").newURI(url, buffer.charser, null).asciiSpec;
else
url = services.get("textToSubURI").unEscapeURIForUI(buffer.charset, url).replace(/ /g, "%20");
util.copyToClipboard(url, true);
});
// opening websites
mappings.add([modes.NORMAL],
["o"], "Open one or more URLs",
function () { commandline.open("", "open ", modes.EX); });
mappings.add([modes.NORMAL], ["O"],
"Open one or more URLs, based on current location",
function () {
var url = services.get("textToSubURI").unEscapeURIForUI(buffer.charset, buffer.URL);
commandline.open("", "open " + url, modes.EX);
});
mappings.add([modes.NORMAL], ["t"],
"Open one or more URLs in a new tab",
function () { commandline.open("", "tabopen ", modes.EX); });
mappings.add([modes.NORMAL], ["T"],
"Open one or more URLs in a new tab, based on current location",
function () {
var url = services.get("textToSubURI").unEscapeURIForUI(buffer.charset, buffer.URL);
commandline.open("", "tabopen " + url, modes.EX);
});
mappings.add([modes.NORMAL], ["w"],
"Open one or more URLs in a new window",
function () { commandline.open("", "winopen ", modes.EX); });
mappings.add([modes.NORMAL], ["W"],
"Open one or more URLs in a new window, based on current location",
function () {
var url = services.get("textToSubURI").unEscapeURIForUI(buffer.charset, buffer.URL);
commandline.open("", "winopen " + url, modes.EX);
});
mappings.add([modes.NORMAL],
["<C-a>"], "Increment last number in URL",
function (count) { Browser.incrementURL(Math.max(count, 1)); },
{ count: true });
mappings.add([modes.NORMAL],
["<C-x>"], "Decrement last number in URL",
function (count) { Browser.incrementURL(-Math.max(count, 1)); },
{ count: true });
mappings.add([modes.NORMAL], ["~"],
"Open home directory",
function () { liberator.open("~"); });
mappings.add([modes.NORMAL], ["gh"],
"Open homepage",
function () { BrowserHome(); });
mappings.add([modes.NORMAL], ["gH"],
"Open homepage in a new tab",
function () {
let homepages = gHomeButton.getHomePage().split('|');
liberator.open(homepages, { from: "homepage", where: liberator.NEW_TAB });
});
mappings.add([modes.NORMAL], ["gu"],
"Go to parent directory",
function (count) {
function getParent(url, count) {
function getParentPath(path) {
if (!path)
return;
path = path.replace(/\/$/, "").replace(/^\/+/, "");
if (path.indexOf("#") > 0)
return path.replace(/#.*/, "");
if (path.indexOf("?") > 0)
return path.replace(/\?.*/, "");
path = path.replace(/\/+$/, "");
if (path.indexOf("/") > 0)
return path.replace(/\/[^\/]*$/,"/");
}
function getParentHost(host) {
if (!/\./.test(host) || /^[0-9+.:]+$/.test(host))
return host;
let hostSuffix = "";
let x = host.lastIndexOf(":");
if (x > 0) {
hostSuffix = host.substr(x);
host = host.substr(0, x);
}
hostSuffix = host.substr(host.length - 6) + hostSuffix;
host = host.substr(0, host.length - 6);
return host.replace(/[^.]*\./, "") + hostSuffix;
}
let parent = url;
let regexp = new RegExp("([a-z]+:///?)([^/]*)(/.*)");
let [, scheme, host, path] = regexp.exec(url);
path = path.replace(/\/$/, "").replace(/^\/+/, "");
for (let i = 0; i < count; i++) {
if (path) {
if (path = getParentPath(path))
parent = scheme + host + "/" + path;
else
parent = scheme + host + "/";
}
else {
host = getParentHost(host);
parent = scheme + host + "/";
}
}
return parent;
}
if (count < 1)
count = 1;
let url = getParent(buffer.URL, count);
if (url == buffer.URL)
liberator.beep();
else
liberator.open(url);
},
{ count: true });
mappings.add([modes.NORMAL], ["gU"],
"Go to the root of the website",
function () {
let uri = content.document.location;
liberator.assert(!/(about|mailto):/.test(uri.protocol)); // exclude these special protocols for now
liberator.open(uri.protocol + "//" + (uri.host || "") + "/");
});
mappings.add([modes.NORMAL], ["gr"],
"View current tab in Reader View",
function () {
let uri = content.document.location.href;
if (!uri.startsWith("about:reader")) {
liberator.open('about:reader?url=' + uri);
} else {
liberator.open(uri.substr("about:reader?url=".length));
}
});
},
commands: function () {
commands.add(["downl[oads]", "dl"],
"Show progress of current downloads",
function () {
liberator.open("chrome://browser/content/downloads/contentAreaDownloadsView.xul",
{ from: "downloads"});
},
{ argCount: "0" });
commands.add(["o[pen]"],
"Open one or more URLs in the current tab",
function (args) {
args = args.string;
if (args)
liberator.open(args);
else
liberator.open("");
}, {
canonicalize: function (cmd) cmd.replace(/^(op?|open?)\b/, 'open'),
completer: function (context) completion.url(context),
literal: 0,
privateData: true
});
}
});
// vim: set fdm=marker sw=4 ts=4 et: