From a1b0d078d79bd8d5bc63a3eba103cbd96fcf25b5 Mon Sep 17 00:00:00 2001 From: ManeraKai Date: Sat, 5 Mar 2022 14:16:58 +0300 Subject: [PATCH] Added Bibliogram settings and theme support #80 --- src/assets/javascripts/helpers/general.js | 4 +- src/assets/javascripts/helpers/instagram.js | 74 ++++++++++++++++++--- src/pages/background/background.js | 1 + src/pages/options/general/general.js | 1 - 4 files changed, 68 insertions(+), 12 deletions(-) diff --git a/src/assets/javascripts/helpers/general.js b/src/assets/javascripts/helpers/general.js index 1537928..fd14f53 100644 --- a/src/assets/javascripts/helpers/general.js +++ b/src/assets/javascripts/helpers/general.js @@ -13,7 +13,7 @@ let theme; const getTheme = () => theme; function setTheme(val) { theme = val - browser.storage.local.set({ theme }); + browser.storage.local.set({ theme, instancesCookies: [] }); console.log("theme: ", theme) } @@ -60,7 +60,7 @@ async function init() { if (r.exceptions) exceptions = r.exceptions; alwaysUsePreferred = r.alwaysUsePreferred ?? false; - theme = r.them ?? "DEFAULT" + theme = r.theme ?? "DEFAULT" applyThemeToSites = r.applyThemeToSites ?? false; resolve(); diff --git a/src/assets/javascripts/helpers/instagram.js b/src/assets/javascripts/helpers/instagram.js index c837bd2..5dfa4e8 100644 --- a/src/assets/javascripts/helpers/instagram.js +++ b/src/assets/javascripts/helpers/instagram.js @@ -122,7 +122,6 @@ function redirect(url, type) { if (type !== "main_frame" || url.pathname.match(bypassPaths)) return 'CANCEL'; // Do not redirect /accounts, /embeds.js, or anything other than main_frame - let instancesList; if (protocol == 'normal') instancesList = [...bibliogramNormalRedirectsChecks, ...bibliogramNormalCustomRedirects]; else if (protocol == 'tor') instancesList = [...bibliogramTorRedirectsChecks, ...bibliogramTorCustomRedirects]; @@ -161,6 +160,50 @@ function changeInstance(url) { return `${randomInstance}${url.pathname}${url.search}`; } +function isBibliogram(url) { + let protocolHost = `${url.protocol}//${url.host}`; + return [ + ...redirects.bibliogram.normal, + ...redirects.bibliogram.tor, + ...bibliogramNormalCustomRedirects, + ...bibliogramTorCustomRedirects, + ].includes(protocolHost); +} + +let instancesCookies; +let theme; +let applyThemeToSites; +function initBibliogramCookies(url) { + let protocolHost = `${url.protocol}//${url.host}`; + browser.cookies.get({ + url: protocolHost, + name: "settings" + }).then( + cookie => { + if (!cookie || !instancesCookies.includes(protocolHost)) { + console.log(`initing cookie for ${protocolHost}`); + let request = new XMLHttpRequest(); + request.open("POST", `${protocolHost}/settings/return?referrer=%2F`); + + if (applyThemeToSites) { + let themeValue; + if (theme == 'light') themeValue = "classic"; + if (theme == 'dark') themeValue = "pussthecat.org-v2" + + if (themeValue) { + let data = `csrf=x&theme=${themeValue}`; + request.send(data); + if (!instancesCookies.includes(protocolHost)) instancesCookies.push(protocolHost); + browser.storage.local.set({ instancesCookies }) + } + } + } else { + console.log("cookie url", protocolHost); + console.log("instancesCookies", instancesCookies); + }; + }) + +} async function init() { return new Promise((resolve) => { @@ -171,6 +214,11 @@ async function init() { "disableInstagram", "instagramRedirects", + "theme", + "applyThemeToSites", + + "instancesCookies", + "bibliogramNormalRedirectsChecks", "bibliogramTorRedirectsChecks", @@ -178,20 +226,25 @@ async function init() { "bibliogramTorCustomRedirects", "instagramProtocol" ], - (result) => { - disable = result.disableInstagram ?? false; + r => { + disable = r.disableInstagram ?? false; redirects.bibliogram = dataJson.bibliogram; - if (result.instagramRedirects) redirects = result.instagramRedirects + if (r.instagramRedirects) redirects = r.instagramRedirects - bibliogramNormalRedirectsChecks = result.bibliogramNormalRedirectsChecks ?? [...redirects.bibliogram.normal]; - bibliogramNormalCustomRedirects = result.bibliogramNormalCustomRedirects ?? []; + theme = r.theme ?? 'DEFAULT'; + applyThemeToSites = r.applyThemeToSites ?? false; - bibliogramTorRedirectsChecks = result.bibliogramTorRedirectsChecks ?? [...redirects.bibliogram.tor]; - bibliogramTorCustomRedirects = result.bibliogramTorCustomRedirects ?? []; + instancesCookies = r.instancesCookies ?? []; - protocol = result.instagramProtocol ?? "normal"; + bibliogramNormalRedirectsChecks = r.bibliogramNormalRedirectsChecks ?? [...redirects.bibliogram.normal]; + bibliogramNormalCustomRedirects = r.bibliogramNormalCustomRedirects ?? []; + + bibliogramTorRedirectsChecks = r.bibliogramTorRedirectsChecks ?? [...redirects.bibliogram.tor]; + bibliogramTorCustomRedirects = r.bibliogramTorCustomRedirects ?? []; + + protocol = r.instagramProtocol ?? "normal"; resolve(); } @@ -211,6 +264,9 @@ export default { getProtocol, setProtocol, + isBibliogram, + initBibliogramCookies, + getBibliogramNormalRedirectsChecks, setBibliogramNormalRedirectsChecks, getBibliogramTorRedirectsChecks, diff --git a/src/pages/background/background.js b/src/pages/background/background.js index 02e575c..e2f99b2 100644 --- a/src/pages/background/background.js +++ b/src/pages/background/background.js @@ -116,6 +116,7 @@ browser.tabs.onUpdated.addListener( catch (_) { return } if (youtubeHelper.isPipedorInvidious(url, 'main_frame', 'piped')) youtubeHelper.initPipedLocalStorage(tabId); if (twitterHelper.isNitter(url, 'main_frame')) newUrl = twitterHelper.initNitterCookies(url); + if (instagramHelper.isBibliogram(url)) instagramHelper.initBibliogramCookies(url); // if (changeInfo.url && youtubeHelper.isPipedorInvidious(url, 'main_frame', 'pipedMaterial')) youtubeHelper.initPipedMaterialLocalStorage(tabId); // if (changeInfo.url && youtubeHelper.isUrlPipedorInvidious(changeInfo.url, 'invidious')) youtubeHelper.initInvidiousCookies(tabId); }); \ No newline at end of file diff --git a/src/pages/options/general/general.js b/src/pages/options/general/general.js index 22c8814..4f10ab3 100644 --- a/src/pages/options/general/general.js +++ b/src/pages/options/general/general.js @@ -80,7 +80,6 @@ generalHelper.init().then(() => { alwaysUsePreferredElement.checked = generalHelper.getAlwaysUsePreferred(); themeElement.value = generalHelper.getTheme(); applyThemeToSitesElement.checked = generalHelper.getApplyThemeToSites(); - console.log("generalHelper.getAlwaysUsePreferred()"); instanceTypeElement.addEventListener("change", (event) => { instanceType = event.target.options[instanceTypeElement.selectedIndex].value