forked from arnoudkooi/SN-Utils
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontent_script_all_frames.js
More file actions
54 lines (43 loc) · 1.42 KB
/
content_script_all_frames.js
File metadata and controls
54 lines (43 loc) · 1.42 KB
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
//attach event listener from popup
chrome.extension.onMessage.addListener(function (request, sender, sendResponse) {
if (request.method == "runFunction")
runFunction(request.myVars);
else if (request.snippet) {
insertTextAtCursor(request.snippet);
}
});
var s = document.createElement('script');
s.src = chrome.extension.getURL('inject.js');
s.onload = function() {
this.remove();
};
(document.head || document.documentElement).appendChild(s);
function runFunction(f) {
var doc;
if (jQuery('#gsft_main').length)
doc = jQuery('#gsft_main')[0].contentWindow.document;
else
doc = document;
var script = doc.createElement('script');
script.appendChild(doc.createTextNode(f));
doc.body.appendChild(script);
}
function insertTextAtCursor(text) {
var el = document.activeElement;
var val = el.value;
var endIndex;
var range;
var doc = el.ownerDocument;
if (typeof el.selectionStart === 'number' &&
typeof el.selectionEnd === 'number') {
endIndex = el.selectionEnd;
el.value = val.slice(0, endIndex) + text + val.slice(endIndex);
el.selectionStart = el.selectionEnd = endIndex + text.length;
} else if (doc.selection !== 'undefined' && doc.selection.createRange) {
el.focus();
range = doc.selection.createRange();
range.collapse(false);
range.text = text;
range.select();
}
}