Skip to content

Commit

Permalink
Add a basic status indicator badge
Browse files Browse the repository at this point in the history
First step towards providing users with more feedback about the SRM
check performed (e.g. #5 and/or #6).

I find it strange that I needed to add browser_action for this, but
there seems to be no way around this. I don't think this actually gives
the extension more permissions, but documentation is a bit unclear.
  • Loading branch information
lukasvermeer committed Mar 9, 2020
1 parent df121e9 commit 934d367
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 11 deletions.
45 changes: 35 additions & 10 deletions chrome-extension/src/background.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,43 @@
// Send message to content.js when URL changes
chrome.tabs.onUpdated.addListener(function
(tabId, changeInfo, tab) {
(tabId, changeInfo, tab) {
if (changeInfo.url) {
chrome.tabs.sendMessage(tabId, {
message: 'URL has changed',
url: changeInfo.url
})
chrome.tabs.sendMessage(tabId, {
message: 'URL has changed',
url: changeInfo.url
})
}
});
});

chrome.runtime.onInstalled.addListener(function(details) {
if (details.reason == "install") {
if (chrome.runtime.setUninstallURL) {
chrome.runtime.setUninstallURL('https://forms.gle/Cgv34WVhgms9MChv7');
}
if (details.reason == "install") {
if (chrome.runtime.setUninstallURL) {
chrome.runtime.setUninstallURL('https://forms.gle/Cgv34WVhgms9MChv7');
}
}
});

chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
console.log(message);
if (message.badgeBackgroundColor) {
chrome.browserAction.setBadgeBackgroundColor({color: message.badgeBackgroundColor});
}
if (message.badgeText) {
chrome.tabs.get(sender.tab.id, function(tab) {
if (chrome.runtime.lastError) {
return; // the prerendered tab has been nuked, happens in omnibox search
}
if (tab.index >= 0) { // tab is visible
chrome.browserAction.setBadgeText({tabId:tab.id, text:message.badgeText});
} else { // prerendered tab, invisible yet, happens quite rarely
var tabId = sender.tab.id, text = message.badgeText;
chrome.webNavigation.onCommitted.addListener(function update(details) {
if (details.tabId == tabId) {
chrome.browserAction.setBadgeText({tabId: tabId, text: text});
chrome.webNavigation.onCommitted.removeListener(update);
}
});
}
});
}
});
10 changes: 10 additions & 0 deletions chrome-extension/src/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const platforms = {
if (request.message === 'URL has changed') {
newIframe();
srmChecked = false;
chrome.runtime.sendMessage({badgeText: 'ON', badgeBackgroundColor: 'gray'});
}
},
);
Expand Down Expand Up @@ -145,6 +146,7 @@ const platforms = {
if (request.message === 'URL has changed') {
newIframe();
srmChecked = false;
chrome.runtime.sendMessage({badgeText: 'ON', badgeBackgroundColor: 'gray'});
}
},
);
Expand Down Expand Up @@ -205,6 +207,7 @@ const platforms = {
if (request.message === 'URL has changed') {
newIframe();
srmChecked = false;
chrome.runtime.sendMessage({badgeText: 'ON', badgeBackgroundColor: 'gray'});
}
},
);
Expand Down Expand Up @@ -278,6 +281,7 @@ const platforms = {
if (request.message === 'URL has changed') {
newIframe();
srmChecked = false;
chrome.runtime.sendMessage({badgeText: 'ON', badgeBackgroundColor: 'gray'});
}
},
);
Expand Down Expand Up @@ -330,6 +334,7 @@ const platforms = {
if (request.message === 'URL has changed') {
newIframe();
srmChecked = false;
chrome.runtime.sendMessage({badgeText: 'ON', badgeBackgroundColor: 'gray'});
}
},
);
Expand Down Expand Up @@ -407,6 +412,7 @@ const platforms = {
if (request.message === 'URL has changed') {
newIframe();
srmChecked = false;
chrome.runtime.sendMessage({badgeText: 'ON', badgeBackgroundColor: 'gray'});
}
},
);
Expand Down Expand Up @@ -474,6 +480,7 @@ const platforms = {
function newIframe() {
if (location.href.slice(-8) === '/reports') {
const oldIframe = document.getElementById('iframeforweight');
chrome.runtime.sendMessage({badgeText: 'ON', badgeBackgroundColor: 'gray'});
if (oldIframe != null) {
oldIframe.parentNode.removeChild(oldIframe);
}
Expand All @@ -493,6 +500,7 @@ const platforms = {
if (request.message === 'URL has changed') {
newIframe();
srmChecked = false;
chrome.runtime.sendMessage({badgeText: 'ON', badgeBackgroundColor: 'gray'});
}
},
);
Expand Down Expand Up @@ -555,3 +563,5 @@ const platforms = {
};

platforms[platform].init();

chrome.runtime.sendMessage({badgeText: 'ON', badgeBackgroundColor: 'gray'});
4 changes: 3 additions & 1 deletion chrome-extension/src/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Sample Ratio Mismatch (SRM) Checker",
"short_name": "SRM Check",
"version": "0.0.4",
"version": "0.0.5",
"description": "Automatically performs Sample Ratio Mismatch (SRM) test and flags potential issues on supported experimentation platforms.",
"permissions": [
"tabs"
Expand All @@ -23,6 +23,8 @@
"background": {
"scripts": [ "background.js" ]
},
"browser_action": {
},
"icons": {
"128": "icon128.png"
},
Expand Down
2 changes: 2 additions & 0 deletions chrome-extension/src/srm.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ function checkSRM(observed, expected) {
const pval = computeSRM(observed, expected);
if (pval < params.pValueThreshold) {
platforms[platform].flagSRM(pval);
chrome.runtime.sendMessage({badgeText: 'SRM', badgeBackgroundColor: 'red'});
} else {
platforms[platform].unflagSRM();
chrome.runtime.sendMessage({badgeText: 'OK', badgeBackgroundColor: 'green'});
}
}

0 comments on commit 934d367

Please sign in to comment.