-
Notifications
You must be signed in to change notification settings - Fork 118
/
content_script_parent.js
218 lines (179 loc) · 7.14 KB
/
content_script_parent.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
setTimeout(() => { //be sure content_script_all_frames.js is loaded first
getFromSyncStorageGlobal("snusettings", function (snusettings) {
if (snusettings && snusettings.hasOwnProperty('iconallowbadge') && !snusettings.iconallowbadge) return;
getFromSyncStorage("snuinstancesettings", function (settings) {
setFavIconBadge(settings);
});
});
},200);
//attach event listener from popup or background script
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.method == "getSelection"){
sendResponse({ selectedText: getSelection() });
return true;
}
else if (request.method == "snuFetch"){
snuFetch(request.options, (resp)=>{ sendResponse(resp) });
return true;
}
else if (request.method == "setFavIconBadge"){
setFavIconBadge(request.options);
return true;
}
else if (request.method == "getVars"){
sendResponse({ myVars: getVars(request.myVars), url: location.origin, frameHref: getFrameHref() });
return true;
}
else if (request.method == "getLocation"){
sendResponse({ url: location.origin, frameHref: getFrameHref() });
return true;
}
else if (request.method == "toggleSearch") {
toggleSearch();
return true;
}
else if (request.method == "snuProcessEvent") { //pass event payload to page
if (typeof cloneInto != 'undefined') request = cloneInto(request, document.defaultView); //required for ff
var event = new CustomEvent(
request.method, request
);
document.dispatchEvent(event);
return true;
}
return true;
//else
//sendResponse({ url: location.origin });
});
function toggleSearch() {
let nextFocus = (document.activeElement.id == "filter")
? "sysparm_search" : "filter";
document.getElementById(nextFocus).focus();
document.getElementById(nextFocus).select();
}
function setFavIconBadge(settings) {
Tinycon.reset();
if (!(settings && settings.hasOwnProperty("icontext") && settings.icontext)) return;
Tinycon.setOptions({
width: settings.iconwidth,
height: settings.iconheight,
font: settings.iconfontsize + 'pt arial',
color: settings.iconcolortext,
background: settings.iconcolorbg,
fallback: true
});
Tinycon.setBubble(settings.icontext);
}
//get the selected text, user has 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();
var iframes = document.querySelectorAll("iframe");
if (!iframes.length && document.querySelector("[global-navigation-config]")) //try to find iframe in case of polaris
iframes = document.querySelector("[global-navigation-config]").shadowRoot.querySelectorAll("iframe");
iframes.forEach(frame => {
try {
result += frame.contentWindow.getSelection().toString();
} catch (error) {
console.log(error);
}
});
return '' + result;
}
//get the href of the contentframe gsft_main
function getFrameHref() {
var frameHref = document.location.href;
if (document.querySelectorAll('#gsft_main').length) //ui16
frameHref = document.getElementById("gsft_main").contentWindow.location.href;
else if (document.querySelectorAll("[component-id]").length && //polaris ui
document.querySelector("[component-id]").shadowRoot.querySelectorAll("#gsft_main").length) {
frameHref = document.querySelector("[component-id]").shadowRoot.querySelector("#gsft_main").contentWindow.location.href;
}
else if (document.querySelectorAll('div.tab-pane.active').length == 1) { //studio
try{
frameHref = document.querySelector('div.tab-pane.active iframe').contentWindow.location.href;
}
catch(ex){
}
}
return frameHref;
}
//initialize g_list variable
function setGList(doc) {
var detail = {
"detail" : {
"type" : "code",
"content" : "try{ var g_list = GlideList2.get(document.querySelector('#sys_target').value); } catch(err){console.log(err);}"
}
};
if (typeof cloneInto != 'undefined') detail = cloneInto(detail, document.defaultView); //required for ff
var event = new CustomEvent('snuEvent', detail);
doc.dispatchEvent(event);
}
//Function to query Servicenow API
function snuFetch(options, callback) {
var hdrs = {
'Cache-Control': 'no-cache',
'Accept': 'application/json',
'Content-Type': 'application/json'
};
if (options.token) //only for instances with high security plugin enabled
hdrs['X-UserToken'] = options.token;
var requestInfo = {
method : 'get',
headers : hdrs
}
if (options.post){
requestInfo.method = 'PUT';
requestInfo.body = options.post;
}
fetch(options.url, requestInfo)
.then(response => response.json())
.then(data => {
callback(data);
});
}
//try to return the window variables, defined in the comma separated varstring string
function getVars(varstring) {
var doc = document;
var ret = {};
if (document.querySelectorAll('#gsft_main').length) //ui16
doc = document.querySelector('#gsft_main').contentWindow.document;
else if (document.querySelectorAll("[component-id]").length && //polaris ui
document.querySelector("[component-id]").shadowRoot.querySelectorAll("#gsft_main").length) {
doc = document.querySelector("[component-id]").shadowRoot.querySelector("#gsft_main").contentWindow.document;
}
else if (document.querySelectorAll('div.tab-pane.active').length == 1) { //studio
try{
doc = document.querySelector('div.tab-pane.active iframe').contentWindow.document;
ret.g_ck = doc.querySelector('input#sysparm_ck').value;
}
catch(ex){
doc = document;
}
}
if (varstring.indexOf('g_list') > -1)
setGList(doc);
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') document.body.setAttribute('tmp_" + currVariable.replace(/\./g, "") + "', window." + currVariable + "); } catch(err){console.log(err);}\n"
}
var detail = {
"detail" : {
"type" : "code",
"content" : scriptContent
}
};
if (typeof cloneInto != 'undefined') detail = cloneInto(detail, document.defaultView); //required for ff
var event = new CustomEvent('snuEvent', detail);
doc.dispatchEvent(event);
for (var i = 0; i < variables.length; i++) {
var currVariable = variables[i];
ret[currVariable.replace(/\./g, "")] = doc.body.getAttribute("tmp_" + currVariable.replace(/\./g, ""));
doc.body.removeAttribute("tmp_" + currVariable.replace(/\./g, ""));
}
return ret;
}