Skip to content
This repository has been archived by the owner on Oct 14, 2020. It is now read-only.

Commit

Permalink
Re: #71, webExtension version.
Browse files Browse the repository at this point in the history
Pattern
- run as background_script
- expect to be webpacked or browsierfied in
- feature telemetry relies on seeing the shield study in the scope.

to be done:
- storage of things
- api to override?
- prefs (as a permission for some addons, specific to each study)
- know that uninstall happened to have finalize.
- actually send telemetry
- actually tag enviroment
  • Loading branch information
gregglind committed Apr 19, 2017
1 parent 0ea943a commit 466113c
Show file tree
Hide file tree
Showing 9 changed files with 220 additions and 0 deletions.
1 change: 1 addition & 0 deletions webExtension/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addon/background_scripts/index.js
Binary file added webExtension/addon/icons/kittens.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added webExtension/addon/icons/puppies.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions webExtension/addon/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"description": "Adds a browser action icon to the toolbar.",
"manifest_version": 2,
"name": "Example Study",
"version": "1.0",
"browser_action": {
"default_title": "Our Feature",
"browser_style": true
},
"background": {
"scripts": [
"background_scripts/index.js"
]
},
"web_accessible_resources": [
"icons/kittens.jpg",
"icons/puppies.jpg"
]
}
25 changes: 25 additions & 0 deletions webExtension/feature/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* global study */



function telemetry (data) {
console.log("module telemetry:", data);
study.telemetry(data); // we did window.study in the other file
}

let times = 0;
function handleClick () {
times += 1;
console.log('got a click');
telemetry({"evt": "click", times: times});
}

function init(branch) {
browser.browserAction.setIcon({path: `icons/${branch}.png`});
browser.browserAction.setTitle({title: branch});
browser.browserAction.onClicked.addListener(handleClick);
}

exports.init = init;


44 changes: 44 additions & 0 deletions webExtension/ourStudy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

const shield = require("./shield-v4.js");
const feature = require("./feature/")
const studyConfig = {
name: self.id,
days: 14,
surveyUrls: {
'end-of-study': 'http://example.com/some/url',
'user-ended-study': 'http://example.com/some/url',
'ineligible': null
}
}

class ExampleStudy extends shield.ShieldStudy {
chooseVariation () {
return "kittens";
}
}

// put it up in global, because we are going to webpack
let study = window.study = new ExampleStudy(studyConfig);

function listenForTelemetryFromContent (request, sender, sendResponse) {
console.log(request, sender, sendResponse);
if (request.telemetry) {
study.telemetry(request.data);
}
}

browser.runtime.onMessage.addListener(listenForTelemetryFromContent);


// this is the first time onlye
if (study.isEligible()) {
let variation = study.chooseVariation();
study.start();
feature.init(variation);
telemetryOn();
}

// other times we need to do other things



16 changes: 16 additions & 0 deletions webExtension/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "webpack-webextension",
"version": "1.0.0",
"description": "A minimal example of how to use npm modules from within a WebExtension.",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack"
},
"license": "MPL-2.0",
"devDependencies": {
"webpack": "^2.3.2"
},
"dependencies": {
"left-pad": "^1.1.1"
}
}
104 changes: 104 additions & 0 deletions webExtension/shield-study.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/* shield under web extensions is just a library,
MEANT to be webpacked/browserified */

//let study = new SubclassedStudy(config);

class ShieldStudy {
constructor (config) {
this.config = config;
}
isEligible () {
return true;
}

chooseVariation () {
/* do some rng or deterministic magic */
}

stored () {
/*firstrun
branchname*/
}

isFirstRun () {
// can check if we have any local storage?
return true;
}

doOnce() {
}

cleanup () {
console.log("cleaing up")
}

checkForExpiration () {
return false
}

telemetry (...args) {
console.log("telemtry sent:", ...args);
}

start () {
console.log('starting up!')
}

die (reason) {
switch (reason) {
case "ineligible":
case "uninstall":
break;
default:
break;
}
}
go () {
// try to do all the things?
/*
if this.isFirstRun
checkEligible OR DIE
pick and else a branch
else
revive a branch
finally:
mark the experiment branch
start the clocks / timers
return the branch
and ask the feature to init it. // not our job
*/
}
}

class PrefStudy extends ShieldStudy {
constructor (config) {
// does special thing with the prefs key
}
isEligible () {
/*is the pref set*/
}
}

/*
if (study.eligible()) {
study.going().then(()=>
yourfeature.startup(study.branch)
)
}
"going" does this:
spec stuff:
- 1st startup
- choose branch once
- check eligible
- Every startup
- re-use branch
- init the feature
*/

exports.ShieldStudy = ShieldStudy;
exports.PrefStudy = PrefStudy;

11 changes: 11 additions & 0 deletions webExtension/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const path = require("path");

module.exports = {
entry: {
background_scripts: "./ourStudy.js",
},
output: {
path: path.resolve(__dirname, "addon"),
filename: "[name]/index.js"
}
};

0 comments on commit 466113c

Please sign in to comment.