Skip to content

Commit

Permalink
Merge pull request containers-everywhere#43 from berrnd/feature-ignor…
Browse files Browse the repository at this point in the history
…e-optionally-youtube-and-searchpages

Add options to ignore YouTube and Google Search Pages
  • Loading branch information
Perflyst authored Apr 17, 2019
2 parents 7b40e4a + 46303cd commit 1fc0603
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 2 deletions.
50 changes: 49 additions & 1 deletion background.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ const DEVELOPER_DOMAINS = [
"madewithcode.com", "design.google", "gallery.io", "domains.google", "material.io", "android.com", "chromium.org", "cobrasearch.com", "chromecast.com", "chrome.com", "chromebook.com", "madewithcode.com", "whatbrowser.org", "withgoogle.com", "web.dev",
];

const SEARCHPAGE_PATHS = [
"/search", "/maps", "/flights"
]


GOOGLE_DOMAINS = GOOGLE_DOMAINS.concat(GOOGLE_INTL_DOMAINS)
.concat(GOOGLE_SERVICES).concat(YOUTUBE_DOMAINS).concat(BLOGSPOT_DOMAINS).concat(ALPHABET_DOMAINS)
Expand All @@ -44,10 +48,13 @@ const MAC_ADDON_ID = "@testpilot-containers";

let macAddonEnabled = false;
let googleCookieStoreId = null;
let extensionSettings = {};

const canceledRequests = {};
const tabsWaitingToLoad = {};
const googleHostREs = [];
const youtubeHostREs = [];
const searchpagePathREs = [];

async function isMACAddonEnabled () {
try {
Expand Down Expand Up @@ -147,6 +154,16 @@ function generateGoogleHostREs () {
for (let googleDomain of GOOGLE_DOMAINS) {
googleHostREs.push(new RegExp(`^(.*\\.)?${googleDomain}$`));
}
for (let youtubeDomain of YOUTUBE_DOMAINS) {
youtubeHostREs.push(new RegExp(`^(.*\\.)?${youtubeDomain}$`));
}
for (let searchpagePath of SEARCHPAGE_PATHS) {
searchpagePathREs.push(new RegExp(`^${searchpagePath}(.*)`));
}
}

async function loadExtensionSettings () {
extensionSettings = await browser.storage.sync.get();
}

async function clearGoogleCookies () {
Expand Down Expand Up @@ -234,13 +251,43 @@ function isGoogleURL (url) {
return false;
}

function isYouTubeURL (url) {
const parsedUrl = new URL(url);
for (let youtubeHostRE of youtubeHostREs) {
if (youtubeHostRE.test(parsedUrl.host)) {
return true;
}
}
return false;
}

function isSearchPageURL (url) {
const parsedUrl = new URL(url);
for (let searchpagePathRE of searchpagePathREs) {
if (searchpagePathRE.test(parsedUrl.pathname)) {
return true;
}
}
return false;
}

function shouldContainInto (url, tab) {
if (!url.startsWith("http")) {
// we only handle URLs starting with http(s)
return false;
}

if (isGoogleURL(url)) {
let handleUrl = isGoogleURL(url);

if (extensionSettings.ignore_youtube && isYouTubeURL(url)) {
handleUrl = false;
}

if (extensionSettings.ignore_searchpages && isSearchPageURL(url)) {
handleUrl = false;
}

if (handleUrl) {
if (tab.cookieStoreId !== googleCookieStoreId) {
// Google-URL outside of Google Container Tab
// Should contain into Google Container
Expand Down Expand Up @@ -376,6 +423,7 @@ async function containGoogle (options) {
console.log(error);
return;
}
loadExtensionSettings();
clearGoogleCookies();
generateGoogleHostREs();

Expand Down
8 changes: 7 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,16 @@
"management",
"tabs",
"webRequestBlocking",
"webRequest"
"webRequest",
"storage"
],

"background": {
"scripts": ["background.js"]
},

"options_ui": {
"page": "options.html",
"browser_style": true
}
}
40 changes: 40 additions & 0 deletions options.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!DOCTYPE html>

<html>

<head>
<meta charset="utf-8">
</head>

<body>
<form>

<h1>Settings</h1>

<hr>

<p>
<label>
<input type="checkbox" id="ignore_youtube" value="1">
Ignore YouTube<br>
<em>(Means don't use Google Container for YouTube sites.)<em>
</label>
</p>

<p>
<label>
<input type="checkbox" id="ignore_searchpages" value="1">
Ignore search pages<br>
<em>(Means don't use Google Container on Google sites with a path of <code>/search</code>, <code>/maps</code> or <code>/flights</code>.)<em>
</label>
</p>

<button type="submit">Save settings</button>

<hr>

</form>
<script src="options.js"></script>
</body>

</html>
26 changes: 26 additions & 0 deletions options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
function onOptionsPageSave(e)
{
e.preventDefault();

// Save settings
browser.storage.sync.set({
"ignore_youtube": document.querySelector("#ignore_youtube").checked,
"ignore_searchpages": document.querySelector("#ignore_searchpages").checked
});

browser.runtime.reload();
}

function onOptionsPageLoaded()
{
// Load saved settings or use defaults when nothing was saved yet
var storageItem = browser.storage.sync.get();
storageItem.then((res) =>
{
document.querySelector("#ignore_youtube").checked = res.ignore_youtube || false;
document.querySelector("#ignore_searchpages").checked = res.ignore_searchpages || false;
});
}

document.addEventListener("DOMContentLoaded", onOptionsPageLoaded);
document.querySelector("form").addEventListener("submit", onOptionsPageSave);

0 comments on commit 1fc0603

Please sign in to comment.