-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
182 lines (161 loc) · 6.31 KB
/
background.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
var userMode = 'Contributor'; // default
var clickSequence = [];
var urls = [];
var navType = 'guided';
var elementsToClick = [];
// Stuff to do when the extension is installed/updated.
chrome.runtime.onInstalled.addListener((info) => {
// open tab to choose user mode when chrome extension is installed
if (info.reason === chrome.runtime.OnInstalledReason.INSTALL) {
chrome.storage.sync.set({ userMode });
chrome.storage.sync.set({ navType });
chrome.tabs.create({
url: 'extension-pages/options.html'
});
}
chrome.storage.local.set({ clickSequence });
// keep track of currently open tab urls
chrome.tabs.query({
currentWindow: true
}, function(tabs) {
for (var i = 0; i < tabs.length; i++) {
urls[tabs[i].id] = tabs[i].url;
}
});
});
chrome.tabs.onRemoved.addListener(function(tabid, removed) {
chrome.storage.sync.get("userMode", ({ userMode }) => {
if(userMode == "Contributor"){
console.log(tabid);
console.log(urls);
if (!urls[tabid].includes("chrome-extension://") && !urls[tabid].includes("chrome://")) {
console.log("action tab closed");
console.log(urls[tabid]);
// ask contributor for previous action
chrome.tabs.create({url:"extension-pages/action_collector.html"});
} else {
chrome.storage.local.set({ 'clickSequence' : [] }); // clear local clicksequence
}
}
});
});
// keep track of urls of all tabs.
chrome.tabs.onUpdated.addListener(function(tabid, changeInfo, tab) {
if (changeInfo.url) {
console.log(changeInfo.url);
urls[tabid] = changeInfo.url;
}
});
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.action == "background script get clicks") {
elementsToClick = request.elements;
chrome.windows.getCurrent(w => {
chrome.tabs.query({active: true, windowId: w.id}, tabs => {
const tabId = tabs[0].id;
// for (let i in request.elements) {
// chrome.storage.local.get('contentReady', function(contentReady) {
// console.log(contentReady);
// if (contentReady) {
if(elementsToClick.length > 0) {
console.log(elementsToClick);
chrome.tabs.sendMessage(tabId, {action: "content script click", element: elementsToClick[0]});
elementsToClick.splice(0,1);
}
// }
// });
// chrome.tabs.update(null, {
// url: request.elements[i].baseURI
// }, function (tab) {
// chrome.tabs.onUpdated.addListener(function listener (id, info) {
// if (info.status === 'complete' && id === tabId) {
// console.log(request.elements[i]);
// chrome.tabs.onUpdated.removeListener(listener);
// chrome.tabs.sendMessage(tabId, {action: "content script click", element: request.elements[i]});
// }
// });
// });
////////////
// console.log("in loop")
// chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
// console.log("updated")
// if (changeInfo.status == 'complete') {
// chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
// console.log(request.elements[i]);
// chrome.tabs.sendMessage(tabs[0].id, {action: "content script click", element: request.elements[i]});
// });
// }
// });
////////////
// }
});
});
}
});
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.contentScriptQuery == "getdata") {
//var url = request.url;
var url = "http://localhost:3000/todos";
fetch(url)
.then(response => response.text())
.then(response => sendResponse(response))
.catch()
return true;
}
if (request.contentScriptQuery == "postData") {
fetch(request.url, {
method: 'POST',
headers: {
'Accept': 'application/json, application/xml, text/plain, text/html, *.*',
'Content-Type': 'application/json; charset=utf-8'
},
body: 'result=' + request.data
})
.then(response => response.json())
.then(response => sendResponse(response))
.then(console.log('sent post'))
.catch(error => console.log('Error:', error));
return true;
}
});
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.action == "send content script more") {
if(elementsToClick.length > 0){
console.log("got message and sending");
chrome.tabs.onUpdated.addListener(function listener (id, info) {
chrome.windows.getCurrent(w => {
chrome.tabs.query({active: true, windowId: w.id}, tabs => {
const tabId = tabs[0].id;
if (info.status === 'complete' && tabId === id && tabs[0].url == elementsToClick[0].baseURI) {
console.log(elementsToClick);
console.log(elementsToClick[0]);
chrome.tabs.sendMessage(tabId, {action: "content script click", element: elementsToClick[0]});
elementsToClick.splice(0,1);
}
});
});
});
}else{
chrome.windows.getCurrent(w => {
chrome.tabs.query({active: true, windowId: w.id}, tabs => {
const tabId = tabs[0].id;
chrome.tabs.sendMessage(tabId, {action: "end clicks"});
});
});
}
}
});
function checkAndClick(element) {
chrome.tabs.query({currentWindow: true, active: true},
function (tabs) {
if (tabs[0].url == element.baseURI) {
console.log(tabs[0].url);
chrome.tabs.sendMessage(
tabs[0].id,
{action: "content script click", element : element});
} else {
setTimeout(checkAndClick(element), 1000);
}
});
}