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
|
/**
* == VimperatorPlugin==
* @name appendAnchor
* @description append anchors to texts look like url.
* @author SAKAI, Kazuaki
* @version 0.02
* @minVersion 2.0pre
* @maxVersion 2.0pre
* == /VimperatorPlugin==
*/
(function(){
if (window.eval(liberator.globalVariables.auto_append_anchor || 'false')) {
let originalHintsShow = liberator.modules.hints.show;
hints.show = function () {
liberator.execute('anc');
originalHintsShow.apply(this, arguments);
};
}
liberator.modules.commands.addUserCommand(['anc'], 'append anchors to texts look like url',
function(arg, special) {
var doc = window.content.document;
var nodes = liberator.modules.buffer.evaluateXPath(
'/descendant::*[not(contains(" TITLE STYLE SCRIPT TEXTAREA XMP A ", concat(" ", translate(local-name(), "aceilmprstxy", "ACEILMPRSTXY"), " ")))]/child::text()'
);
var regex = new RegExp("h?(ttps?):/+([a-zA-Z0-9][-_.!~*'()a-zA-Z0-9;/?:@&=+$,%#]+[-_~*(a-zA-Z0-9;/?@&=+$%#])");
var range = doc.createRange();
var last;
var href;
for (let i = 0, l = nodes.snapshotLength; i < l; i++) {
let node = nodes.snapshotItem(i);
range.selectNode(node);
while (node && (last = range.toString().search(regex)) > -1) {
range.setStart(node, last);
range.setEnd(node, last + RegExp.lastMatch.length);
href = 'h' + RegExp.$1 + '://' + RegExp.$2;
let anchor = doc.createElement('a');
range.insertNode(anchor);
anchor.setAttribute('href', href);
range.surroundContents(anchor);
node = node.nextSibling.nextSibling;
range.selectNode(node);
}
}
range.detach();
},{}
);
})();
|