Created
August 21, 2017 08:02
-
-
Save nickvergessen/b9883401cd931f16bfa602eada3c81aa to your computer and use it in GitHub Desktop.
moz-extension://1a50ab6f-d572-49b1-a808-93fe89112531/data/content_script/inject.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var n_dark_themes = 33; | |
var tab = document.location.href; | |
var link = document.getElementById("dark-mode"); | |
var head = document.documentElement || document.head || document.querySelector("head"); | |
var hostname = function (url) { | |
url = url.replace("www.", ''); | |
var s = url.indexOf("//") + 2; | |
if (s > 1) { | |
var o = url.indexOf('/', s); | |
if (o > 0) return url.substring(s, o); | |
else { | |
o = url.indexOf('?', s); | |
if (o > 0) return url.substring(s, o); | |
else return url.substring(s); | |
} | |
} else return url; | |
}; | |
var custom = { | |
"ebay": ".ebay.com", | |
"yahoo": "www.yahoo.", | |
"twitch": ".twitch.tv", | |
"github": "github.com", | |
"docs": "docs.google.", | |
"bing": "www.bing.com", | |
"amazon": "www.amazon.", | |
"gmail": "mail.google.", | |
"tumblr": "www.tumblr.", | |
"twitter": "twitter.com", | |
"inbox": "inbox.google.", | |
"drive": "drive.google.", | |
"sites": "sites.google.", | |
"youtube": "www.youtube.", | |
"dropbox": "www.dropbox.", | |
"reddit": "www.reddit.com", | |
"maps": ".google.com/maps/", | |
"facebook": "www.facebook.", | |
"wikipedia": "wikipedia.org", | |
"duckduckgo": "duckduckgo.com", | |
"stackoverflow": "stackoverflow.com" | |
}; | |
if (!link) { | |
link = document.createElement("link"); | |
link.setAttribute("type", "text/css"); | |
link.setAttribute("id", "dark-mode"); | |
link.setAttribute("rel", "stylesheet"); | |
if (head) head.appendChild(link); | |
} | |
var update = function () { | |
var tmp = {}; | |
for (var name in custom) tmp[name] = true; | |
for (var i = 1; i <= n_dark_themes; i++) tmp['dark_' + i] = false; | |
tmp["dark_1"] = true; | |
tmp["whitelist"] = []; | |
tmp["state"] = "light"; | |
chrome.storage.local.get(tmp, function (e) { | |
var href = '', id = null; | |
var _tab = hostname(tab); | |
for (var i = 0; i < e.whitelist.length; i++) { | |
if (e.whitelist[i] === _tab) return link.setAttribute("href", href); | |
} | |
/* */ | |
for (var i = 1; i <= n_dark_themes; i++) { | |
if (e['dark_' + i]) { | |
id = i; | |
break; | |
} | |
} | |
/* */ | |
for (var name in custom) { | |
if (e[name]) { | |
if (tab.indexOf(custom[name]) !== -1) { | |
href = e.state === "dark" ? chrome.runtime.getURL("data/content_script/custom/" + name + ".css") : ''; | |
link.setAttribute("href", href); | |
return; | |
} | |
} | |
} | |
/* */ | |
href = e.state === "dark" ? (id ? chrome.runtime.getURL('data/content_script/general/dark_' + id + '.css') : '') : ''; | |
link.setAttribute("href", href); | |
}); | |
}; | |
var init = function (e) { | |
if (e) tab = e; | |
update(); | |
}; | |
var observer = new MutationObserver(function (e) { | |
if (head) head.appendChild(link); | |
observer.disconnect(); | |
}); | |
if (window === window.top) update(); | |
else chrome.runtime.sendMessage({"message": "top"}, init); | |
chrome.storage.onChanged.addListener(update); | |
observer.observe(document, {"childList": true, "subtree": true}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment