Add on/off-switch (popup)

Also hide double (custom) domain in options.
merge-requests/1/head
magnolia1234 4 years ago
parent 6c6bbcfc37
commit 21d466af9e

@ -13,7 +13,8 @@ const restrictions = {
'elcomercio.pe': /.+\/elcomercio.pe\/.+((\w)+(\-)+){3,}.+/,
'gestion.pe': /.+\/gestion.pe\/.+((\w)+(\-)+){3,}.+/,
'quora.com': /^((?!quora\.com\/search\?q=).)*$/,
'seekingalpha.com': /.+seekingalpha\.com\/article\/.+/
'seekingalpha.com': /.+seekingalpha\.com\/article\/.+/,
'wsj.com': /^((?!\/cn\.wsj\.com\/).)*$/
}
// Don't remove cookies before page load
@ -210,6 +211,10 @@ const userAgentDesktop = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.go
const userAgentMobile = "Chrome/41.0.2272.96 Mobile Safari/537.36 (compatible ; Googlebot/2.1 ; +http://www.google.com/bot.html)"
var enabledSites = [];
var disabledSites = [];
var defaultSites_domains = Object.values(defaultSites);
var customSites = {};
var customSites_domains = [];
function setDefaultOptions() {
ext_api.storage.sync.set({
@ -246,6 +251,9 @@ ext_api.storage.sync.get({
}).map(function (key) {
return sites[key].toLowerCase();
});
customSites = sites_custom;
customSites_domains = Object.values(sites_custom).map(x => x.domain);
disabledSites = defaultSites_domains.concat(customSites_domains).filter(x => !enabledSites.includes(x) && x !== '###');
if (enabledSites.includes('ad.nl'))
enabledSites = enabledSites.concat(ad_region_domains);
if (enabledSites.includes('###_au_comm_media')) {
@ -290,6 +298,7 @@ ext_api.storage.onChanged.addListener(function (changes, namespace) {
}).map(function (key) {
return sites[key];
});
disabledSites = defaultSites_domains.concat(customSites_domains).filter(x => !enabledSites.includes(x) && x !== '###');
if (enabledSites.includes('ad.nl'))
enabledSites = enabledSites.concat(ad_region_domains);
if (enabledSites.includes('###_au_comm_media'))
@ -305,6 +314,8 @@ ext_api.storage.onChanged.addListener(function (changes, namespace) {
if (key === 'sites_custom') {
var sites_custom = storageChange.newValue;
var sites_custom_old = storageChange.oldValue;
customSites = sites_custom;
customSites_domains = Object.values(sites_custom).map(x => x.domain);
// add/remove custom sites in options
var sites_custom_added = Object.keys(sites_custom).filter(x => !Object.keys(sites_custom_old).includes(x) && !defaultSites.hasOwnProperty(x));
@ -345,19 +356,18 @@ ext_api.storage.onChanged.addListener(function (changes, namespace) {
// reset disableJavascriptOnListedSites eventListener
ext_api.webRequest.onBeforeRequest.removeListener(disableJavascriptOnListedSites);
ext_api.webRequest.handlerBehaviorChanged();
// Refresh the current tab
ext_api.tabs.query({
active: true,
currentWindow: true
}, function (tabs) {
if (tabs.length > 0 && tabs[0].url && tabs[0].url.indexOf("http") !== -1) {
ext_api.tabs.update(tabs[0].id, {
url: tabs[0].url
});
}
});
}
// Refresh the current tab
ext_api.tabs.query({
active: true,
currentWindow: true
}, function (tabs) {
if (tabs.length > 0 && tabs[0].url && tabs[0].url.indexOf("http") !== -1) {
ext_api.tabs.update(tabs[0].id, {
url: tabs[0].url
});
}
});
}
});
@ -570,13 +580,64 @@ ext_api.tabs.onActivated.addListener(function (activeInfo) { ext_api.tabs.get(ac
function updateBadge (activeTab) {
if (!activeTab) { return; }
const badgeText = getTextB(activeTab.url);
ext_api.browserAction.setBadgeBackgroundColor({color: 'red'});
let badgeText = '';
let color = 'red';
let currentUrl = activeTab.url;
if (currentUrl) {
let isDefaultSite = matchUrlDomain(defaultSites_domains, currentUrl);
if (isSiteEnabled({url: currentUrl})) {
badgeText = 'ON';
color = 'red';
} else if (matchUrlDomain(enabledSites, currentUrl)) {
badgeText = 'ON-';
color = 'orange';
} else if (matchUrlDomain(disabledSites, currentUrl)) {
badgeText = 'OFF';
color = 'blue';
}
}
ext_api.browserAction.setBadgeBackgroundColor({color: color});
ext_api.browserAction.setBadgeText({text: badgeText});
}
function getTextB(currentUrl) {
return currentUrl && isSiteEnabled({url: currentUrl}) ? 'ON' : '';
function site_switch() {
ext_api.tabs.query({
active: true,
currentWindow: true
}, function (tabs) {
if (tabs.length > 0 && tabs[0].url && tabs[0].url.indexOf("http") !== -1) {
let currentUrl = tabs[0].url;
let isDefaultSite = matchUrlDomain(defaultSites_domains, currentUrl);
let defaultSite_title = isDefaultSite ? Object.keys(defaultSites).find(key => defaultSites[key] === isDefaultSite) : '';
let isCustomSite = matchUrlDomain(Object.values(customSites_domains), currentUrl);
let customSite_title = isCustomSite ? Object.keys(customSites).find(key => customSites[key].domain === isCustomSite) : '';
let site_title = defaultSite_title || customSite_title;
let domain = isDefaultSite || isCustomSite;
if (domain) {
let added_site = [];
let removed_site = [];
if (enabledSites.includes(domain))
removed_site.push(site_title);
else
added_site.push(site_title);
chrome.storage.sync.get({
sites: {}
}, function (items) {
var sites = items.sites;
for (var key of added_site)
sites[key] = domain;
for (var key of removed_site)
delete sites[key];
chrome.storage.sync.set({
sites: sites
}, function () {
true;
});
});
}
}
});
}
// remove cookies after page load

@ -1,5 +1,6 @@
// defaultSites are loaded from sites.js at installation extension (and are saved to local storage)
// var defaultSites = {};
var ext_api = chrome || browser;
// Saves options to ext_api.storage
function save_options() {
@ -71,17 +72,18 @@ function renderOptions() {
labelEl.appendChild(document.createTextNode(' ——— Custom Sites ———'));
sitesEl.appendChild(labelEl);
var sites_custom = items.sites_custom;
var defaultSites_domains = ext_api.extension.getBackgroundPage().defaultSites_domains;
for (var key in sites_custom) {
if (defaultSites.hasOwnProperty(key)) {
var domain = sites_custom[key]['domain'];
if (defaultSites.hasOwnProperty(key) || defaultSites_domains.includes(domain)) {
continue;
}
var value = sites_custom[key]['domain'];
var labelEl = document.createElement('label');
var inputEl = document.createElement('input');
inputEl.type = 'checkbox';
inputEl.dataset.key = key;
inputEl.dataset.value = value;
inputEl.dataset.value = domain;
clean_key = key.replace(/\s\(.*\)/, '');
inputEl.checked = Object.keys(sites).some(title => (title.replace(/\s\(.*\)/, '') === clean_key));
if (value !== '' && value !== '###') {

@ -23,18 +23,19 @@
<div id='add_site'></div>
<br/>
<div id="status_add"></div>
<span style='float:left;padding-bottom:20px'>
<span style='float:left;padding-bottom:5px'>
<button id="add">Add</button>
</span>
<div style="clear:both;"></div>
<div>
<h3>List of custom sites</h3>
* already in default list (double domain)
<br/>
</div>
<div id='custom_sites'></div>
<br/>
<div id="status_delete"></div>
<span style='float:left;padding-bottom:20px'>
<span style='float:left;padding-bottom:5px'>
<button id="delete">Delete</button>
<button id="edit">Edit (re-Add)</button>
</span>

@ -233,7 +233,10 @@ function renderOptions() {
var optionEl;
for (var key in sites_custom) {
optionEl = document.createElement('option');
optionEl.text = key + ': ' + sites_custom[key]['domain'] +
let domain = sites_custom[key]['domain'];
let isDefaultSite = ext_api.extension.getBackgroundPage().defaultSites_domains.includes(domain);
optionEl.text = isDefaultSite ? '*' : '';
optionEl.text += key + ': ' + domain +
(sites_custom[key]['googlebot']>0 ? ' | googlebot' : '') +
(sites_custom[key]['block_javascript']>0 ? ' | block javascript' : '') +
(sites_custom[key]['block_javascript_ext']>0 ? ' | block javascript ext' : '');

@ -4,11 +4,13 @@
<meta charset="utf-8">
</head>
<body>
<div style="width:275px"><strong>Bypass Paywalls Clean <span id="version"></span></strong>
<div style="width:100%"><strong>Bypass Paywalls Clean <span id="version"></span></strong>
<br><a href="options.html" style="color:black">Options</a> |
<a href="options_custom.html" style="color:black">Custom</a> |
<a href="https://bitbucket.org/magnolia1234/bypass-paywalls-firefox-clean/src/master/README.md" style="color:black" target=”_blank”>BitBucket</a>
<a href="https://bitbucket.org/magnolia1234/bypass-paywalls-firefox-clean/src/master/README.md" style="color:black" target=”_blank”>BitBucket</a> |
<button id="site_switch">on/off</button>
<br><span id="version_new"></span></div>
<script src="version.js"></script>
<script src="popup.js"></script>
</body>
</html>

@ -0,0 +1,4 @@
var ext_api = chrome || browser;
document.getElementById("site_switch").addEventListener('click', function() {
ext_api.extension.getBackgroundPage().site_switch();
});
Loading…
Cancel
Save