Skip to content

Commit d576de1

Browse files
author
Beatriz Rizental
authored
Bug 1687644 - Use the new external API on the web ext sample (#28)
1 parent 09ae8b7 commit d576de1

File tree

12 files changed

+136
-29
lines changed

12 files changed

+136
-29
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
"use strict";
6+
7+
import Glean from "glean";
8+
9+
export const webExtStarted = new Glean._private.DatetimeMetricType({
10+
category: "sample",
11+
name: "webext_installed",
12+
sendInPings: ["sample"],
13+
lifetime: "ping",
14+
disabled: false
15+
}, "millisecond");
16+
17+
export const popupOpened = new Glean._private.CounterMetricType({
18+
category: "sample",
19+
name: "popup_opened",
20+
sendInPings: ["sample"],
21+
lifetime: "ping",
22+
disabled: false
23+
}, "millisecond");
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
"use strict";
6+
7+
import Glean from "glean";
8+
9+
export const samplePing = new Glean._private.PingType("sample", true, false);

samples/web-extension/index.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,33 @@
55
"use strict";
66

77
import Glean from "glean";
8+
import { samplePing } from "./generatedPings";
9+
import { webExtStarted, popupOpened } from "./generatedMetrics";
10+
11+
// TODO: Do not wait for Glean to be initialized before recording metrics
12+
// once Bug 1687491 is resolved.
13+
Glean.initialize("web-extension", true)
14+
.then(() => {
15+
console.log("Glean has been succesfully initialized.");
16+
webExtStarted.set()
17+
.then(() => console.log("`webext-installed` was succesfully set."));
18+
});
19+
20+
// Listen for messages from the popup.
21+
browser.runtime.onMessage.addListener(msg => {
22+
console.log(`New message received! ${msg}`);
23+
24+
if (msg === "popup-opened") {
25+
popupOpened.add()
26+
.then(() => console.log("`popup-opened` was succesfully added."));
27+
}
28+
29+
if (msg === "send-ping") {
30+
samplePing.submit()
31+
.then(wasSubmitted => {
32+
console.log(
33+
`Attempted to send ping "${samplePing.name}". Was the ping sent? ${wasSubmitted}`);
34+
});
35+
}
36+
});
837

9-
Glean.initialize("web-extension", true);

samples/web-extension/manifest.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@
66
"version": "0.0.1",
77
"background": {
88
"scripts": [
9+
"node_modules/webextension-polyfill/dist/browser-polyfill.min.js",
910
"./dist/bundle.js"
1011
]
1112
},
13+
"permissions": [
14+
"storage",
15+
"https://incoming.telemetry.mozilla.org/"
16+
],
1217
"browser_action": {
1318
"browser_style": true,
1419
"default_icon": {

samples/web-extension/package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/web-extension/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"author": "The Glean Team <glean-team@mozilla.com>",
1111
"license": "MPL-2.0",
1212
"dependencies": {
13-
"glean": "file://../../"
13+
"glean": "file://../../",
14+
"webextension-polyfill": "^0.7.0"
1415
},
1516
"devDependencies": {
1617
"webpack": "^5.4.0",

samples/web-extension/popup.html

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
<html lang="en">
33
<head>
44
<meta charset="UTF-8">
5-
<script type="text/javascript" src="popup.js"></script>
5+
<script type="text/javascript" src="node_modules/webextension-polyfill/dist/browser-polyfill.js"></script>
66
</head>
77
<body>
8-
<p style="margin: 2rem;">
9-
Now, inspect this web extension.
8+
<p style="margin: 2rem; text-align:center; min-width: 200px;">
9+
Everytime this pop-up is opened a counter metric ("popup_opened") is incremented :D
10+
<br /><br />
11+
<button id="send-ping">Send a ping!</button>
1012
</p>
13+
<script type="text/javascript" src="popup.js"></script>
1114
</body>
1215
</html>

samples/web-extension/popup.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,11 @@
44

55
"use strict";
66

7-
console.info("Nothing will happen. Glean.js is not yet implemented.");
7+
// Let the background script know the pop up has been opened.
8+
browser.runtime.sendMessage("popup-opened");
9+
10+
const sendAPingButton = document.getElementById("send-ping");
11+
sendAPingButton.addEventListener("click", async () => {
12+
// Tell the background script to upload a ping
13+
browser.runtime.sendMessage("send-ping");
14+
}, { passive: true });

samples/web-extension/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const path = require("path");
88

99
module.exports = {
1010
entry: "./index.js",
11-
devtool: "cheap-module-source-map",
11+
devtool: "inline-source-map",
1212
output: {
1313
filename: "bundle.js",
1414
path: path.resolve(__dirname, "dist"),

src/glean.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -228,18 +228,10 @@ class Glean {
228228
}
229229

230230
static get applicationId(): string | undefined {
231-
if (!Glean.instance._initialized) {
232-
console.warn("Attempted to access the Glean.applicationId before Glean was initialized.");
233-
}
234-
235231
return Glean.instance._applicationId;
236232
}
237233

238234
static get serverEndpoint(): string | undefined {
239-
if (!Glean.instance._initialized) {
240-
console.warn("Attempted to access the Glean.serverEndpoint before Glean was initialized.");
241-
}
242-
243235
return Glean.instance._serverEndpoint;
244236
}
245237

@@ -251,10 +243,6 @@ class Glean {
251243
* @returns Whether upload is enabled.
252244
*/
253245
static isUploadEnabled(): boolean {
254-
if (!Glean.instance._initialized) {
255-
console.warn("Attempted to access the Glean.uploadEnabled before Glean was initialized.");
256-
}
257-
258246
return Glean.uploadEnabled;
259247
}
260248

0 commit comments

Comments
 (0)