From e37fadb501079973f56d16993a15f64ee1af6953 Mon Sep 17 00:00:00 2001 From: magnolia1234 <7676006-magnolia1234@users.noreply.gitlab.com> Date: Thu, 13 Jan 2022 17:22:28 +0100 Subject: [PATCH] Update popup (domain to clear cookies) --- background.js | 9 +++++++-- changelog.txt | 1 + options/clearCookies.js | 16 +++++++++++++++- options/optin/opt-in.js | 4 ++-- options/options_custom.js | 2 +- options/popup.js | 12 +++++++++++- 6 files changed, 37 insertions(+), 7 deletions(-) diff --git a/background.js b/background.js index fe6e033..dde3901 100755 --- a/background.js +++ b/background.js @@ -214,8 +214,13 @@ function set_rules(sites, sites_updated, sites_custom) { if (rule.hasOwnProperty('block_regex')) { if (rule.block_regex instanceof RegExp) blockedRegexes[domain] = rule.block_regex; - else - blockedRegexes[domain] = new RegExp(rule.block_regex.replace('{domain}', domain.replace(/\./g, '\\.').replace(/(^\/|\/$)/g, ''))); + else { + try { + blockedRegexes[domain] = new RegExp(rule.block_regex.replace('{domain}', domain.replace(/\./g, '\\.').replace(/(^\/|\/$)/g, ''))); + } catch (e) { + false; + } + } } if (rule.useragent) { switch (rule.useragent) { diff --git a/changelog.txt b/changelog.txt index 1f03074..e4b8cdb 100644 --- a/changelog.txt +++ b/changelog.txt @@ -4,6 +4,7 @@ Changelog Bypass Paywalls Clean - Chrome Post-release Add Mainichi Shimbun (Japan) Fix Bloomberg (quotes) +Update popup (domain to clear cookies) * v2.5.1.0 (2022-01-09) Add group Hearst Communications (local USA) diff --git a/options/clearCookies.js b/options/clearCookies.js index 7b2bd00..a322fe0 100644 --- a/options/clearCookies.js +++ b/options/clearCookies.js @@ -3,12 +3,26 @@ var ext_api = (typeof browser === 'object') ? browser : chrome; window.localStorage.clear(); sessionStorage.clear(); -var cookie_domain = document.domain.replace(/^(www|amp(\d|html)?|m|wap)\./, ''); +var cookie_domain = getCookieDomain(document.domain); + // send domain to background.js (to clear cookies) ext_api.runtime.sendMessage({ domain: cookie_domain }); +function getCookieDomain(hostname) { + let domain = hostname; + let n = 0; + let parts = hostname.split('.'); + let str = '_gd' + (new Date()).getTime(); + while (n < (parts.length - 1) && document.cookie.indexOf(str + '=' + str) == -1) { + domain = parts.slice(-1 - (++n)).join('.'); + document.cookie = str + "=" + str + ";domain=" + domain + ";"; + } + document.cookie = str + "=;expires=Thu, 01 Jan 1970 00:00:01 GMT;domain=" + domain + ";"; + return domain; +} + var msg = "Cookies (and local storage) removed from " + cookie_domain; showMessage(msg, 2000); diff --git a/options/optin/opt-in.js b/options/optin/opt-in.js index c6e545c..d0b0ef0 100644 --- a/options/optin/opt-in.js +++ b/options/optin/opt-in.js @@ -28,7 +28,7 @@ window.addEventListener("load", function () { "optInShown": true, "customShown": true }); - open(location).close(); + window.close(); }); var custom_enabled = document.getElementById('custom-enabled'); @@ -94,4 +94,4 @@ window.addEventListener("load", function () { }); update_enabled.innerText = 'NO'; }); -}); \ No newline at end of file +}); diff --git a/options/options_custom.js b/options/options_custom.js index 354b060..1636338 100644 --- a/options/options_custom.js +++ b/options/options_custom.js @@ -117,7 +117,7 @@ function add_options() { } if (title && sites_custom[title]['domain']) { - sites_custom[title]['domain'] = sites_custom[title]['domain'].replace(/(http(s)?:\/\/(www\.)?|^www\.|\/$)/g, '').toLowerCase(); + sites_custom[title]['domain'] = sites_custom[title]['domain'].replace(/(http(s)?:\/\/|\/$)/g, '').replace(/^(www|amp(html)?|m|wap)(\d)?\./, '').toLowerCase(); // add new site to local storage ext_api.storage.local.get({ diff --git a/options/popup.js b/options/popup.js index 9a7d8a2..f91d4f0 100644 --- a/options/popup.js +++ b/options/popup.js @@ -41,7 +41,7 @@ document.getElementById("clear_cookies").addEventListener('click', function () { }, function (tabs) { if (tabs && tabs[0] && tabs[0].url && tabs[0].url.startsWith('http')) { let hostname = new URL(tabs[0].url).hostname; - let cookie_domain = hostname.replace(/^(www|amp(\d|html)?|m|wap)\./, ''); + let cookie_domain = getCookiePermDomain(hostname); ext_api.permissions.contains({ origins: ["*://*." + cookie_domain + "/*"] }, function (result) { @@ -69,4 +69,14 @@ function closeButton() { window.close(); } +function getCookiePermDomain(hostname) { + let domain = hostname.replace(/^(www|amp(html)?|m|wap)(\d)?\./, ''); + let domain_split = domain.split('.'); + let num = 2; + if (domain_split.length > 2 && domain.match(/(\w){2,4}\.(\w){2}$/)) + num = 3; + domain = domain_split.slice(-num).join('.'); + return domain; +} + document.getElementById("button-close").addEventListener('click', closeButton);