From 972d8f0faa4c1070167cde2269af6dd720fff206 Mon Sep 17 00:00:00 2001 From: Rodrigo O Date: Tue, 8 May 2018 18:59:04 -0300 Subject: [PATCH] =?UTF-8?q?Permitir=20a=20desativa=C3=A7=C3=A3o=20por=20si?= =?UTF-8?q?te=20na=20webextension=20(#100)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Inclui options.html para desativação por site (#92) * Alguns sites estavam fora da lista, deixa em ordem alfabética * Modifica background.js para respeitar prefrências * Content script respeita opções, autoreload background Recarrega os listeners do background script ao mudar opções. * Remove código para debugging * Padroniza nome de variável * Bugfix: content não funcionava com opções nula --- .eslintrc.yml | 3 + README.md | 34 +-- webext/background.js | 527 +++++++++++++++++++++++++--------------- webext/content-start.js | 84 ++++--- webext/content.js | 105 ++++---- webext/manifest.json | 5 + webext/options.html | 191 +++++++++++++++ webext/options.js | 76 ++++++ 8 files changed, 737 insertions(+), 288 deletions(-) create mode 100644 webext/options.html create mode 100644 webext/options.js diff --git a/.eslintrc.yml b/.eslintrc.yml index cc557db..fa30a51 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -17,3 +17,6 @@ rules: semi: - error - always + max-len: + - 1 + - code: 80 diff --git a/README.md b/README.md index 28f2d63..302e837 100644 --- a/README.md +++ b/README.md @@ -44,27 +44,29 @@ Se você tiver alguma dúvida ou ideia para burlar um site novo, abra uma issue O Burlesco funciona com os seguintes sites de notícia: -- Folha de S.Paulo -- O Globo -- O Estado de S. Paulo -- GaúchaZH - Correio Popular -- Gazeta do Povo - Diário Catarinense -- Jornal de Santa Catarina - Diário de Santa Maria -- Nexo -- JOTA -- O Estado do Maranhão -- Jornal NH -- UOL -- Veja - Exame -- Superinteressante -- Quatro Rodas -- The Economist -- Foreign Policy - Financial Times +- Folha de Londrina +- Folha de S.Paulo +- Foreign Policy +- Gazeta do Povo +- GaúchaZH +- Gramophone +- JOTA +- Jornal NH +- Jornal de Santa Catarina +- Nexo +- O Estado de S. Paulo +- O Estado do Maranhão +- O Globo +- Quatro Rodas +- Superinteressante +- The Economist - The New York Times - The Wall Street Journal - The Washington Post +- UOL +- Veja diff --git a/webext/background.js b/webext/background.js index 10c7c45..89cc8c4 100644 --- a/webext/background.js +++ b/webext/background.js @@ -1,226 +1,330 @@ -// Script blocking -chrome.webRequest.onBeforeRequest.addListener( - function() { - return {cancel: true}; - }, - { - urls: [ - // O Estado de S. Paulo - '*://*.estadao.com.br/paywall/*', +const WHITELIST = { + folhadespaulo: { + xhrBlocking: [ + 'http://paywall.folha.uol.com.br/status.php', + 'https://paywall.folha.uol.com.br/status.php' + ] + } +}; - // Folha de S.Paulo +const BLOCKLIST = { + correiopopular: { + scriptBlocking: [ + '*://correio.rac.com.br/includes/js/novo_cp/fivewall.js*', + ] + }, + diariocatarinense: { + xhrBlocking: [ + 'http://dc.clicrbs.com.br/jornal-2015/jsp/paywall.jspx*', + ] + }, + diariodesantamaria: { + scriptBlocking: [ + '*://www.rbsonline.com.br/cdn/scripts/SLoader.js', + ] + }, + exame: { + scriptBlocking: [ + '*://cdn.tinypass.com/api/tinypass.min.js', + ] + }, + folhadespaulo: { + scriptBlocking: [ '*://paywall.folha.uol.com.br/*', '*://static.folha.uol.com.br/paywall/*', - - // O Globo - '*://ogjs.infoglobo.com.br/*/js/controla-acesso-aux.js', - - // Gazeta do Povo + ], + xhrBlocking: [ + '*://paywall.folha.uol.com.br/*', + '*://static.folha.uol.com.br/paywall/*', + ] + }, + financialtimes: { + cookieBlocking: { + urlFilter: '*://*.ft.com/*', + blockAll: true + }, + headerInjection: { + name: 'Referer', + value: 'https://www.google.com.br/', + urlFilter: '*://www.ft.com/*' + } + }, + gazetadopovo: { + scriptBlocking: [ '*://www.netdeal.com.br/*', - - // Correio Popular - '*://correio.rac.com.br/includes/js/novo_cp/fivewall.js*', - - // O Estado do Maranhão - '*://dashboard.tinypass.com/xbuilder/experience/load*', - 'http://assets.imirante.com/2.0/oestadoma/js/jquery.login.min.js', - - // Jornal de Novo Hamburgo - '*://*.jornalnh.com.br/includes/js/paywall.js*', - '*://blockv2.fivewall.com.br/*', - - // Diário de Santa Maria - '*://www.rbsonline.com.br/cdn/scripts/SLoader.js', - - // New York Times - '*://*.nytimes.com/js/mtr.js', - '*://*.nyt.com/js/mtr.js', - - // Washington Post - '*://*.washingtonpost.com/*pwapi/*.js*', - '*://*.washingtonpost.com/*drawbridge/drawbridge.js?_*', - - // O Globo, Exame, Super Interessante, Veja, The Economist, Quatro Rodas - '*://cdn.tinypass.com/api/tinypass.min.js', - - // UOL - '*://tm.jsuol.com.br/modules/content-gate.js', - - // GaúchaZH + ] + }, + gauchazh: { + scriptBlocking: [ '*://gauchazh.clicrbs.com.br/static/main*', '*://www.rbsonline.com.br/cdn/scripts/special-paywall.min.js*', '*://www.rbsonline.com.br/cdn/scripts/paywall.min.js*', - - // Gramophone + ] + }, + gramophone: { + scriptBlocking: [ '*://api.tinypass.com/tpl/*', - - // Folha de Londrina - '*://www.folhadelondrina.com.br/*/fivewall.js*' - ], - types: ['script'] + ] }, - ['blocking'] -); - -var WHITELIST = [ - 'http://paywall.folha.uol.com.br/status.php', - 'https://paywall.folha.uol.com.br/status.php' -]; -chrome.webRequest.onBeforeRequest.addListener( - function(details) { - if (WHITELIST.indexOf(details.url) !== -1) - return {cancel: false}; - else - return {cancel: true}; - }, - { - urls: [ - // Folha de S.Paulo - '*://paywall.folha.uol.com.br/*', - '*://static.folha.uol.com.br/paywall/*', - - // Diário Catarinense - 'http://dc.clicrbs.com.br/jornal-2015/jsp/paywall.jspx*', - - // Jornal de Santa Catarina + jornaldesantacatarina: { + scriptBlocking: [ 'http://jornaldesantacatarina.clicrbs.com.br/jornal/jsp/paywall*' - ], - types: ['xmlhttprequest'] + ] }, - ['blocking'] -); - -// Cookie blocking -chrome.webRequest.onBeforeRequest.addListener( - function() { - chrome.cookies.remove({ - 'url': 'http://jota.info/*', - 'name': 'articles' - }); + jornalnh: { + scriptBlocking: [ + '*://*.jornalnh.com.br/includes/js/paywall.js*', + '*://blockv2.fivewall.com.br/*', + ] }, - { - urls: [ - // JOTA - 'http://jota.info/*' - ], - types: ['main_frame'] - } -); - -chrome.webRequest.onBeforeRequest.addListener( - function() { - removeCookies('https://www.ft.com'); + jota: { + cookieBlocking: { + urlFilter: 'http://jota.info/*', + cookie: { + url: 'http://jota.info/*', + name: 'articles' + } + } }, - { - urls: [ - // Financial Times - '*://*.ft.com/*' + nexo: { + cookieBlocking: { + urlFilter: '*://api.nexojornal.com.br/*', + blockAll: true + } + }, + oestadodespaulo: { + xhrBlocking: [ + '*://*.estadao.com.br/paywall/*', + ] + }, + oestadodomaranhao: { + xhrBlocking: [ + '*://dashboard.tinypass.com/xbuilder/experience/load*', + 'http://assets.imirante.com/2.0/oestadoma/js/jquery.login.min.js', + ] + }, + oglobo: { + scriptBlocking: [ + '*://ogjs.infoglobo.com.br/*/js/controla-acesso-aux.js', + ] + }, + quatrorodas: { + scriptBlocking: [ + '*://cdn.tinypass.com/api/tinypass.min.js', + ] + }, + superinteressante: { + scriptBlocking: [ + '*://cdn.tinypass.com/api/tinypass.min.js', + ] + }, + theeconomist: { + scriptBlocking: [ + '*://cdn.tinypass.com/api/tinypass.min.js', + ] + }, + thenewyorktimes: { + scriptBlocking: [ + '*://*.nytimes.com/js/mtr.js', + '*://*.nyt.com/js/mtr.js', + ] + }, + thewashingtonpost: { + scriptBlocking: [ + '*://*.washingtonpost.com/*pwapi/*.js*', + '*://*.washingtonpost.com/*drawbridge/drawbridge.js?_*', + ] + }, + thewallstreetjournal: { + cookieBlocking: { + urlFilter: '*://*.wsj.com/*', + blockAll: true + }, + headerInjection: { + name: 'Referer', + value: 'https://www.facebook.com/', + urlFilter: '*://*.wsj.com/*' + } + }, + uol: { + scriptBlocking: [ + '*://tm.jsuol.com.br/modules/content-gate.js', + ] + }, + veja: { + scriptBlocking: [ + '*://cdn.tinypass.com/api/tinypass.min.js', ] } -); +}; -chrome.webRequest.onBeforeRequest.addListener( - function() { - removeCookies('https://www.wsj.com'); - }, - { - urls: [ - // The Wall Street Journal - '*://*.wsj.com/*' - ] - } -); - -function removeCookies(url) { - chrome.cookies.getAll({}, function(cookies) { - cookies.forEach(function(cookie) { - chrome.cookies.remove({ - 'url': url, - 'name': cookie.name - }); - }); - }); +function onBeforeRequestScript() { + return {cancel: true}; } -chrome.webRequest.onHeadersReceived.addListener( - // Block cookies from being set - function (details) { - details.responseHeaders.forEach(function(responseHeader) { - if (responseHeader.name.toLowerCase() == 'set-cookie') { - responseHeader.value = ''; - } - }); - return { - responseHeaders: details.responseHeaders - }; - }, - { - urls: [ - // Financial Times - '*://*.ft.com/*', +function setScriptBlocking(enabledSites) { + let urlFilters = []; - // The Wall Street Journal - '*://*.wsj.com/' - ] - }, - ['blocking', 'responseHeaders'] -); + for (let item in BLOCKLIST) { + let script = BLOCKLIST[item].scriptBlocking; + if (enabledSites && enabledSites[item] == false) + continue; + if (script == undefined) + continue; + urlFilters = urlFilters.concat(script); + } -chrome.webRequest.onBeforeSendHeaders.addListener( - function(details) { - injectHeader('Cookie', '', details.requestHeaders); - return {requestHeaders: details.requestHeaders}; - }, - { - urls: [ - // Jornal Nexo - '*://api.nexojornal.com.br/*' - ], - types: ['xmlhttprequest'] - }, - ['blocking', 'requestHeaders'] -); + chrome.webRequest.onBeforeRequest.addListener( + onBeforeRequestScript, + { + urls: urlFilters, + types: ['script'] + }, + ['blocking'] + ); +} -// Referer injection -chrome.webRequest.onBeforeSendHeaders.addListener( - function(details) { +let whitelist = []; +function onBeforeRequestXml(details) { + if (whitelist.indexOf(details.url) !== -1) + return {cancel: false}; + else + return {cancel: true}; +} +function setXhrBlocking(enabledSites) { + let blocklist = []; + + for (let item in BLOCKLIST) { + let xhr = BLOCKLIST[item].xhrBlocking; + if (xhr == undefined) + continue; + if (enabledSites && enabledSites[item] == false) + continue; + blocklist = blocklist.concat(xhr); + } + + for (let item in WHITELIST) { + if (enabledSites && enabledSites[item] == false) + continue; + let xhr = BLOCKLIST[item].xhrBlocking; + if (xhr == undefined) + continue; + whitelist = whitelist.concat(xhr); + + } + + chrome.webRequest.onBeforeRequest.addListener( + onBeforeRequestXml, + { + urls: blocklist, + types: ['xmlhttprequest'] + }, + ['blocking'] + ); +} + +function onHeadersReceivedCookie(details) { + details.responseHeaders.forEach(function(responseHeader) { + if (responseHeader.name.toLowerCase() == 'set-cookie') { + responseHeader.value = ''; + } + }); + return { + responseHeaders: details.responseHeaders + }; +} +function onBeforeSendHeadersCookie(details) { + injectHeader('Cookie', '', details.requestHeaders); + return {requestHeaders: details.requestHeaders}; +} + +function makeCookieRemove(cookie) { + return function() { + chrome.cookies.remove(cookie); + }; +} +let callbacksOnBeforeRequestCookie = []; + + +function setCookieBlocking(enabledSites) { + let urlFilters = []; + + for (let item in BLOCKLIST) { + let cookie = BLOCKLIST[item].cookieBlocking; + if (cookie == undefined) + continue; + if (enabledSites && enabledSites[item] == false) + continue; + + if (cookie.blockAll) { + urlFilters.push(cookie.urlFilter); + } + else { + let callback = makeCookieRemove(cookie.cookie); + callbacksOnBeforeRequestCookie.push(callback); + chrome.webRequest.onBeforeRequest.addListener( + callback, + { + urls: [cookie.urlFilter], + types: ['xmlhttprequest', 'script', 'main_frame'] + } + ); + } + } + + + chrome.webRequest.onHeadersReceived.addListener( + onHeadersReceivedCookie, + { + urls: urlFilters, + types: ['xmlhttprequest', 'script', 'main_frame'] + }, + ['blocking', 'responseHeaders'] + ); + + chrome.webRequest.onBeforeSendHeaders.addListener( + onBeforeSendHeadersCookie, + { + urls: urlFilters, + types: ['xmlhttprequest', 'script', 'main_frame'] + }, + ['blocking', 'requestHeaders'] + ); +} +function makeInjectHeader(name, value) { + return function(details) { injectHeader( - 'Referer', - 'https://www.google.com.br/', + name, + value, details.requestHeaders ); - return {requestHeaders: details.requestHeaders}; - }, - { - urls: [ - // Financial Times - '*://www.ft.com/*' - ], - types: ['xmlhttprequest', 'main_frame'] - }, - ['blocking', 'requestHeaders'] -); + }; +} +let callbacksOnBeforeSendHeadersInjection = []; -chrome.webRequest.onBeforeSendHeaders.addListener( - function(details) { - injectHeader( - 'Referer', - 'https://www.facebook.com/', - details.requestHeaders +function setHeaderInjection(enabledSites) { + for (let item in BLOCKLIST) { + let header = BLOCKLIST[item].headerInjection; + if (header == undefined) + continue; + if (enabledSites && enabledSites[item] == false) + continue; + + let callback = makeInjectHeader(header.name, header.value); + callbacksOnBeforeSendHeadersInjection.push(callback); + chrome.webRequest.onBeforeSendHeaders.addListener( + callback, + { + urls: [ + header.urlFilter + ], + types: ['xmlhttprequest', 'main_frame'] + }, + ['blocking', 'requestHeaders'] ); - - return {requestHeaders: details.requestHeaders}; - }, - { - urls: [ - // The Wall Street Journal - '*://*.wsj.com/*' - ], - types: ['xmlhttprequest', 'main_frame'] - }, - ['blocking', 'requestHeaders'] -); + } +} function injectHeader(name, value, requestHeaders) { /** @@ -240,3 +344,34 @@ function injectHeader(name, value, requestHeaders) { else requestHeaders[headerIndex] = newHeader; } + +function apply() { + chrome.storage.local.get('sites', function(result) { + let enabledSites = result.sites; + + setScriptBlocking(enabledSites); + setXhrBlocking(enabledSites); + setCookieBlocking(enabledSites); + setHeaderInjection(enabledSites); + }); +} + +function removeListeners() { + chrome.webRequest.onBeforeRequest.removeListener(onBeforeRequestScript); + chrome.webRequest.onBeforeRequest.removeListener(onBeforeRequestXml); + chrome.webRequest.onHeadersReceived.removeListener(onHeadersReceivedCookie); + chrome.webRequest.onBeforeSendHeaders.removeListener( + onBeforeSendHeadersCookie); + for (let item of callbacksOnBeforeRequestCookie) + chrome.webRequest.onBeforeRequest.removeListener(item); + for (let item of callbacksOnBeforeSendHeadersInjection) + chrome.webRequest.onBeforeSendHeaders.removeListener(item); +} + +apply(); +chrome.runtime.onMessage.addListener(function(message) { + if (message == 'update') { + removeListeners(); + apply(); + } +}); diff --git a/webext/content-start.js b/webext/content-start.js index 7e5b559..8748f16 100644 --- a/webext/content-start.js +++ b/webext/content-start.js @@ -1,39 +1,55 @@ // run_at: document_start -var code = null; +chrome.storage.local.get('sites', function(result) { + for (let site in result.sites) { + let enabledSites = result.sites; + if (enabledSites[site] == false) + continue; + if (INJECTION[site] == undefined) + continue; -if (/gauchazh.clicrbs.com.br/.test(document.location.host)) - code = ` - function patchJs(jsurl) { - var xhttp = new XMLHttpRequest(); - xhttp.onreadystatechange = function() { - if (this.readyState == 4 && this.status == 200) { - var injectme = this.responseText; - injectme = injectme.replace('e.showLoginPaywall,','false,'); - injectme = injectme.replace('e.showPaywall,','false,'); - injectme = injectme.replace('e.requestCPF||!1,','false,'); - injectme = injectme.replace('!e.showLoginPaywall&&!e.showPaywall||!1','true'); - var script = document.createElement("script"); - script.type = "text/javascript"; - var textNode = document.createTextNode(injectme); - script.appendChild(textNode); - document.head.appendChild(script); - } - }; - xhttp.open("GET", jsurl, true); - xhttp.send(); + if (INJECTION[site].url.test(document.location.host)) { + var script = document.createElement('script'); + script.textContent = INJECTION[site].code; + (document.head||document.documentElement).appendChild(script); + script.parentNode.removeChild(script); + break; } + } +}); - document.addEventListener("DOMContentLoaded", function(event) { - var scripts = Array.from(document.getElementsByTagName('script')); - var script = scripts.find((el) => { return el.src.includes('static/main') }); - if (script) - patchJs(script.src); - }); - `; +const INJECTION = { + gauchazh: { + url : /gauchazh.clicrbs.com.br/, + code: ` + function patchJs(jsurl) { + var xhttp = new XMLHttpRequest(); + xhttp.onreadystatechange = function() { + if (this.readyState == 4 && this.status == 200) { + var injectme = this.responseText; + injectme = injectme.replace('e.showLoginPaywall,','false,'); + injectme = injectme.replace('e.showPaywall,','false,'); + injectme = injectme.replace('e.requestCPF||!1,','false,'); + injectme = injectme.replace( + '!e.showLoginPaywall&&!e.showPaywall||!1','true'); + var script = document.createElement("script"); + script.type = "text/javascript"; + var textNode = document.createTextNode(injectme); + script.appendChild(textNode); + document.head.appendChild(script); + } + }; + xhttp.open("GET", jsurl, true); + xhttp.send(); + } -if (code !== null) { - var script = document.createElement('script'); - script.textContent = code; - (document.head||document.documentElement).appendChild(script); - script.parentNode.removeChild(script); -} + document.addEventListener("DOMContentLoaded", function(event) { + var scripts = Array.from(document.getElementsByTagName('script')); + var script = scripts.find((el) => { + return el.src.includes('static/main') + }); + if (script) + patchJs(script.src); + }); + ` + } +}; diff --git a/webext/content.js b/webext/content.js index 63e82cd..5acfd6d 100644 --- a/webext/content.js +++ b/webext/content.js @@ -1,45 +1,66 @@ // run_at: document_idle -var code = null; -if (/oglobo\.globo\.com/.test(document.location.host)) - code = 'paywallAtivo = false;'; +const INJECTION = { + oglobo: { + url: /oglobo\.globo\.com/, + code: 'paywallAtivo = false;' + }, + theeconomist: { + url : /www\.economist\.com/, + code: 'document.cookie = "ec_limit=allow";' + }, + foreignpolicy: { + url: /foreignpolicy\.com/, + code: ` + document.getElementById("paywall_bg").remove(); + document.body.classList.remove("overlay-no-scroll"); + document.body.style.overflow = "visible"; + document.documentElement.classList.remove("overlay-no-scroll"); + ` + }, + folhadespaulo: { + url: /folha.uol.com.br/, + code: ` + omtrClickUOL = function(){};function showText() { + $("#bt-read-more-content").next().show(); + $("#bt-read-more-content").next().show().prev().remove(); + } + setTimeout(showText, 100); + ` + }, + financialtimes: { + url: /ft.com/, + code: ` + document.cookie = ""; + localStorage.clear(); + sessionStorage.clear(); + indexedDB.deleteDatabase("next-flags"); + indexedDB.deleteDatabase("next:ads");' + ` + }, + veja: { + url: /veja.abril.com.br/, + code: ` + document.querySelector('.content-blocked').classList + .remove('content-blocked'); + document.querySelector('.callpaywall').remove(); + ` + } +}; -else if (/www\.economist\.com/.test(document.location.host)) - code = 'document.cookie = "ec_limit=allow";'; +chrome.storage.local.get('sites', function(result) { + for (let site in INJECTION) { + let enabledSites = result.sites; + if (enabledSites && enabledSites[site] == false) + continue; + if (INJECTION[site] == undefined) + continue; -else if (/foreignpolicy\.com/.test(document.location.host)) { - code = '\ - document.getElementById("paywall_bg").remove();\ - document.body.classList.remove("overlay-no-scroll");\ - document.body.style.overflow = "visible";\ - document.documentElement.classList.remove("overlay-no-scroll");\ - '; -} - -else if (/folha.uol.com.br/.test(document.location.host)) { - code = 'omtrClickUOL = function(){};function showText() {\ - $("#bt-read-more-content").next().show();\ - $("#bt-read-more-content").next().show().prev().remove();\ - } \ - setTimeout(showText, 100);'; -} - -else if (/ft.com/.test(document.location.host)) { - code = 'document.cookie = "";\ - localStorage.clear();\ - sessionStorage.clear();\ - indexedDB.deleteDatabase("next-flags");\ - indexedDB.deleteDatabase("next:ads");'; -} - -else if (/veja.abril.com.br/.test(document.location.host)) - code = ` - document.querySelector('.content-blocked').classList.remove('content-blocked'); - document.querySelector('.callpaywall').remove(); - `; - -if (code !== null) { - var script = document.createElement('script'); - script.textContent = code; - (document.head||document.documentElement).appendChild(script); - script.parentNode.removeChild(script); -} + if (INJECTION[site].url.test(document.location.host)) { + var script = document.createElement('script'); + script.textContent = INJECTION[site].code; + (document.head||document.documentElement).appendChild(script); + script.parentNode.removeChild(script); + break; + } + } +}); diff --git a/webext/manifest.json b/webext/manifest.json index a38f598..ef16cba 100644 --- a/webext/manifest.json +++ b/webext/manifest.json @@ -40,6 +40,7 @@ "webRequest", "webRequestBlocking", "cookies", + "storage", "*://correio.rac.com.br/*", "*://dc.clicrbs.com.br/*", @@ -85,5 +86,9 @@ "gecko": { "id": "burlesco@burlesco.com" } + }, + + "options_ui": { + "page": "options.html" } } diff --git a/webext/options.html b/webext/options.html new file mode 100644 index 0000000..e66e234 --- /dev/null +++ b/webext/options.html @@ -0,0 +1,191 @@ + + + + Burlesco - Opções + + + + +

Ativar o Burlesco nos sites:

+
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+ +
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+ + +
+ + +
+
+ + +
+
+ + + diff --git a/webext/options.js b/webext/options.js new file mode 100644 index 0000000..88b4865 --- /dev/null +++ b/webext/options.js @@ -0,0 +1,76 @@ +const SITES = [ + 'correiopopular', + 'diariocatarinense', + 'diariodesantamaria', + 'exame', + 'financialtimes', + 'folhadespaulo', + 'foreignpolicy', + 'gazetadopovo', + 'gauchazh', + 'gramophone', + 'jornaldesantacatarina', + 'jornalnh', + 'jota', + 'nexo', + 'oestadodespaulo', + 'oestadodomaranhao', + 'oglobo', + 'quatrorodas', + 'superinteressante', + 'theeconomist', + 'thenewyorktimes', + 'thewallstreetjournal', + 'thewashingtonpost', + 'uol', + 'veja' +]; + +function saveOptions(e) { + function showUpdateSucess() { + document.querySelector('#save-success').style.display = 'inline-block'; + setTimeout(function() { + document.querySelector('#save-success').style.display = 'none'; + }, 3000); + } + + e.preventDefault(); + + let siteStatus = {}; + for (let site of SITES) + siteStatus[site] = document.querySelector('#' + site).checked; + + chrome.storage.local.set({sites: siteStatus}); + chrome.runtime.sendMessage('update'); + + showUpdateSucess(); +} + +function restoreOptions() { + function setCurrentSite(site, status) { + document.querySelector('#' + site).checked = status; + } + + chrome.storage.local.get('sites', function(result) { + for (let site in result.sites) + setCurrentSite(site, result.sites[site]); + }); +} + +function changeAll(check) { + let inputs = document.querySelectorAll('input[type="checkbox"]'); + for (let input of inputs) { + input.checked = check; + } +} +function checkNone() { + changeAll(false); +} +function checkAll() { + changeAll(true); +} + +document.addEventListener('DOMContentLoaded', restoreOptions); +document.querySelector('form').addEventListener('submit', saveOptions); +document.querySelector('#all').addEventListener('click', checkAll); +document.querySelector('#none').addEventListener('click', checkNone);