forked from gebrkn/copytables
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reload-chrome.js
executable file
·72 lines (57 loc) · 1.72 KB
/
reload-chrome.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
/// Reload the extensions page and the the given url.
/// run me as `osascript -l JavaScript reload-chrome.js 'http://whatever`
function run(argv) {
var app = Application('Google Chrome'),
extUrl = 'chrome://extensions/',
ctxUrl = argv;
function each(a, fn) {
for (var i = 0; i < a.length; i++)
fn(a[i], i);
}
function find(a, fn) {
for (var i = 0; i < a.length; i++)
if (fn(a[i], i))
return a[i];
return null;
}
function enumTabs() {
var wts = [];
each(app.windows, function (win) {
each(win.tabs, function (tab, i) {
wts.push([win, tab, i])
});
});
return wts.filter(function (wt) {
return !wt[0].title().match(/^Developer Tools/);
});
}
function reload() {
var wts = enumTabs();
var ctxWt = find(wts, function (wt) {
return wt[1].url() == ctxUrl;
});
var extWt = find(wts, function (wt) {
return wt[1].url() == extUrl;
});
if (!ctxWt) {
console.log('no tab found for ' + ctxUrl);
var w = app.Window().make();
ctxWt = [w, w.tabs[0], 0];
w.tabs[0].url = ctxUrl;
}
if (!extWt) {
console.log('no tab found for ' + extUrl);
var w = app.Window().make();
extWt = [w, w.tabs[0], 0];
w.tabs[0].url = extUrl;
}
while(extWt[1].loading());
while(ctxWt[1].loading());
extWt[1].reload();
while(extWt[1].loading());
ctxWt[1].reload();
ctxWt[0].activeTabIndex = ctxWt[2] + 1;
}
app.activate();
reload();
}