forked from arnoudkooi/SN-Utils
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontent_script_parent.js
More file actions
128 lines (94 loc) · 3.91 KB
/
content_script_parent.js
File metadata and controls
128 lines (94 loc) · 3.91 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
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
//add typeahead for usage in Application Navigator
var t = document.createElement('script');
t.src = chrome.extension.getURL('js/typeahead.bundle.min.js');
t.onload = function() {
this.remove();
};
(document.head || document.documentElement).appendChild(t);
//attach event listener from popup
chrome.extension.onMessage.addListener(function (request, sender, sendResponse) {
if (request.method == "getSelection")
sendResponse({ selectedText: getSelection() });
else if (request.method == "getVars")
sendResponse({ myVars: getVars(request.myVars), url: location.origin, frameHref: getFrameHref() });
else if (request.method == "getLocation")
sendResponse({ url: location.origin, frameHref: getFrameHref() });
//else
//sendResponse({ url: location.origin });
});
//get the selected text, user gas selected with mouse.
function getSelection() {
if (("" + document.activeElement.name).indexOf('label') > -1 ||
("" + document.activeElement.name).indexOf('name') > -1)
return '';
var result = '' + self.document.getSelection().toString();
for (var i = 0; !result && i < self.frames.length; i++) {
try {
result = self.frames[i].document.getSelection().toString();
} catch (error) {
console.log(error);
}
}
return '' + result;
}
//get the href of the contentframe gsft_main
function getFrameHref() {
var frameHref = '';
if (jQuery('#gsft_main').length)
frameHref = document.getElementById("gsft_main").contentWindow.location.href ;
else if (jQuery('div.tab-pane.active').length == 1){
frameHref = jQuery('iframe.activetab')[0].contentWindow.location.href;
}
else
frameHref = document.location.href;
return frameHref;
}
//initialize g_list variable
function setGList() {
var doc;
if ($('#gsft_main').length)
doc = $('#gsft_main')[0].contentWindow.document;
else
doc = document;
var scriptContent = "try{ var g_list = GlideList2.get(jQuery('#sys_target').val()); } catch(err){console.log(err);}";
var script = doc.createElement('script');
script.appendChild(doc.createTextNode(scriptContent));
doc.body.appendChild(script);
}
//try to return the window variables, defined in the comma separated varstring string
function getVars(varstring) {
// if(window.frameElement && window.frameElement.nodeName == "IFRAME")
// return; //dont run in iframes
if (varstring.indexOf('g_list') > -1)
setGList();
var doc;
var ret = {};
if (jQuery('#gsft_main').length)
doc = jQuery('#gsft_main')[0].contentWindow.document;
else if (jQuery('div.tab-pane.active').length == 1){
ret.g_ck = jQuery('input#sysparm_ck').val();
ret.arnoud = 'kooi';
jQuery('iframe').removeClass('activetab');
jQuery('div.tab-pane.active iframe').addClass('activetab');
doc = jQuery('iframe.activetab')[0].contentWindow.document;
}
else
doc = document;
var variables = varstring.replace(/ /g, "").split(",");
var scriptContent = "";
for (var i = 0; i < variables.length; i++) {
var currVariable = variables[i];
scriptContent += "try{ if (typeof window." + currVariable + " !== 'undefined') jQuery('body').attr('tmp_" + currVariable.replace(/\./g, "") + "', window." + currVariable + "); } catch(err){console.log(err);}\n"
}
var script = doc.createElement('script');
script.id = 'tmpScript';
script.appendChild(doc.createTextNode(scriptContent));
doc.body.appendChild(script);
for (var i = 0; i < variables.length; i++) {
var currVariable = variables[i];
ret[currVariable.replace(/\./g, "")] = $(doc.body).attr("tmp_" + currVariable.replace(/\./g, ""));
$(doc.body).removeAttr("tmp_" + currVariable.replace(/\./g, ""));
}
$(doc.body).find("#tmpScript").remove();
return ret;
}