Skip to content

Commit

Permalink
WIP - Auto send users to origins they have set to go to. Fixes mozill…
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanKingston committed Mar 18, 2017
1 parent 9d5223c commit 4bb53ef
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 2 deletions.
79 changes: 79 additions & 0 deletions webextension/background.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,82 @@
browser.contextMenus.create({
id: "open-in-this-container",
title: "Always Open in This Container",
contexts: ["all"],
});

function getUserContextIdFromCookieStore(cookieStore) {
const container = cookieStore.replace("firefox-container-", "");
if (container !== cookieStore) {
return container;
}
return false;
}

browser.runtime.onMessage.addListener((request) => {
if (request.neverAsk === true) {
storageArea.set({neverAsk: true});
}
});

const storagePrefix = "originContainerMap@@_";
const storageArea = browser.storage.local;

browser.contextMenus.onClicked.addListener((info, tab) => {
const pageUrl = new window.URL(info.pageUrl);
const userContextId = getUserContextIdFromCookieStore(tab.cookieStoreId);
// Mapping ${pageUrl.origin} to ${userContextId}
if (userContextId) {
const originStoreKey = `${storagePrefix}${pageUrl.origin}`;
storageArea.set({[originStoreKey]: userContextId});
}
});

browser.webRequest.onBeforeRequest.addListener((options) => {
if (options.frameId !== 0 || options.tabId === -1) {
return {};
}
const pageUrl = new window.URL(options.url);
const originStoreKey = `${storagePrefix}${pageUrl.origin}`;
return Promise.all([
browser.tabs.get(options.tabId),
storageArea.get([originStoreKey, "neverAsk"])
]).then(([tab, storage]) => {
const userContextId = getUserContextIdFromCookieStore(tab.cookieStoreId);
let neverAsk = false;
if (!(originStoreKey in storage)) {
return;
}
if ("neverAsk" in storage) {
neverAsk = storage.neverAsk;
}
const mapUserContextId = storage[originStoreKey];
if (userContextId === mapUserContextId) {
return {};
}

loadOtherPage(options.url, mapUserContextId, neverAsk);
// If the user just opened the tab, we can auto close it
if (tab.url === "about:newtab" || tab.url === "about:home") {
browser.tabs.remove(tab.id);
}
return {
cancel: true,
};
}).catch((e) => {
throw e;
});
},{urls: ["<all_urls>"], types: ["main_frame"]}, ["blocking"]);


function loadOtherPage(url, userContextId, neverAsk = false) {
const loadPage = browser.extension.getURL("confirm-page.html");
// If the user has explicitly checked "Never Ask Again" on the warning page we will send them straight there
if (neverAsk) {
browser.tabs.create({url, cookieStoreId: `firefox-container-${userContextId}`});
} else {
browser.tabs.create({url: `${loadPage}?url=${url}`, cookieStoreId: `firefox-container-${userContextId}`});
}
}

const themeManager = {
existingTheme: null,
Expand Down
30 changes: 30 additions & 0 deletions webextension/confirm-page.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Containers confirm navigation</title>
<link rel="stylesheet" href="/css/confirm-page.css">
</head>
<body>
<main>
<h1>Hey, we heard you liked containers!</h1>
<form id="redirect-form">
<p>
A page told us to open:
</p>
<div id="redirect-url"></div>
<p>
You asked for <span id="redirect-origin"></span> to always open in <span id="container-name">this</span> container, are you sure you want to confirm?<br />
</p>
<p>If you didn't mean to open this link or it looks suspicious don't click it.</p>
<br />
<p>
<label>Never ask me this again? <input id="never-ask" type="checkbox" /></label>
<br />
<button id="confirm" class="button primary">Take me there</button>
</p>
</form>
</main>

<script src="js/confirm-page.js"></script>
</body>
</html>
31 changes: 31 additions & 0 deletions webextension/css/confirm-page.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* General Rules and Resets */
body {
background: url(/img/onboarding-1.png) top right no-repeat;
inline-size: 600px;
}

html {
box-sizing: border-box;
font: message-box;
}

#redirect-url,
#redirect-origin {
font-weight: bold;
}

#confirm {
float: right;
}

.button.primary {
background-color: #0996f8;
block-size: 54px;
border: none;
color: white;
text-align: center;
}

.button.primary:hover {
background-color: #0675d3;
}
2 changes: 1 addition & 1 deletion webextension/css/popup.css
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ span ~ .panel-header-text {
.panel-footer {
align-items: center;
background: #efefef;
block-size: 55px;
block-size: 54px;
border-block-end: 1px solid #d8d8d8;
color: #000;
display: flex;
Expand Down
25 changes: 25 additions & 0 deletions webextension/js/confirm-page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const redirectUrl = new URL(window.location).searchParams.get("url");
document.getElementById("redirect-url").textContent = redirectUrl;
const redirectOrigin = new URL(redirectUrl).origin;
document.getElementById("redirect-origin").textContent = redirectOrigin;

document.getElementById("redirect-form").addEventListener("submit", (e) => {
e.preventDefault();
const neverAsk = document.getElementById("never-ask").checked;
// Sending neverAsk message to background to store for next time we see this process
if (neverAsk) {
browser.runtime.sendMessage({
neverAsk: true
}).then(() => {
redirect();
}).catch(() => {
// Can't really do much here user will have to click it again
});
}
redirect();
});

function redirect() {
const redirectUrl = document.getElementById("redirect-url").textContent;
document.location.replace(redirectUrl);
}
8 changes: 7 additions & 1 deletion webextension/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@
"homepage_url": "https://testpilot.firefox.com/",

"permissions": [
"storage",
"cookies",
"tabs"
"tabs",
"contextMenus",
"webRequestBlocking",
"webRequest",
"<all_urls>",
"activeTab"
],

"browser_action": {
Expand Down

0 comments on commit 4bb53ef

Please sign in to comment.