From 6750872ea5000a8eae1926cb2fb3f3cb3b2cc6aa Mon Sep 17 00:00:00 2001 From: "Ilya Ig. Petrov" Date: Thu, 1 Dec 2016 07:01:20 -0800 Subject: [PATCH] Refactor err handlers, unixify newlines, restructure files --- .../minimalistic-pac-setter/README.md | 56 ++++--- .../extension/0-error-handlers.js | 56 ------- .../extension/00-init-apis.js | 14 ++ .../extension/11-api-error-handlers.js | 148 ++++++++++++++++++ ...-api-sync-pac-script-with-pac-provider.js} | 40 ++--- .../extension/20-api-fixes.js | 23 +++ ...block-informer.js => 30-block-informer.js} | 6 +- ...3-context-menus.js => 40-context-menus.js} | 0 .../extension/manifest.json | 68 ++++---- .../pages/choose-pac-provider/index.html | 131 +++++++++------- .../pages/choose-pac-provider/index.js | 21 ++- 11 files changed, 357 insertions(+), 206 deletions(-) delete mode 100644 extensions/chromium/minimalistic-pac-setter/extension/0-error-handlers.js create mode 100644 extensions/chromium/minimalistic-pac-setter/extension/00-init-apis.js create mode 100644 extensions/chromium/minimalistic-pac-setter/extension/11-api-error-handlers.js rename extensions/chromium/minimalistic-pac-setter/extension/{1-sync-pac-script-with-pac-provider.js => 12-api-sync-pac-script-with-pac-provider.js} (94%) mode change 100755 => 100644 create mode 100644 extensions/chromium/minimalistic-pac-setter/extension/20-api-fixes.js rename extensions/chromium/minimalistic-pac-setter/extension/{2-block-informer.js => 30-block-informer.js} (96%) mode change 100755 => 100644 rename extensions/chromium/minimalistic-pac-setter/extension/{3-context-menus.js => 40-context-menus.js} (100%) mode change 100755 => 100644 diff --git a/extensions/chromium/minimalistic-pac-setter/README.md b/extensions/chromium/minimalistic-pac-setter/README.md index 0f2776b..888109a 100755 --- a/extensions/chromium/minimalistic-pac-setter/README.md +++ b/extensions/chromium/minimalistic-pac-setter/README.md @@ -1,26 +1,30 @@ -# О расширении - -Обход интернет-цензуры в России пока что не является преступлением. - -Расширение позволяет обходить блокировки РосКомНадзора, давая вам доступ -к библиотекам, энциклопедиям, сайтам оппозиционеров, а также к неповинным -сайтам, случайно заблокированным в силу разных причин. - -Проксирует только заблокированные сайты, оставляя нетронутыми все остальные. - -Устанавливает PAC-скрипт, работающий через сервера anticenz.org и antizapret.prostovpn.org. - -Обновляет PAC-скрипт каждые 4 часа, что составляет примерно 7MB трафика в сутки. -Также расширение постоянно потребляет ~15MB памяти для информирования о блокировках через иконку. - -Синяя лента – кампания фонда EFF в защиту свобод слова, прессы и союзов. - -Если расширение не работает: https://git.io/vgDDj - -Антицензура на Реддите: https://www.reddit.com/r/anticensorship_russia -Группа в G+: https://goo.gl/Lh0Cjh -История изменений: https://github.com/ilyaigpetrov/anti-censorship-russia/releases - -# Дополнительно - -Иконка синей ленты: http://www.iconsdb.com/icon-sets/cardboard-blue-icons/ribbon-12-icon.html +# Dev + +Linting JS: `npm run lint` + +# О расширении + +Обход интернет-цензуры в России пока что не является преступлением. + +Расширение позволяет обходить блокировки РосКомНадзора, давая вам доступ +к библиотекам, энциклопедиям, сайтам оппозиционеров, а также к неповинным +сайтам, случайно заблокированным в силу разных причин. + +Проксирует только заблокированные сайты, оставляя нетронутыми все остальные. + +Устанавливает PAC-скрипт, работающий через сервера anticenz.org и antizapret.prostovpn.org. + +Обновляет PAC-скрипт каждые 4 часа, что составляет примерно 7MB трафика в сутки. +Также расширение постоянно потребляет ~15MB памяти для информирования о блокировках через иконку. + +Синяя лента – кампания фонда EFF в защиту свобод слова, прессы и союзов. + +Если расширение не работает: https://git.io/vgDDj + +Антицензура на Реддите: https://www.reddit.com/r/anticensorship_russia +Группа в G+: https://goo.gl/Lh0Cjh +История изменений: https://github.com/ilyaigpetrov/anti-censorship-russia/releases + +# Дополнительно + +Иконка синей ленты: http://www.iconsdb.com/icon-sets/cardboard-blue-icons/ribbon-12-icon.html diff --git a/extensions/chromium/minimalistic-pac-setter/extension/0-error-handlers.js b/extensions/chromium/minimalistic-pac-setter/extension/0-error-handlers.js deleted file mode 100644 index 97edb16..0000000 --- a/extensions/chromium/minimalistic-pac-setter/extension/0-error-handlers.js +++ /dev/null @@ -1,56 +0,0 @@ -'use strict'; - -{ - const extName = chrome.runtime.getManifest().name; - const notify = ( - id, - title, - message, - icon = 'default-128.png', - context = extName - ) => chrome.notifications.create( - id, - { - title: title, - message: message, - contextMessage: context, - requireInteraction: true, - type: 'basic', - iconUrl: './icons/' + icon, - } - ); - - window.addEventListener('error', (err) => { - - console.warn('Global error'); - notify('Unhandled error', 'Unhandled error', JSON.stringify(err), - 'ext-error-128.png'); - - }); - - window.addEventListener('unhandledrejection', (event) => { - - console.warn('Unhandled rejection. Throwing error.'); - event.preventDefault(); - throw event.reason; - - }); - - chrome.proxy.onProxyError.addListener((details) => { - - console.warn('PAC ERROR:', details); - notify('pac-error', ' PAC !', JSON.stringify(details), - 'pac-error-128.png' ); - - }); - - chrome.proxy.settings.onChange.addListener((details) => { - - console.log('Proxy settings changed.', details); - // const ifOther = details.levelOfControl.startsWith('controlled_by_other'); - notify('Proxy change', 'Proxy changed', JSON.stringify(details), - 'no-control-128.png'); - - }); - -} diff --git a/extensions/chromium/minimalistic-pac-setter/extension/00-init-apis.js b/extensions/chromium/minimalistic-pac-setter/extension/00-init-apis.js new file mode 100644 index 0000000..3f9787c --- /dev/null +++ b/extensions/chromium/minimalistic-pac-setter/extension/00-init-apis.js @@ -0,0 +1,14 @@ +'use strict'; + +window.utils = { + + areSettingsNotControlledFor(details) { + + return ['controlled_by_other', 'not_controllable'] + .some( (prefix) => details.levelOfControl.startsWith(prefix) ); + + }, + +}; + +window.apis = {}; diff --git a/extensions/chromium/minimalistic-pac-setter/extension/11-api-error-handlers.js b/extensions/chromium/minimalistic-pac-setter/extension/11-api-error-handlers.js new file mode 100644 index 0000000..aa1e5ff --- /dev/null +++ b/extensions/chromium/minimalistic-pac-setter/extension/11-api-error-handlers.js @@ -0,0 +1,148 @@ +'use strict'; + +{ // Private namespace + + const handlersState = function(key, value) { + + console.log(key, value, '!'); + key = 'handlers-' + key; + if (value === null) { + return localStorage.removeItem(key); + } + if (value === undefined) { + const item = localStorage.getItem(key); + return item && JSON.parse(item); + } + if (value instanceof Date) { + throw new TypeError('Converting Date format to JSON is not supported.'); + } + localStorage.setItem(key, JSON.stringify(value)); + + }; + + const ifPrefix = 'if-on-'; + + window.apis.errorHandlers = { + + getEventsMap() { + + return new Map([ + ['pac-error', 'ошибки PAC скриптов'], + ['ext-error', 'ошибки расширения'], + ['no-control', 'утеря контроля над настройками'], + ]); + + }, + + switch(onOffStr, eventName) { + + if (!['on', 'off'].includes(onOffStr)) { + throw new TypeError('First argument bust be "on" or "off".'); + } + for( + const name of (eventName ? [eventName] : this.getEventsMap().keys() ) + ) { + handlersState( ifPrefix + name, onOffStr === 'on' ? 'on' : null ); + } + + }, + + isOn(eventName) { + + return handlersState( ifPrefix + eventName); + + }, + + ifNotControlled: null, + + isNotControlled(details) { + + this.ifNotControlled = window.utils.areSettingsNotControlledFor( details ); + if (this.ifNotControlled) { + chrome.browserAction.disable(); + } else { + chrome.browserAction.enable(); + } + return this.ifNotControlled; + + }, + + }; + +} + +// INIT +chrome.proxy.settings.get( + {}, + (details) => window.apis.errorHandlers.isNotControlled(details) +); + +{ + + const extName = chrome.runtime.getManifest().name; + + const mayNotify = function( + id, title, message, + icon = 'default-128.png', + context = extName + ) { + + if ( !window.apis.errorHandlers.isOn(id) ) { + return; + } + chrome.notifications.create( + id, + { + title: title, + message: message, + contextMessage: context, + requireInteraction: true, + type: 'basic', + iconUrl: './icons/' + icon, + isClickable: true, + } + ); + + }; + + window.addEventListener('error', (err) => { + + console.warn('GLOBAL ERROR', err); + mayNotify('ext-error', 'Unhandled error', JSON.stringify(err), + 'ext-error-128.png'); + + }); + + window.addEventListener('unhandledrejection', (event) => { + + console.warn('Unhandled rejection. Throwing error.'); + event.preventDefault(); + throw event.reason; + + }); + + chrome.proxy.onProxyError.addListener((details) => { + + if (window.apis.errorHandlers.ifNoControl) { + return; + } + console.warn('PAC ERROR', details); + mayNotify('pac-error', ' PAC !', JSON.stringify(details), + 'pac-error-128.png' ); + + }); + + chrome.proxy.settings.onChange.addListener((details) => { + + console.log('Proxy settings changed.', details); + const noCon = 'no-control'; + if ( window.apis.errorHandlers.isNotControlled(details) ) { + mayNotify(noCon, 'Proxy changed', JSON.stringify(details), + 'no-control-128.png'); + } else { + chrome.notifications.clear( noCon ); + } + + }); + +} diff --git a/extensions/chromium/minimalistic-pac-setter/extension/1-sync-pac-script-with-pac-provider.js b/extensions/chromium/minimalistic-pac-setter/extension/12-api-sync-pac-script-with-pac-provider.js old mode 100755 new mode 100644 similarity index 94% rename from extensions/chromium/minimalistic-pac-setter/extension/1-sync-pac-script-with-pac-provider.js rename to extensions/chromium/minimalistic-pac-setter/extension/12-api-sync-pac-script-with-pac-provider.js index 5b6f72d..fc7743a --- a/extensions/chromium/minimalistic-pac-setter/extension/1-sync-pac-script-with-pac-provider.js +++ b/extensions/chromium/minimalistic-pac-setter/extension/12-api-sync-pac-script-with-pac-provider.js @@ -9,39 +9,17 @@ */ /* - In background scripts use window.antiCensorRu public variables. - In pages window.antiCensorRu is not accessible, + In background scripts use window.apis.antiCensorRu public variables. + In pages window.apis.antiCensorRu is not accessible, use chrome.runtime.getBackgroundPage(..), extension.getBackgroundPage is deprecated */ { // Private namespace starts. - window.antiCensorRu = { + window.apis.antiCensorRu = { version: chrome.runtime.getManifest().version, - fixErrorsContext() { - /* `setTimeout` changes context of execution from other window - (e.g. popup) to background window, so we may catch errors - in bg error handlers. - More: https://bugs.chromium.org/p/chromium/issues/detail?id=357568 - */ - for(const prop of Object.keys(this)) { - if ( typeof(this[prop]) === 'function' ) { - const method = this[prop]; - this[prop] = function(...args) { - - setTimeout(method.bind(this, ...args), 0); - - }; - } - } - }, - - throw() { - throw new Error('Artificial error'); - }, - pacProviders: { Антизапрет: { pacUrl: 'https://antizapret.prostovpn.org/proxy.pac', @@ -293,6 +271,7 @@ } ) ); + }, }; @@ -307,8 +286,7 @@ E.g. install window may fail to open or be closed by user accidentally. In such case extension _should_ try to work on default parameters. */ - const antiCensorRu = window.antiCensorRu; - antiCensorRu.fixErrorsContext(); + const antiCensorRu = window.apis.antiCensorRu; chrome.alarms.onAlarm.addListener( (alarm) => { @@ -393,6 +371,7 @@ * Changed storage.ifNotInstalled to storage.ifFirstInstall * Added storage.lastPacUpdateStamp **/ + }); function asyncLogGroup(...args) { @@ -406,6 +385,7 @@ cb(...cbArgs); }; + } function checkChromeError(betterStack) { @@ -458,8 +438,7 @@ } chrome.proxy.settings.get({}, (details) => { - const ifThis = details.levelOfControl.startsWith('controlled_by_this'); - if (!ifThis) { + if ( window.utils.areSettingsNotControlledFor( details ) ) { console.warn('Failed, other extension is in control.'); return cb({clarification: {message: 'Настройки прокси контролирует другое расширение. Какое?'}}); } @@ -500,6 +479,7 @@ } ); + } function getIpsFor(host, cb) { @@ -600,6 +580,7 @@ } ) ); + } function setPacScriptFromProvider(provider, cb) { @@ -625,6 +606,7 @@ } ); + } } diff --git a/extensions/chromium/minimalistic-pac-setter/extension/20-api-fixes.js b/extensions/chromium/minimalistic-pac-setter/extension/20-api-fixes.js new file mode 100644 index 0000000..998915c --- /dev/null +++ b/extensions/chromium/minimalistic-pac-setter/extension/20-api-fixes.js @@ -0,0 +1,23 @@ +'use strict'; + +/* `setTimeout` changes context of execution from other window + (e.g. popup) to background window, so we may catch errors + in bg error handlers. + More: https://bugs.chromium.org/p/chromium/issues/detail?id=357568 +*/ +// Fix error context of methods of all APIs. +for(const api of Object.keys(window.apis)) { + for(const prop of Object.keys(api)) { + if ( typeof(api[prop]) !== 'function' ) { + continue; + } + const method = api[prop]; + api[prop] = function(...args) { + + setTimeout(method.bind(this, ...args), 0); + + }; + } +} + + diff --git a/extensions/chromium/minimalistic-pac-setter/extension/2-block-informer.js b/extensions/chromium/minimalistic-pac-setter/extension/30-block-informer.js old mode 100755 new mode 100644 similarity index 96% rename from extensions/chromium/minimalistic-pac-setter/extension/2-block-informer.js rename to extensions/chromium/minimalistic-pac-setter/extension/30-block-informer.js index 3522816..f952397 --- a/extensions/chromium/minimalistic-pac-setter/extension/2-block-informer.js +++ b/extensions/chromium/minimalistic-pac-setter/extension/30-block-informer.js @@ -14,6 +14,8 @@ Crazy parallel Chrome. **/ +const antiCensorRu = window.apis.antiCensorRu; + window.chrome.browserAction.setBadgeBackgroundColor({ color: '#db4b2f', }); @@ -66,7 +68,7 @@ window.tabWithError2ip = {}; // For errors only: Error? -> Check this IP! (title) => { const ifTitleSetAlready = /\n/.test(title); - const proxyHost = window.antiCensorRu.getPacProvider() + const proxyHost = antiCensorRu.getPacProvider() .proxyIps[requestDetails.ip]; const hostname = new URL( requestDetails.url ).hostname; @@ -141,7 +143,7 @@ window.tabWithError2ip = {}; // For errors only: Error? -> Check this IP! function isProxiedAndInformed(requestDetails) { if ( !(requestDetails.ip - && window.antiCensorRu.isProxied( requestDetails.ip )) ) { + && antiCensorRu.isProxied( requestDetails.ip )) ) { return false; } diff --git a/extensions/chromium/minimalistic-pac-setter/extension/3-context-menus.js b/extensions/chromium/minimalistic-pac-setter/extension/40-context-menus.js old mode 100755 new mode 100644 similarity index 100% rename from extensions/chromium/minimalistic-pac-setter/extension/3-context-menus.js rename to extensions/chromium/minimalistic-pac-setter/extension/40-context-menus.js diff --git a/extensions/chromium/minimalistic-pac-setter/extension/manifest.json b/extensions/chromium/minimalistic-pac-setter/extension/manifest.json index 35f6bab..295fdb6 100755 --- a/extensions/chromium/minimalistic-pac-setter/extension/manifest.json +++ b/extensions/chromium/minimalistic-pac-setter/extension/manifest.json @@ -1,34 +1,34 @@ -{ - "manifest_version": 2, - - "name": "Обход блокировок Рунета 0.15", - "description": "Аргументы против цензуры: https://git.io/vEkI9", - "version": "0.0.0.15", - "icons": { - "128": "/icons/default-128.png" - }, - "author": "ilyaigpetrov@gmail.com", - "homepage_url": "https://github.com/anticensorship-russia/chromium-extension", - - "permissions": [ - "proxy", - "webRequest", - "alarms", - "storage", - "", - "tabs", - "contextMenus", - "notifications" - ], - "background": { - "scripts": ["0-error-handlers.js", "1-sync-pac-script-with-pac-provider.js", "2-block-informer.js", "3-context-menus.js"] - }, - "browser_action": { - "default_title": "Этот сайт благословлён", - "default_popup": "/pages/choose-pac-provider/index.html" - }, - "options_ui": { - "page": "/pages/choose-pac-provider/index.html", - "chrome_style": true - } -} +{ + "manifest_version": 2, + + "name": "Обход блокировок Рунета 0.15", + "description": "Аргументы против цензуры: https://git.io/vEkI9", + "version": "0.0.0.15", + "icons": { + "128": "/icons/default-128.png" + }, + "author": "ilyaigpetrov@gmail.com", + "homepage_url": "https://github.com/anticensorship-russia/chromium-extension", + + "permissions": [ + "proxy", + "webRequest", + "alarms", + "storage", + "", + "tabs", + "contextMenus", + "notifications" + ], + "background": { + "scripts": ["00-init-apis.js", "11-api-error-handlers.js", "12-api-sync-pac-script-with-pac-provider.js", "20-api-fixes.js", "30-block-informer.js", "40-context-menus.js"] + }, + "browser_action": { + "default_title": "Этот сайт благословлён", + "default_popup": "/pages/choose-pac-provider/index.html" + }, + "options_ui": { + "page": "/pages/choose-pac-provider/index.html", + "chrome_style": true + } +} diff --git a/extensions/chromium/minimalistic-pac-setter/extension/pages/choose-pac-provider/index.html b/extensions/chromium/minimalistic-pac-setter/extension/pages/choose-pac-provider/index.html index ace9d0e..5251c58 100755 --- a/extensions/chromium/minimalistic-pac-setter/extension/pages/choose-pac-provider/index.html +++ b/extensions/chromium/minimalistic-pac-setter/extension/pages/choose-pac-provider/index.html @@ -1,58 +1,73 @@ - - - - Выбор провайдера PAC - - - -
    -
  • -
