-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
99 lines (90 loc) · 2.33 KB
/
main.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
// Vars
let waitForLazyLoader = [
'app',
'runtime',
'windows',
'tabs'
];
let response = undefined;
let debuggerWindow = undefined;
let debuggerTab = undefined;
// short handlers
let defined = x => typeof x !== 'undefined' && x !== null;
// Functions
let loadFile = function (resolve) {
let ajax = new XMLHttpRequest();
ajax.onreadystatechange = function() {
if (ajax.readyState === 4 && ajax.responseText !== "") {
response = JSON.parse(ajax.responseText);
resolve();
}
};
ajax.withCredentials = true;
ajax.open('GET', 'file://' + JSON.parse(localStorage.getItem('prefs')).path, true);
ajax.setRequestHeader("Content-type", "application/json");
ajax.send();
}
let checkLazyLoadAvailability = function (object) {
if (waitForLazyLoader.length === 0) return true;
let obCopy = object;
waitForLazyLoader[0].split('.').forEach(v => obCopy = obCopy[v]);
if (defined(obCopy)) {
waitForLazyLoader.splice(0, 1);
return checkLazyLoadAvailability(object);
}
return false;
}
let lazyLoadQueue = function (object, resolve) {
checkLazyLoadAvailability(object);
if (waitForLazyLoader.length > 0) {
t = setTimeout(() => {
clearTimeout(t); // cleanup
lazyLoadQueue(object, resolve);
}, 1000);
} else {
resolve();
}
}
let boot = function (resolve) {
chrome.windows.create({
url: response.url
}, function () {
chrome.windows.getLastFocused({ populate:true }, (a) => {
debuggerWindow = a;
resolve();
});
});
}
let linkChaged = function () {
chrome.tabs.update(debuggerTab.id, { url: response.url});
}
let startWatchingLinkChages = function () {
setInterval(() => {
let copyResponse = response;
new Promise(loadFile)
.then(() => {
if (response.url !== copyResponse.url) {
linkChaged();
}
});
}, 1000);
}
// Start
// Chrismas tree, I know
chrome.extension.onMessage.addListener(function (request, sender, sendResponse) {
if (request.action === 'start') {
new Promise(loadFile)
.then(() => new Promise(lazyLoadQueue.bind(null, chrome))
.then(() => new Promise(boot)
.then(() => new Promise(function (resolve) {
debuggerTab = debuggerWindow.tabs[0];
resolve();
})
.then(startWatchingLinkChages))));
}
});
chrome.extension.onMessage.addListener(function (request, sender, sendResponse) {
if (request.action === 'prefs') {
sendResponse(JSON.parse(localStorage.getItem('prefs')));
}
});