From f20da20e4805fcfcf640f39791a1500a26674725 Mon Sep 17 00:00:00 2001 From: "Ilya Ig. Petrov" Date: Thu, 23 Feb 2017 06:39:54 +0000 Subject: [PATCH] Swap events for requests --- .../chromium/runet-censorship-bypass/grep.txt | 1 + .../src/extension-common/00-init-apis.js | 33 ++++++++++++++++--- .../extension-common/35-pac-kitchen-api.js | 7 ++-- ...7-sync-pac-script-with-pac-provider-api.js | 5 ++- .../src/extension-common/manifest.tmpl.json | 1 + .../pages/choose-pac-provider/index.js | 14 ++++++-- .../src/extension-full/20-ip-to-host-api.js | 17 ++++++---- .../src/templates-data.js | 2 ++ 8 files changed, 58 insertions(+), 22 deletions(-) create mode 100644 extensions/chromium/runet-censorship-bypass/grep.txt diff --git a/extensions/chromium/runet-censorship-bypass/grep.txt b/extensions/chromium/runet-censorship-bypass/grep.txt new file mode 100644 index 0000000..1b2534c --- /dev/null +++ b/extensions/chromium/runet-censorship-bypass/grep.txt @@ -0,0 +1 @@ +grep -r addEvent ./ --exclude-dir=vendor --exclude-dir=node_modules diff --git a/extensions/chromium/runet-censorship-bypass/src/extension-common/00-init-apis.js b/extensions/chromium/runet-censorship-bypass/src/extension-common/00-init-apis.js index f21f68a..1cb6214 100644 --- a/extensions/chromium/runet-censorship-bypass/src/extension-common/00-init-apis.js +++ b/extensions/chromium/runet-censorship-bypass/src/extension-common/00-init-apis.js @@ -2,7 +2,7 @@ { - const IF_DEBUG = false; + const IF_DEBUG = true; if (!IF_DEBUG) { // I believe logging objects precludes them from being GCed. @@ -18,6 +18,10 @@ }); } + const privates = { + requestToResponder: {}, + }; + const self = window.utils = { mandatory() { @@ -84,15 +88,34 @@ }, - addEventHandler(type, handler) { + assert(value) { - document.addEventListener(type, (event) => handler(...event.detail)); + if(!value) { + console.assert(value); + throw new Error('Assert failed for:' + value); + } }, - fireEvent(type, ...args) { + addRequestResponder(requestType, responder) { - document.dispatchEvent( new CustomEvent(type, {detail: args}) ); + if( privates.requestToResponder[requestType] ) { + throw new TypeError(`Request ${requestType} already has responder!`); + } + privates.requestToResponder[requestType] = responder; + + }, + + fireRequest(requestType, ...args) { + + const cb = args.slice(-1)[0]; + self.assert(typeof(cb) === 'function'); + const responder = privates.requestToResponder[requestType]; + if(responder) { + responder(...args); + } else { + cb(); + } }, diff --git a/extensions/chromium/runet-censorship-bypass/src/extension-common/35-pac-kitchen-api.js b/extensions/chromium/runet-censorship-bypass/src/extension-common/35-pac-kitchen-api.js index 2ee9081..a221a00 100644 --- a/extensions/chromium/runet-censorship-bypass/src/extension-common/35-pac-kitchen-api.js +++ b/extensions/chromium/runet-censorship-bypass/src/extension-common/35-pac-kitchen-api.js @@ -295,7 +295,6 @@ keepCookedNowAsync(pacMods = mandatory(), cb = throwIfError) { - console.log('Keep cooked now...', pacMods); if (typeof(pacMods) === 'function') { cb = pacMods; pacMods = getCurrentConfigs(); @@ -307,10 +306,11 @@ } kitchenState(modsKey, pacMods); } + console.log('Keep cooked now...', pacMods); this._tryNowAsync( (err, res, ...warns) => { - console.log('Try now cb...', err); + console.log('Try now err:', err); if (err) { return cb(err, res, ...warns); } @@ -321,8 +321,7 @@ } const hosts = par.map( (ps) => ps.split(/\s+/)[1] ); - window.utils.fireEvent('ip-to-host-replace-all', hosts, throwIfError); - cb(null, null, ...warns); + window.utils.fireRequest('ip-to-host-replace-all', hosts, (err, res, ...moreWarns) => cb( err, res, ...warns.concat(moreWarns) )); } ); diff --git a/extensions/chromium/runet-censorship-bypass/src/extension-common/37-sync-pac-script-with-pac-provider-api.js b/extensions/chromium/runet-censorship-bypass/src/extension-common/37-sync-pac-script-with-pac-provider-api.js index 559873b..b635575 100644 --- a/extensions/chromium/runet-censorship-bypass/src/extension-common/37-sync-pac-script-with-pac-provider-api.js +++ b/extensions/chromium/runet-censorship-bypass/src/extension-common/37-sync-pac-script-with-pac-provider-api.js @@ -93,8 +93,7 @@ 'Getting IPs for PAC hosts...', cb ); - window.utils.fireEvent('ip-to-host-update-all', () => {/* Swallow. */}); - cb(); + window.utils.fireRequest('ip-to-host-update-all', cb); }; @@ -261,7 +260,7 @@ }, - _periodicUpdateAlarmReason: 'Периодичное обновление PAC-скрипта Антизапрет', + _periodicUpdateAlarmReason: 'Периодичное обновление PAC-скрипта', pushToStorageAsync(cb = throwIfError) { diff --git a/extensions/chromium/runet-censorship-bypass/src/extension-common/manifest.tmpl.json b/extensions/chromium/runet-censorship-bypass/src/extension-common/manifest.tmpl.json index 4ce1ca0..9a515c8 100644 --- a/extensions/chromium/runet-censorship-bypass/src/extension-common/manifest.tmpl.json +++ b/extensions/chromium/runet-censorship-bypass/src/extension-common/manifest.tmpl.json @@ -23,6 +23,7 @@ ], "background": { + ${persistent} "scripts": [ "00-init-apis.js" , "11-error-handlers-api.js" diff --git a/extensions/chromium/runet-censorship-bypass/src/extension-common/pages/choose-pac-provider/index.js b/extensions/chromium/runet-censorship-bypass/src/extension-common/pages/choose-pac-provider/index.js index e057f6d..af91c64 100644 --- a/extensions/chromium/runet-censorship-bypass/src/extension-common/pages/choose-pac-provider/index.js +++ b/extensions/chromium/runet-censorship-bypass/src/extension-common/pages/choose-pac-provider/index.js @@ -599,9 +599,17 @@ HTTPS 11.22.33.44:8080;">${conf.value || localStorage.getItem(uiRaw) || ''} { + + pacKitchen.resetToDefaults(); + backgroundPage.utils.fireRequest('ip-to-host-reset-to-defaults', cb); + + }, + 'Откройте окно заново для отображения эффекта.', + () => window.close() + ); }; diff --git a/extensions/chromium/runet-censorship-bypass/src/extension-full/20-ip-to-host-api.js b/extensions/chromium/runet-censorship-bypass/src/extension-full/20-ip-to-host-api.js index 3c5df18..d8a4e49 100644 --- a/extensions/chromium/runet-censorship-bypass/src/extension-full/20-ip-to-host-api.js +++ b/extensions/chromium/runet-censorship-bypass/src/extension-full/20-ip-to-host-api.js @@ -188,7 +188,7 @@ const self = window.apis.ipToHost = { - persist() { + persistData() { console.log('Persisting ipToHost...', privates); const ipToHost = {}; @@ -299,7 +299,7 @@ this._updateAllAsync((err, ...args) => { if (!err) { - this.persist(); + this.persistData(); } cb(err, ...args); @@ -320,7 +320,7 @@ this._replaceAllAsync(hostArr, (allErr, ...args) => { if (!allErr) { - this.persist(); + this.persistData(); } cb(allErr, ...args); @@ -337,14 +337,17 @@ }; - window.utils.addEventHandler( + window.utils.addRequestResponder( 'ip-to-host-update-all', (...args) => self.updateAllAsync(...args) ); - window.utils.addEventHandler( + window.utils.addRequestResponder( 'ip-to-host-replace-all', (...args) => self.replaceAllAsync(...args) ); - window.utils.addEventHandler( - 'ip-to-host-reset-to-defaults', () => self.resetToDefaults() + window.utils.addRequestResponder( + 'ip-to-host-reset-to-defaults', (cb) => { + self.resetToDefaults(); + cb(); + } ); } diff --git a/extensions/chromium/runet-censorship-bypass/src/templates-data.js b/extensions/chromium/runet-censorship-bypass/src/templates-data.js index 3009b96..58f3d75 100644 --- a/extensions/chromium/runet-censorship-bypass/src/templates-data.js +++ b/extensions/chromium/runet-censorship-bypass/src/templates-data.js @@ -11,6 +11,7 @@ exports.contexts.full = Object.assign({}, commonContext, { nameSuffixEn: '', nameSuffixRu: '', extra_permissions: ', "webRequest"', + persistent: '', scripts_2x: ', "20-ip-to-host-api.js"', scripts_7x: ', "70-block-informer.js"', }); @@ -20,6 +21,7 @@ exports.contexts.mini = Object.assign({}, commonContext, { nameSuffixEn: ' MINI', nameSuffixRu: ' МИНИ', extra_permissions: '', + persistent: '"persistent": false,', scripts_2x: ', "20-for-mini-only.js"', scripts_7x: '', });