-
- Обновлялись: ... -
-
Загрузка...
- - - - - + + + + Выбор провайдера PAC + + + +
    +
  • +
+
+ Обновлялись: ... +
+
Загрузка...
+
+
+
Я ❤️ уведомления:
+
    +
+
+ + + + + diff --git a/extensions/chromium/minimalistic-pac-setter/extension/pages/choose-pac-provider/index.js b/extensions/chromium/minimalistic-pac-setter/extension/pages/choose-pac-provider/index.js index fe044ec..9405386 100755 --- a/extensions/chromium/minimalistic-pac-setter/extension/pages/choose-pac-provider/index.js +++ b/extensions/chromium/minimalistic-pac-setter/extension/pages/choose-pac-provider/index.js @@ -16,7 +16,7 @@ chrome.runtime.getBackgroundPage( (backgroundPage) => { }; - const antiCensorRu = backgroundPage.antiCensorRu; + const antiCensorRu = backgroundPage.apis.antiCensorRu; // SET DATE @@ -182,6 +182,25 @@ chrome://extensions › }; } + const conpanel = document.getElementById('list-of-handlers'); + backgroundPage.apis.errorHandlers.getEventsMap().forEach( (value, name) => { + + const li = document.createElement('li'); + li.innerHTML = ` + + `; + const box = li.querySelector('input'); + box.checked = backgroundPage.apis.errorHandlers.isOn(name); + box.onclick = function() { + + const id = this.id.replace('if-on-', ''); + backgroundPage.apis.errorHandlers.switch(this.checked ? 'on' : 'off', id); + + }; + conpanel.appendChild(li); + + }); + setStatusTo(''); if (antiCensorRu.ifFirstInstall) { const id = antiCensorRu.currentPacProviderKey || 'none';