diff options
author | SimonBrazell <simon@brazell.com.au> | 2021-01-10 22:04:03 +1100 |
---|---|---|
committer | SimonBrazell <simon@brazell.com.au> | 2021-01-10 22:04:03 +1100 |
commit | 944f2e6ef09a1534bac061acca2e3b3a4c13b13a (patch) | |
tree | 86bbd36f9896db47b6938d098658192bd31db01e /src/assets/javascripts | |
parent | Merge pull request #153 from somoso/patch-1 (diff) | |
download | libredirect-944f2e6ef09a1534bac061acca2e3b3a4c13b13a.zip |
Restructure code with helper modules, add search engine settings, & advanced settings collapsibles.
Diffstat (limited to 'src/assets/javascripts')
-rw-r--r-- | src/assets/javascripts/helpers/common.js | 9 | ||||
-rw-r--r-- | src/assets/javascripts/helpers/google-maps.js | 43 | ||||
-rw-r--r-- | src/assets/javascripts/helpers/google-search.js | 9 | ||||
-rw-r--r-- | src/assets/javascripts/helpers/instagram.js | 47 | ||||
-rw-r--r-- | src/assets/javascripts/helpers/reddit.js | 24 | ||||
-rw-r--r-- | src/assets/javascripts/helpers/twitter.js | 38 | ||||
-rw-r--r-- | src/assets/javascripts/helpers/youtube.js | 33 | ||||
-rw-r--r-- | src/assets/javascripts/localise.js | 19 | ||||
-rw-r--r-- | src/assets/javascripts/persist-invidious-prefs.js | 30 | ||||
-rw-r--r-- | src/assets/javascripts/remove-twitter-sw.js | 89 |
10 files changed, 341 insertions, 0 deletions
diff --git a/src/assets/javascripts/helpers/common.js b/src/assets/javascripts/helpers/common.js new file mode 100644 index 00000000..6edd3d3c --- /dev/null +++ b/src/assets/javascripts/helpers/common.js @@ -0,0 +1,9 @@ +export default class { + static filterInstances(instances) { + return instances.filter((instance) => !instance.includes(".onion")); + } + + static getRandomInstance(instances) { + return instances[~~(instances.length * Math.random())]; + } +} diff --git a/src/assets/javascripts/helpers/google-maps.js b/src/assets/javascripts/helpers/google-maps.js new file mode 100644 index 00000000..3a041c67 --- /dev/null +++ b/src/assets/javascripts/helpers/google-maps.js @@ -0,0 +1,43 @@ +export default class { + static targets = /https?:\/\/(((www|maps)\.)?(google\.).*(\/maps)|maps\.(google\.).*)/; + static redirects = ["https://openstreetmap.org"]; + static mapCentreRegex = /@(-?\d[0-9.]*),(-?\d[0-9.]*),(\d{1,2})[.z]/; + static dataLatLngRegex = /(!3d|!4d)(-?[0-9]{1,10}.[0-9]{1,10})/g; + static placeRegex = /\/place\/(.*)\//; + static travelModes = { + driving: "fossgis_osrm_car", + walking: "fossgis_osrm_foot", + bicycling: "fossgis_osrm_bike", + transit: "fossgis_osrm_car", // not implemented on OSM, default to car. + }; + static layers = { + none: "S", + transit: "T", + traffic: "S", // not implemented on OSM, default to standard. + bicycling: "C", + }; + static addressToLatLng(address, callback) { + const xmlhttp = new XMLHttpRequest(); + xmlhttp.onreadystatechange = () => { + if (xmlhttp.readyState === XMLHttpRequest.DONE) { + if (xmlhttp.status === 200) { + const json = JSON.parse(xmlhttp.responseText)[0]; + if (json) { + callback( + `${json.lat}%2C${json.lon}`, + `${json.boundingbox[2]},${json.boundingbox[1]},${json.boundingbox[3]},${json.boundingbox[0]}` + ); + } + } else { + console.info("Error: Status is " + xmlhttp.status); + } + } + }; + xmlhttp.open( + "GET", + `https://nominatim.openstreetmap.org/search/${address}?format=json&limit=1`, + false + ); + xmlhttp.send(); + } +} diff --git a/src/assets/javascripts/helpers/google-search.js b/src/assets/javascripts/helpers/google-search.js new file mode 100644 index 00000000..dc12042f --- /dev/null +++ b/src/assets/javascripts/helpers/google-search.js @@ -0,0 +1,9 @@ +export default class { + static targets = /https?:\/\/(((www|maps)\.)?(google\.).*(\/search)|search\.(google\.).*)/; + static redirects = [ + { link: "https://duckduckgo.com", q: "/" }, + { link: "https://startpage.com", q: "/search/" }, + { link: "https://www.qwant.com", q: "/" }, + { link: "https://www.mojeek.com", q: "/search" }, + ]; +} diff --git a/src/assets/javascripts/helpers/instagram.js b/src/assets/javascripts/helpers/instagram.js new file mode 100644 index 00000000..bd0ad9f5 --- /dev/null +++ b/src/assets/javascripts/helpers/instagram.js @@ -0,0 +1,47 @@ +export default class { + static targets = [ + "instagram.com", + "www.instagram.com", + "help.instagram.com", + "about.instagram.com", + ]; + static redirects = [ + "https://bibliogram.art", + "https://bibliogram.snopyta.org", + "https://bibliogram.pussthecat.org", + "https://bibliogram.nixnet.services", + "https://bg.endl.site", + "https://bibliogram.13ad.de", + "https://bibliogram.pixelfed.uno", + "https://bibliogram.ethibox.fr", + "https://bibliogram.hamster.dance", + "https://bibliogram.kavin.rocks", + "https://bibliogram.ggc-project.de", + ]; + static reservedPaths = [ + "about", + "explore", + "support", + "press", + "api", + "privacy", + "safety", + "admin", + "graphql", + "accounts", + "help", + "terms", + "contact", + "blog", + "igtv", + "u", + "p", + "fragment", + "imageproxy", + "videoproxy", + ".well-known", + "tv", + "reel", + ]; + static bypassPaths = /\/(accounts\/|embeds?.js)/; +} diff --git a/src/assets/javascripts/helpers/reddit.js b/src/assets/javascripts/helpers/reddit.js new file mode 100644 index 00000000..46fff4cf --- /dev/null +++ b/src/assets/javascripts/helpers/reddit.js @@ -0,0 +1,24 @@ +export default class { + static targets = [ + "www.reddit.com", + "np.reddit.com", + "new.reddit.com", + "amp.reddit.com", + ]; + static redirects = [ + "https://old.reddit.com", // desktop + "https://i.reddit.com", // mobile + // teddit: privacy w/ old UI + "https://teddit.net", + "https://teddit.ggc-project.de", + "https://teddit.kavin.rocks", + "https://snew.notabug.io", // anti-censorship + // libreddit: privacy w/ modern UI + "https://libredd.it", + "https://libreddit.spike.codes", + "https://libreddit.kavin.rocks", + "https://libreddit.insanity.wtf", + "https://libreddit.dothq.co", + ]; + static bypassPaths = /\/(gallery\/poll\/rpan\/settings\/topics)/; +} diff --git a/src/assets/javascripts/helpers/twitter.js b/src/assets/javascripts/helpers/twitter.js new file mode 100644 index 00000000..1ebc025b --- /dev/null +++ b/src/assets/javascripts/helpers/twitter.js @@ -0,0 +1,38 @@ +export default class { + /* + Please remember to also update the manifest.json file + (content_scripts > matches, 'remove-twitter-sw.js') + when updating this list: + */ + static targets = [ + "twitter.com", + "www.twitter.com", + "mobile.twitter.com", + "pbs.twimg.com", + "video.twimg.com", + ]; + static redirects = [ + "https://nitter.net", + "https://nitter.snopyta.org", + "https://nitter.42l.fr", + "https://nitter.nixnet.services", + "https://nitter.13ad.de", + "https://nitter.pussthecat.org", + "https://nitter.mastodont.cat", + "https://nitter.dark.fail", + "https://nitter.tedomum.net", + "https://nitter.cattube.org", + "https://nitter.fdn.fr", + "https://nitter.1d4.us", + "https://nitter.kavin.rocks", + "https://tweet.lambda.dance", + "https://nitter.cc", + "https://nitter.weaponizedhumiliation.com", + "https://nitter.vxempire.xyz", + "https://nitter.unixfox.eu", + "http://3nzoldnxplag42gqjs23xvghtzf6t6yzssrtytnntc6ppc7xxuoneoad.onion", + "http://nitter.l4qlywnpwqsluw65ts7md3khrivpirse744un3x7mlskqauz5pyuzgqd.onion", + "http://nitterlgj3n5fgwesu3vxc5h67ruku33nqaoeoocae2mvlzhsu6k7fqd.onion", + "http://npf37k3mtzwxreiw52ccs5ay4e6qt2fkcs2ndieurdyn2cuzzsfyfvid.onion", + ]; +} diff --git a/src/assets/javascripts/helpers/youtube.js b/src/assets/javascripts/helpers/youtube.js new file mode 100644 index 00000000..3d1fcd27 --- /dev/null +++ b/src/assets/javascripts/helpers/youtube.js @@ -0,0 +1,33 @@ +export default class { + static targets = [ + "m.youtube.com", + "youtube.com", + "img.youtube.com", + "www.youtube.com", + "youtube-nocookie.com", + "www.youtube-nocookie.com", + "youtu.be", + "s.ytimg.com", + "music.youtube.com", + ]; + /* + Please remember to also update the manifest.json file + (content_scripts > matches, 'persist-invidious-prefs.js') + when updating this list: + */ + static redirects = [ + "https://invidious.snopyta.org", + "https://invidious.xyz", + "https://invidious.kavin.rocks", + "https://tube.connect.cafe", + "https://invidious.zapashcanon.fr", + "https://invidiou.site", + "https://vid.mint.lgbt", + "https://invidious.site", + "https://yewtu.be", + "http://fz253lmuao3strwbfbmx46yu7acac2jz27iwtorgmbqlkurlclmancad.onion", + "http://qklhadlycap4cnod.onion", + "http://c7hqkpkpemu6e7emz5b4vyz7idjgdvgaaa3dyimmeojqbgpea3xqjoid.onion", + "http://w6ijuptxiku4xpnnaetxvnkc5vqcdu7mgns2u77qefoixi63vbvnpnqd.onion", + ]; +} diff --git a/src/assets/javascripts/localise.js b/src/assets/javascripts/localise.js new file mode 100644 index 00000000..cbe5c191 --- /dev/null +++ b/src/assets/javascripts/localise.js @@ -0,0 +1,19 @@ +window.browser = window.browser || window.chrome; + +function localisePage() { + var data = document.querySelectorAll("[data-localise]"); + + for (var i in data) + if (data.hasOwnProperty(i)) { + var obj = data[i]; + var tag = obj.getAttribute("data-localise").toString(); + + var msg = tag.replace(/__MSG_(\w+)__/g, function (_match, v1) { + return v1 ? browser.i18n.getMessage(v1) : null; + }); + + if (msg && msg !== tag) obj.textContent = msg; + } +} + +localisePage(); diff --git a/src/assets/javascripts/persist-invidious-prefs.js b/src/assets/javascripts/persist-invidious-prefs.js new file mode 100644 index 00000000..4c13a310 --- /dev/null +++ b/src/assets/javascripts/persist-invidious-prefs.js @@ -0,0 +1,30 @@ +'use strict'; + +window.browser = window.browser || window.chrome; + +function getCookie() { + let ca = document.cookie.split(';'); + for (let i = 0; i < ca.length; i++) { + let c = ca[i]; + while (c.charAt(0) == ' ') c = c.substring(1, c.length); + if (c.indexOf('PREFS=') == 0) { + return JSON.parse( + decodeURIComponent(c.substring('PREFS='.length, c.length)) + ) + }; + } + return {}; +} + +browser.storage.sync.get( + ['alwaysProxy', 'videoQuality', 'invidiousDarkMode', 'persistInvidiousPrefs'], + (result) => { + if (result.persistInvidiousPrefs) { + const prefs = getCookie(); + prefs.local = result.alwaysProxy; + prefs.quality = result.videoQuality; + prefs.dark_mode = result.invidiousDarkMode; + document.cookie = `PREFS=${encodeURIComponent(JSON.stringify(prefs))}`; + } + } +); \ No newline at end of file diff --git a/src/assets/javascripts/remove-twitter-sw.js b/src/assets/javascripts/remove-twitter-sw.js new file mode 100644 index 00000000..c39b4c9e --- /dev/null +++ b/src/assets/javascripts/remove-twitter-sw.js @@ -0,0 +1,89 @@ +"use strict"; + +let disableNitter; +let nitterInstance; +let redirectBypassFlag; +let exceptions; + +window.browser = window.browser || window.chrome; + +Promise.all([ + import(browser.extension.getURL("src/assets/javascripts/helpers/common.js")), + import(browser.extension.getURL("src/assets/javascripts/helpers/twitter.js")), +]).then( + (helpers) => { + let commonHelper; + let twitterHelper; + [commonHelper, twitterHelper] = helpers; + + function isNotException(url) { + return !exceptions.some((regex) => regex.test(url.href)); + } + + function shouldRedirect(url) { + return ( + !redirectBypassFlag && + isNotException(url) && + !disableNitter && + url.host !== nitterInstance && + !url.pathname.includes("/home") + ); + } + + function redirectTwitter(url) { + if (url.host.split(".")[0] === "pbs") { + return `${nitterInstance}/pic/${encodeURIComponent(url.href)}`; + } else if (url.host.split(".")[0] === "video") { + return `${nitterInstance}/gif/${encodeURIComponent(url.href)}`; + } else { + return `${nitterInstance}${url.pathname}${url.search}`; + } + } + + browser.storage.sync.get( + [ + "nitterInstance", + "disableNitter", + "removeTwitterSW", + "redirectBypassFlag", + "exceptions", + ], + (result) => { + redirectBypassFlag = result.redirectBypassFlag; + browser.storage.sync.set({ + redirectBypassFlag: false, + }); + if (!result.removeTwitterSW) { + disableNitter = result.disableNitter; + nitterInstance = + result.nitterInstance || + commonHelper.default.getRandomInstance( + twitterHelper.default.redirects + ); + exceptions = result.exceptions + ? result.exceptions.map((e) => { + return new RegExp(e); + }) + : []; + navigator.serviceWorker.getRegistrations().then((registrations) => { + for (let registration of registrations) { + if (registration.scope === "https://twitter.com/") { + registration.unregister(); + console.log("Unregistered Twitter SW", registration); + } + } + }); + const url = new URL(window.location); + if (shouldRedirect(url)) { + const redirect = redirectTwitter(url); + console.info("Redirecting", `"${url.href}"`, "=>", `"${redirect}"`); + window.location = redirect; + } + } + } + ); + }, + (error) => { + console.error(error); + } +); |