This repository has been archived by the owner on Jul 16, 2019. It is now read-only.
/
background.js
130 lines (115 loc) · 3.78 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
/* global getOpenwpmSetup */
class OpenwpmLifeCycleHandler {
/**
* Listen to onStopped, onStarted
* `browser.openwpm.start` fires onStarted if successful
* onStopped is fired in the event of some errors that forces OpenWPM to stop, or upon `browser.openwpm.stop`
*/
constructor() {
browser.openwpm.onStopped.addListener(this.onStopped.bind(this));
browser.openwpm.onStarted.addListener(this.onStarted.bind(this));
}
/**
* @param {object} openwpmInfo browser.openwpm.openwpmInfo object
*
* @returns {undefined}
*/
async onStarted(openwpmInfo) {
console.log("OpenWPM has started", openwpmInfo);
feature.configure(openwpmInfo);
}
/**
* @param {object} ending An ending result
*
* @returns {undefined}
*/
async onStopped(ending) {
console.log(`OpenWPM has stopped`, ending);
await this.cleanup();
}
/**
* @returns {undefined}
*/
async cleanup() {
// do whatever work your add-on needs to clean up
}
}
class FooFeature {
constructor() {}
async configure(/* openwpmInfo*/) {
console.log(
"Feature is now enabled, sending 'test:onFeatureEnabled' event (for the tests)",
);
browser.runtime.sendMessage("test:onFeatureEnabled").catch(console.error);
}
}
// construct. will be configured after setup.
const feature = new FooFeature();
/**
* Run every startup to get config and instantiate the feature
*
* @returns {undefined}
*/
async function onEveryExtensionLoad() {
new OpenwpmLifeCycleHandler();
const openwpmSetup = await getOpenwpmSetup();
console.log("OpenWPM setup: ", openwpmSetup);
try {
await browser.openwpm.start(openwpmSetup);
} catch (e) {
console.error("OpenWPM error: ", e);
}
}
// Since this is a test-addon, we don't initiate any code directly, but wait
// for events sent by tests. This allows us to control and test the execution
// properly.
// Note: Since this is the first onMessage listener, it will be able to send
// a response to the sending party
// onEveryExtensionLoad();
const onEveryExtensionLoadTestListener = request => {
console.log("onEveryExtensionLoad listener - request", request);
if (request === "test:onEveryExtensionLoad") {
console.log("Removing onEveryExtensionLoadTestListener");
browser.runtime.onMessage.removeListener(onEveryExtensionLoadTestListener);
console.log("Running onEveryExtensionLoad()");
onEveryExtensionLoad();
}
};
browser.runtime.onMessage.addListener(onEveryExtensionLoadTestListener);
// The tests that probe the web extensions APIs directly rely on an extension
// page opening up in a new window/tab.
// For more information, see shield-studies-addon-utils/testUtils/executeJs.js
const createData = {
type: "detached_panel",
url: "extension-page-for-tests/index.html",
width: 500,
height: 500,
};
browser.windows.create(createData);
// Testing that the web extension events works properly with our bundled APIs requires the below code
/**
* Fired when the extension is first installed, when the extension is updated
* to a new version, and when the browser is updated to a new version.
*
* See: https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/runtime/onInstalled
*
* @param {object} details webExtension details object
* @returns {undefined} Nothing
*/
function handleInstalled(details) {
console.log(
"The 'handleInstalled' event was fired.",
details.reason,
details,
);
}
/**
* Fired when a profile that has this extension installed first starts up.
* This event is not fired when a private browsing/incognito profile is started.
* @returns {undefined} Nothing
*/
async function handleStartup() {
console.log("The 'handleStartup' event was fired.", arguments);
}
browser.runtime.onStartup.addListener(handleStartup);
browser.runtime.onInstalled.addListener(handleInstalled);