-
Notifications
You must be signed in to change notification settings - Fork 848
/
main.js
106 lines (96 loc) · 2.78 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
100
101
102
103
104
105
106
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
const widgets = require("widget");
const data = require("self").data;
const tabs = require("tabs");
const { StartupPanel } = require("./startup-panel");
const { Page } = require("page-worker");
const simplePrefs = require('simple-prefs');
var button = widgets.Widget({
id: "towtruck-starter",
label: "Start TowTruck",
contentURL: data.url("button.html"),
contentScriptFile: data.url("button.js"),
onClick: startTowTruck,
width: 48
});
StartupPanel({
name: "TowTruck",
contentURL: data.url("startup-help.html")
});
var autoDomains = [];
function updateAutoDomains() {
var domains = simplePrefs.prefs.autoDomains;
domains = domains.split(/,/g);
autoDomains = [];
domains.forEach(function (item) {
item = item.replace(/^\s+/, "").replace(/\s+$/, "");
item = item.split(/;/);
var domain = item[0];
var shareId = item[1] || null;
if (domain.indexOf("//") == -1) {
// Just a plain domain
domain = "^https?:\\/\\/" + domain;
}
domain = new RegExp(domain, "i");
autoDomains.push({domain: domain, shareId: shareId});
});
}
simplePrefs.on("autoDomains", updateAutoDomains);
updateAutoDomains();
function startTowTruck(shareId) {
var tab = tabs.activeTab;
if (tab.towtruckCloser) {
tab.towtruckCloser();
return;
}
tab.towtruckCloser = function () {
tab.towtruckCloser = null;
button.port.emit("TowTruckOff");
tab.removeListener("ready", attachWorker);
};
var worker;
function attachWorker() {
worker = tab.attach({
contentScriptFile: [
data.url("attachment.js")
]
});
worker.port.on("Close", function () {
tab.towtruckCloser();
});
worker.port.emit("Config", {url: simplePrefs.prefs.towtruckJs, hubBase: simplePrefs.prefs.hubBase, shareId: shareId || null});
}
button.port.emit("TowTruckOn");
tab.on("ready", attachWorker);
attachWorker();
}
// Need poll for back button code
tabs.on("open", watchTab);
function watchTab(tab) {
tab.on("ready", function () {
if (tabs.activeTab == tab && tab.url.indexOf("#&towtruck") != -1) {
startTowTruck();
}
if (tabs.activeTab == tab && ! tab.towtruckCloser) {
var started = false;
autoDomains.forEach(function (matcher) {
if ((! started) && tab.url.search(matcher.domain) != -1) {
startTowTruck(matcher.shareId);
started = true;
}
});
}
});
}
for (var i=0; i<tabs.length; i++) {
watchTab(tabs[i]);
}
tabs.on("activate", function () {
if (tabs.activeTab.towtruckCloser) {
button.port.emit("TowTruckOn");
} else {
button.port.emit("TowTruckOff");
}
});