-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_keys.js
executable file
·141 lines (118 loc) · 3.48 KB
/
convert_keys.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
var tfs_path, regex;
function searchForKeyNames()
{
function createLinkFromNode(node) {
var l, m;
var txt = node.textContent;
var span = null;
var p = 0;
if (txt.trim().length == 0)
return;
tfsTagExpression = new RegExp("(" + regex + ")", "g");
while ( (m=tfsTagExpression.exec(txt)) !== null)
{
if (null===span) {
// Create a new span for the replaced text and newly created href
span=document.createElement('span');
}
// Get the link without trailing dots
link = m[0].replace(/\.*$/, '');
// Put in text up to the link
span.appendChild(document.createTextNode(txt.substring(p, m.index)));
// Create a link and put it in the span
a = document.createElement('a');
a.className='linkclass';
a.appendChild(document.createTextNode(link));
a.setAttribute('href', tfs_path + link);
a.setAttribute('target', '_blank');
a.style.textDecoration = "underline";
span.appendChild(a);
//track insertion point
p = m.index+m[0].length;
}
if (span) {
// Take the text after the last link
span.appendChild(document.createTextNode(txt.substring(p, txt.length)));
// Replace the original text with the new span
try {
node.parentNode.replaceChild(span, node);
} catch (e) {
console.error(e);
console.log(node);
}
}
}
if ('text/xml'!=document.contentType && 'application/xml'!=document.contentType) {
var node, allLinks=findTextNodes();
for(var i=0; i<allLinks.length; i++) {
node=allLinks[i];
createLinkFromNode(node);
}
}
}
var observer = new MutationObserver(onMutation);
var observerConfig = {
attributes: false,
characterData: false,
childList: true,
subtree: true
};
observer.start = function() {
observer.observe(document.body, observerConfig);
};
observer.stop = function() {
observer.disconnect();
};
function onMutation() {
observer.stop();
searchForKeyNames();
observer.start();
}
function findTextNodes(root) {
root = root || document.body;
var textNodes = [];
var ignoreTags = /^(?:a|noscript|option|script|style|textarea)$/i;
(function findTextNodes(node) {
node = node.firstChild;
while (node) {
if (node.nodeType == 3)
textNodes.push(node)
else if (!ignoreTags.test(node.nodeName))
findTextNodes(node)
node = node.nextSibling;
}
})(root);
return textNodes;
}
var options = {};
chrome.runtime.sendMessage('get_options', function (options_) {
options = options_;
// If whitelist is configured, check if address matches it
if (options.whitelist.length > 0) {
for (var i = 0; i < options.whitelist.length; i++) {
var curResult = RegExp(options.whitelist[i]).test(document.domain);
if (curResult == true) {
// Set global regex value
regex = options.regex;
// Format tfs path with a trailing slash if not present
tfs_path = options.tfs_path;
// if (tfs_path.substr(-1) != '/') tfs_path += '/';
// Ready to begin search for key names
searchForKeyNames();
observer.start();
return;
}
}
}
// If no whitelist is configured, simply run
else {
// Set global regex value
regex = options.regex;
// Format tfs path with a trailing slash if not present
tfs_path = options.tfs_path;
// if (tfs_path.substr(-1) != '/') tfs_path += '/';
// Ready to begin search for key names
searchForKeyNames();
observer.start();
}
})