Skip to content

Instantly share code, notes, and snippets.

@rhelmer
Created September 2, 2020 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rhelmer/90a4ac6cf8b507c8b489258f64e23e03 to your computer and use it in GitHub Desktop.
Save rhelmer/90a4ac6cf8b507c8b489258f64e23e03 to your computer and use it in GitHub Desktop.
pioneer_injection.diff
diff --git a/experiment-apis/pioneer.js b/experiment-apis/pioneer.js
new file mode 100644
index 0000000..fe7cb0e
--- /dev/null
+++ b/experiment-apis/pioneer.js
@@ -0,0 +1,96 @@
+/* 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/. */
+
+"use strict";
+
+/* global ExtensionAPI, ExtensionCommon, Services, XPCOMUtils */
+
+XPCOMUtils.defineLazyModuleGetters(this, {
+ Services: "resource://gre/modules/Services.jsm",
+});
+
+var { ExtensionCommon } = ChromeUtils.import(
+ "resource://gre/modules/ExtensionCommon.jsm"
+);
+
+const EventManager = ExtensionCommon.EventManager;
+
+const PREF_SURVEY_COMPLETE = "extensions.pioneer.surveyComplete";
+
+this.pioneer = class extends ExtensionAPI {
+ onStartup() {
+ // FIXME loop over all windows, and add window open listener.
+ const win = Services.wm.getMostRecentWindow("navigator:browser");
+
+ this.listener = () => {
+ if (win.gBrowser.contentWindow.location.pathname == "pioneer") {
+ const doc = win.gBrowser.contentDocument;
+ const dialog = doc.createElement("dialog");
+ const form = doc.createElement("form");
+ const survey = { lines: ["1", "2", "3"] };
+ for (const line of survey.lines) {
+ const p = doc.createElement("p");
+ const span = doc.createElement("span");
+ span.textContent = line;
+ p.appendChild(span);
+ const input = doc.createElement("input");
+ p.appendChild(input);
+ form.append(p);
+ }
+ const button = doc.createElement("button");
+ button.type = "submit";
+
+ form.onsubmit = (event) => {
+ this.filledForm = event.target;
+ dialog.close();
+ };
+ form.appendChild(button);
+ dialog.appendChild(form);
+ doc.body.prepend(dialog);
+
+ // This modal will not be dismissed until the user completes the survey.
+ dialog.showModal();
+ }
+
+ const surveyComplete = Services.prefs.getBoolPref(
+ PREF_SURVEY_COMPLETE,
+ false
+ );
+ if (!surveyComplete) {
+ win.addEventListener("DOMContentLoaded", this.listener);
+ }
+ console.debug("startup completed");
+ };
+ }
+ onShutdown() {
+ // FIXME loop over all windows, and add window open listener.
+ const win = Services.wm.getMostRecentWindow("navigator:browser");
+ win.removeEventListener("DOMContentLoaded", this.listener);
+ }
+ getAPI(context) {
+ return {
+ pioneer: {
+ survey: new EventManager({
+ context,
+ name: "pioneer.survey",
+ register: (fire) => {
+ const callback = () => {
+ fire
+ .async(this.filledForm)
+ .then(() =>
+ // Only set the survey as seen when the core add-on has received the message.
+ Services.prefs.setBoolPref(PREF_SURVEY_COMPLETE, true)
+ )
+ .catch(() => {}); // ignore Message Manager disconnects
+ };
+ Services.prefs.addObserver(PREF_SURVEY_COMPLETE, callback);
+ return () => {
+ Services.prefs.removeObserver(PREF_SURVEY_COMPLETE, callback);
+ };
+ },
+ }).api(),
+ },
+ };
+ }
+};
diff --git a/experiment-apis/pioneer.json b/experiment-apis/pioneer.json
new file mode 100644
index 0000000..e41456a
--- /dev/null
+++ b/experiment-apis/pioneer.json
@@ -0,0 +1,12 @@
+[
+ {
+ "namespace": "pioneer",
+ "description": "experimental API extension to allow access to about:config preferences",
+ "events": [
+ {
+ "name": "survey",
+ "type": "function"
+ }
+ ]
+ }
+]
diff --git a/manifest.json b/manifest.json
index 009119e..f9595f6 100644
--- a/manifest.json
+++ b/manifest.json
@@ -33,5 +33,16 @@
"background": {
"scripts": ["background.js"]
+ },
+
+ "experiment_apis": {
+ "pioneer": {
+ "schema": "experiment-apis/pioneer.json",
+ "parent": {
+ "scopes": ["addon_parent"],
+ "script": "experiment-apis/pioneer.js",
+ "events": ["startup"]
+ }
+ }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment