Add custom sites

Add your own custom sites (also for testing).
Check 'Custom'-link in popup-menu.
By default sites' cookies are removed after page loads (to bypass article limit).
Also you can enable Googlebot user-agent or disable Javascript for (sub)domain(s).
master
magnolia1234 2020-02-27 22:51:31 +01:00 committed by GitHub
parent 30263eba35
commit cb36164be0
6 changed files with 294 additions and 41 deletions

View File

@ -4,6 +4,7 @@
* [List of supported websites](#list-of-supported-websites)
* [Sites with limited number of free articles](#sites-with-limited-number-of-free-articles)
* [New site requests](#new-site-requests)
* [Add custom site](#add-custom-site)
* [Troubleshooting](#troubleshooting)
* [Changelog-releases](#changelog-releases)
* [Pull Requests](#pull-requests)
@ -271,6 +272,12 @@ You can submit a request for a new website [here](https://github.com/magnolia123
4. Disable javascript on the website by clicking the button right icon </> on the uBlock panel.
5. Refresh the page.
### Add custom site
Add your own custom site (also for testing).
Check 'Custom'-link in popup-menu.
By default sites' cookies are removed after page loads (to bypass article limit).
Also you can enable Googlebot user-agent or disable Javascript for (sub)domain(s).
### Troubleshooting
* This extension works best alongside [uBlock Origin](https://chrome.google.com/webstore/detail/ublock-origin/cjpalhdlnbpafiamejdnhcphjbkeiagm).
* If a site doesn't work, try turning off uBlock and refreshing. Also try reinstalling the extension.

View File

@ -89,7 +89,7 @@ const remove_cookies_select_drop = {
}
// Override User-Agent with Googlebot
const use_google_bot = [
const use_google_bot_default = [
'adelaidenow.com.au',
'barrons.com',
'cairnspost.com.au',
@ -120,7 +120,9 @@ const use_google_bot = [
'weeklytimesnow.com.au',
'worldpoliticsreview.com',
'wsj.com',
]
];
var use_google_bot_custom = [];
var use_google_bot = use_google_bot_default.concat(use_google_bot_custom);
// block paywall-scripts individually
var blockedRegexes = {
@ -182,35 +184,93 @@ function setDefaultOptions() {
}
// Get the enabled sites (from local storage) & add to allow/remove_cookies (if not already in one of these arrays)
// Add googlebot- and block_javascript-settings for custom sites
chrome.storage.sync.get({
sites: {}
}, function(items) {
var sites = items.sites;
enabledSites = Object.keys(items.sites).map(function(key) {
return items.sites[key];
});
enabledSites = enabledSites.filter(function(el) { return (el !== '###'); });
for (var domainIndex in enabledSites) {
var domainVar = enabledSites[domainIndex];
if (!allow_cookies.includes(domainVar) && !remove_cookies.includes(domainVar)) {
allow_cookies.push(domainVar);
remove_cookies.push(domainVar);
}
}
});
sites: {},
sites_custom: {}
}, function (items) {
var sites = items.sites;
var sites_custom = items.sites_custom;
// Listen for changes to options
chrome.storage.onChanged.addListener(function(changes, namespace) {
var key;
for (key in changes) {
var storageChange = changes[key];
if (key === 'sites') {
var sites = storageChange.newValue;
enabledSites = Object.keys(sites).map(function(key) {
return sites[key];
});
// custom googlebot
use_google_bot_custom = Object.keys(sites_custom).filter(function (key) {
return sites_custom[key]['googlebot'] > 0;
}).map(function (key) {
return sites_custom[key]['domain']
});
use_google_bot = use_google_bot_default.slice();
// custom block javascript (only (sub)domain)
block_js_custom = Object.keys(sites_custom).filter(function (key) {
return sites_custom[key]['block_javascript'] > 0;
}).map(function (key) {
return sites_custom[key]['domain']
});
block_js = block_js_default.slice();
enabledSites = Object.keys(items.sites).map(function (key) {
return items.sites[key];
});
enabledSites = enabledSites.filter(function (el) {
return (el !== '###');
});
console.log(enabledSites);
for (var domainIndex in enabledSites) {
var domainVar = enabledSites[domainIndex];
if (!allow_cookies.includes(domainVar) && !remove_cookies.includes(domainVar)) {
allow_cookies.push(domainVar);
remove_cookies.push(domainVar);
}
if (use_google_bot_custom.includes(domainVar)) {
use_google_bot.push(domainVar);
}
if (block_js_custom.includes(domainVar)) {
block_js.push("*://*." + domainVar + "/*"); // subdomains of site
block_js.push("*://" + domainVar + "/*"); // site without www.-prefix
}
}
});
// Listen for changes to options
chrome.storage.onChanged.addListener(function (changes, namespace) {
for (var key in changes) {
var storageChange = changes[key];
if (key === 'sites') {
var sites = storageChange.newValue;
enabledSites = Object.keys(sites).map(function (key) {
return sites[key];
});
}
if (key === 'sites_custom') {
var sites_custom = storageChange.newValue;
use_google_bot_custom = Object.keys(sites_custom).filter(function (key) {
return sites_custom[key]['googlebot'] > 0;
}).map(function (key) {
return sites_custom[key]['domain']
});
use_google_bot = use_google_bot_default.slice();
for (var domainIndex in use_google_bot_custom) {
var domainVar = use_google_bot_custom[domainIndex];
if (!use_google_bot.includes(domainVar)) {
use_google_bot.push(domainVar);
}
}
block_js_custom = Object.keys(sites_custom).filter(function (key) {
return sites_custom[key]['block_javascript'] > 0;
}).map(function (key) {
return sites_custom[key]['domain']
});
block_js = block_js_default.slice();
for (var domainIndex in block_js_custom) {
var domainVar = block_js_custom[domainIndex];
if (!block_js.includes(domainVar)) {
block_js.push("*://*." + domainVar + "/*"); // subdomains of site
block_js.push("*://" + domainVar + "/*"); // site without www.-prefix
}
}
}
}
}
});
// Set and show default options on install
@ -222,19 +282,22 @@ chrome.runtime.onInstalled.addListener(function (details) {
}
});
var block_js_default = ["*://*.tinypass.com/*", "*://*.poool.fr/*", "*://*.piano.io/*", "*://*.outbrain.com/*"];
var block_js_custom = [];
var block_js = block_js_default.concat(block_js_custom);
// Disable javascript for these sites/general paywall-scripts
chrome.webRequest.onBeforeRequest.addListener(function(details) {
if (!isSiteEnabled(details)) {
return;
}
return {cancel: true};
},
{
urls: ["*://*.tinypass.com/*", "*://*.poool.fr/*", "*://*.piano.io/*", "*://*.outbrain.com/*"],
chrome.webRequest.onBeforeRequest.addListener(function (details) {
if (!isSiteEnabled(details)) {
return;
}
return {
cancel: true
};
}, {
urls: block_js,
types: ["script"]
},
["blocking"]
);
},
["blocking"]);
chrome.webRequest.onBeforeSendHeaders.addListener(function(details) {
var requestHeaders = details.requestHeaders;

View File

@ -40,7 +40,7 @@ function save_options() {
// Restores checkbox input states using the preferences stored in chrome.storage.
function renderOptions() {
chrome.storage.sync.get({
sites: {}
sites: {}, sites_custom: {}
}, function(items) {
var sites = items.sites;
var sitesEl = document.getElementById('bypass_sites');
@ -62,6 +62,29 @@ function renderOptions() {
labelEl.appendChild(document.createTextNode(' '+key));
sitesEl.appendChild(labelEl);
}
// custom
var labelEl = document.createElement('label');
labelEl.appendChild(document.createTextNode(' ——— Custom Sites ———'));
sitesEl.appendChild(labelEl);
var sites_custom = items.sites_custom;
for (var key in sites_custom) {
if (defaultSites.hasOwnProperty(key)) {
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.checked = (key in sites) || (key.replace(/\s\(.*\)/, '') in sites);
if (value !=='###') {
labelEl.appendChild(inputEl);
}
labelEl.appendChild(document.createTextNode(' '+key));
sitesEl.appendChild(labelEl);
}
});
}

46
options_custom.html Normal file
View File

@ -0,0 +1,46 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Bypass Paywalls Clean Options Custom</title>
<style>
#bypass_sites label, #add_site label {
display: block;
}
body {
width:350px;
height:600px;
}
</style>
</head>
<body>
<h1>Options Custom</h1>
<div>
Add new site: enter unique title/domain (without www.)<br/>
& options for googlebot/block javascript (only on (sub)domain).<br/>
Enable new site in options (cookies removed by default).<br/>
Redefine options of custom site by adding entry with same title.
<br/><br/>
</div>
<div id='add_site'></div>
<br/>
<span style='float:left;padding-bottom:20px'>
<button id="add">Add</button>
</span>
<div style="clear:both;"></div>
<div>
Current custom sites (edit textarea & save).<br/>
Saves only when valid json-text; clear & save to reset.<br/>
Also possible to export/import json-text for new installation.
</div>
<br/>
<div id='bypass_sites'></div>
<br/>
<div id="status"></div>
<div id="error"></div>
<span style='float:left;padding-bottom:20px'>
<button id="save">Save</button>
</span>
<script src="options_custom.js"></script>
</body>
</html>

114
options_custom.js Normal file
View File

@ -0,0 +1,114 @@
// Saves options to chrome.storage
function save_options() {
var gh_url = document.getElementById('bypass_sites').value;
var textareaEl = document.querySelector('#bypass_sites textarea');
var sites_custom = {};
if (textareaEl.value !== '')
var sites_custom = JSON.parse(textareaEl.value);
chrome.storage.sync.set({
sites_custom: sites_custom
}, function () {
// Update status to let user know options were saved.
var status = document.getElementById('status');
status.textContent = 'Options saved.';
setTimeout(function () {
status.textContent = '';
location.href = 'options.html';
//window.close();
}, 800);
});
}
// Add custom site to chrome.storage
function add_options() {
var gh_url = document.getElementById('add_site').value;
var inputEls = document.querySelectorAll('#add_site input');
var sites_custom = {};
for (let i = 0; i < inputEls.length; i++) {
if (inputEls[i].dataset.key === 'title') {
var title = inputEls[i].value;
if (title === '')
break;
sites_custom[title] = {};
} else {
if (inputEls[i].dataset.value) {
if (inputEls[i].checked)
sites_custom[title][inputEls[i].dataset.key] = inputEls[i].dataset.value;
} else
sites_custom[title][inputEls[i].dataset.key] = inputEls[i].value;
}
}
if (sites_custom[title]['domain'] === '')
sites_custom = {};
else
sites_custom[title]['domain'] = sites_custom[title]['domain'].replace('www.', '');
// add new site to local storage
chrome.storage.sync.get({
sites_custom: {}
}, function (items) {
var sites_custom_old = items.sites_custom;
for (var key in sites_custom) {
sites_custom_old[key] = sites_custom[key];
}
chrome.storage.sync.set({
sites_custom: sites_custom_old
}, function () {
// Update status to let user know options were saved.
var status = document.getElementById('status');
status.textContent = 'Site added.';
setTimeout(function () {
status.textContent = '';
location.href = 'options.html';
//window.close();
}, 800);
});
});
}
// Restores checkbox input states using the preferences stored in chrome.storage.
function renderOptions() {
chrome.storage.sync.get({
sites_custom: {}
}, function (items) {
var sites_custom = items.sites_custom;
var sitesEl = document.getElementById('bypass_sites');
var labelEl = document.createElement('label');
var textareaEl = document.createElement('textarea');
textareaEl.value = JSON.stringify(sites_custom);
textareaEl.rows = 12;
textareaEl.cols = 45;
labelEl.appendChild(textareaEl);
sitesEl.appendChild(labelEl);
// add site
var add_sitesEl = document.getElementById('add_site');
var inputEl;
var add_checkboxes = {
'title': 0,
'domain': 0,
'googlebot': 1,
'block_javascript': 1
};
for (var key in add_checkboxes) {
labelEl = document.createElement('label');
inputEl = document.createElement('input');
inputEl.dataset.key = key;
labelEl.appendChild(inputEl);
if (add_checkboxes[key]) {
inputEl.type = 'checkbox';
inputEl.dataset.value = 1;
}
labelEl.appendChild(document.createTextNode(' ' + key));
add_sitesEl.appendChild(labelEl);
}
});
}
document.addEventListener('DOMContentLoaded', renderOptions);
document.getElementById('save').addEventListener('click', save_options);
document.getElementById('add').addEventListener('click', add_options);

View File

@ -5,7 +5,7 @@
</head>
<body>
<div style="width:225px;"><strong>Bypass Paywalls Clean <span id="version"></span></strong>
<br><a href="options.html">Options</a> | <a href="https://github.com/magnolia1234/bypass-paywalls-chrome-clean/blob/master/README.md" target=”_blank”>GitHub</a> | <strong><span id="version_new"></span></strong></div>
<br><a href="options.html">Options</a> | <a href="options_custom.html">Custom</a> | <a href="https://github.com/magnolia1234/bypass-paywalls-chrome-clean/blob/master/README.md" target=”_blank”>GitHub</a> | <strong><span id="version_new"></span></strong></div>
<script src="version.js"></script>
</body>
</html>