-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3388a09
commit dffb029
Showing
15 changed files
with
111 additions
and
104 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,60 @@ | ||
// turnStyles preloading and injection | ||
|
||
// what kind of browser are we in | ||
const target = chrome || browser | ||
const syncDB = target.storage ? target.storage.sync : false | ||
const script = target.runtime.getURL("turnStyles.js") | ||
|
||
const stored = window.localStorage.getItem("tsdb") | ||
const remove = window.localStorage.getItem("ts-reset") | ||
const bridge = target.storage ? target.storage.sync : false | ||
|
||
const Attach = () => { | ||
if (remove) return Format() | ||
if (bridge) return Backup() | ||
if (syncDB) return Backup() | ||
else return Inject() | ||
} | ||
|
||
const Format = () => { | ||
// wipe out all tS data | ||
window.localStorage.removeItem("tsdb") | ||
window.localStorage.removeItem("ts-reset") | ||
if (bridge) bridge.remove([ "tsdb" ], () => Backup()) | ||
if (syncDB) syncDB.remove([ "tsdb" ], () => Backup()) | ||
else Inject() | ||
} | ||
|
||
const Backup = () => { | ||
// save to bridge (addon db) | ||
// save to syncDB (addon db) | ||
let save = stored && !remove | ||
let tsdb = save ? JSON.parse(stored) : false | ||
if (tsdb) bridge.set({ tsdb }) | ||
if (tsdb) syncDB.set({ tsdb }) | ||
// listen for db update messages | ||
window.addEventListener("message", Update) | ||
// fetch and inject our app data | ||
bridge.get([ "tsdb" ], db => Inject(db)) | ||
syncDB.get([ "tsdb" ], db => Inject(db)) | ||
} | ||
|
||
const Update = ev => { | ||
let tsdb = ev.data.tsdb | ||
if (tsdb) bridge.set({ tsdb: tsdb }) | ||
const Update = Event => { | ||
let tsdb = Event.data.tsdb | ||
if (tsdb) syncDB.set({ tsdb: tsdb }) | ||
} | ||
|
||
const Inject = db => { | ||
const Inject = DB => { | ||
// inject tS data and scripts | ||
let backup = db ? JSON.stringify(db.tsdb || {}) : false | ||
let script = target.runtime.getURL("turnStyles.js") | ||
let extURL = script.split("/turnStyles.js")[0] | ||
let tsSync = DB ? DB.tsdb : {} | ||
let tsBase = script.split("/turnStyles.js")[0] | ||
// inject the base URL/sync data | ||
window.localStorage.setItem("tsBase", extURL) | ||
window.localStorage.setItem("tsSync", backup) | ||
if (tsSync) tsSync = JSON.stringify(tsSync) | ||
window.localStorage.setItem("tsBase", tsBase) | ||
window.localStorage.setItem("tsSync", tsSync) | ||
// the main script gets us started | ||
Append(script) | ||
} | ||
|
||
const Append = js => { | ||
// append JS file/code to DOM | ||
let isFile = js.indexOf(".js") > -1 | ||
let script = document.createElement("script") | ||
if (isFile) script.src = js | ||
else script.textContent = js | ||
script.type = "text/javascript" | ||
document.body.append(script) | ||
const Append = JS => { | ||
let elem = document.createElement("script") | ||
elem.src = `${ JS }?v=${ Math.random() }` | ||
elem.type = "text/javascript" | ||
document.body.append(elem) | ||
} | ||
|
||
Attach() |
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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
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