From 05d02438c90503b60bf944c856dfc79b1351119c Mon Sep 17 00:00:00 2001 From: "Ilya Ig. Petrov" Date: Sat, 3 Dec 2016 10:01:06 -0800 Subject: [PATCH] Refactor (Async suffix, default cb), fix error page gradient --- .../extension/11-api-error-handlers.js | 31 +++- ...2-api-sync-pac-script-with-pac-provider.js | 169 +++++++++--------- .../extension/40-context-menus.js | 6 +- .../pages/choose-pac-provider/index.js | 26 ++- .../extension/pages/view-error/index.html | 43 +++-- .../extension/pages/view-error/index.js | 67 +++++-- 6 files changed, 204 insertions(+), 138 deletions(-) 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 index fdd9fcf..e1e2bf0 100644 --- a/extensions/chromium/minimalistic-pac-setter/extension/11-api-error-handlers.js +++ b/extensions/chromium/minimalistic-pac-setter/extension/11-api-error-handlers.js @@ -57,7 +57,7 @@ isNotControlled(details) { - this.ifNotControlled = window.utils.areSettingsNotControlledFor( details ); + this.ifNotControlled = window.utils.areSettingsNotControlledFor(details); if (this.ifNotControlled) { chrome.browserAction.disable(); } else { @@ -134,13 +134,22 @@ (details) => handlers.isNotControlled(details) ); + const openAndFocus = (url) => { + + chrome.tabs.create( + {active: true, url: url}, + (tab) => chrome.windows.update(tab.windowId, {focused: true}) + ); + + }; + chrome.notifications.onClicked.addListener( function(notId) { chrome.notifications.clear(notId); if(notId === 'no-control') { - return chrome.tabs.create({active: true, url: 'chrome://settings/#proxy'}); + return openAndFocus('chrome://settings/#proxy'); } - chrome.tabs.create({active: true, url: './pages/view-error/index.html#' + notId}); + openAndFocus('./pages/view-error/index.html#' + notId); }); @@ -151,9 +160,18 @@ if (handlers.ifNoControl) { return; } + /* + Example: + details: "line: 7: Uncaught Error: This is error, man.", + error: "net::ERR_PAC_SCRIPT_FAILED", + fatal: false, + */ console.warn('PAC ERROR', details); - handlers.mayNotify('pac-error', 'Ошибка PAC!', details, - 'pac-error-128.png' ); + // TOOD: add "view pac script at this line" button. + handlers.mayNotify('pac-error', 'Ошибка PAC!', + details.error + '\n' + details.details, + 'pac-error-128.png' + ); }); @@ -163,7 +181,8 @@ const noCon = 'no-control'; if ( handlers.isNotControlled(details) ) { console.log(details); - handlers.mayNotify(noCon, 'Прокси контролирует другое расширение', details, + handlers.mayNotify(noCon, 'Другое расширение контролирует прокси', + 'Какое?', 'no-control-128.png'); } else { chrome.notifications.clear( noCon ); diff --git a/extensions/chromium/minimalistic-pac-setter/extension/12-api-sync-pac-script-with-pac-provider.js b/extensions/chromium/minimalistic-pac-setter/extension/12-api-sync-pac-script-with-pac-provider.js index f914863..7a0b741 100644 --- a/extensions/chromium/minimalistic-pac-setter/extension/12-api-sync-pac-script-with-pac-provider.js +++ b/extensions/chromium/minimalistic-pac-setter/extension/12-api-sync-pac-script-with-pac-provider.js @@ -16,6 +16,71 @@ */ { // Private namespace starts. + function mandatory() { + + throw new TypeError('Missing required argument. ' + + 'Be explicit if you swallow errors.'); + + } + + function throwIfError(err) { + + if(err) { + throw err; + } + + } + + function asyncLogGroup(...args) { + + const cb = args.pop(); + if(!(cb && typeof(cb) === 'function')) { + throw new TypeError('cb must be a function, but got: ' + cb); + } + console.group(...args); + return function(...cbArgs) { + + console.groupEnd(); + console.log('Group finished.'); + cb(...cbArgs); + + }; + + } + + function checkChromeError(betterStack) { + + // Chrome API calls your cb in a context different from the point of API + // method invokation. + const err = chrome.runtime.lastError || chrome.extension.lastError || null; + if (err) { + const args = ['API returned error:', err]; + if (betterStack) { + args.push('\n' + betterStack); + } + console.warn(...args); + } + return err; + + } + + function chromified(cb = mandatory(), ...replaceArgs) { + + const stack = (new Error()).stack; + // Take error first callback and convert it to chrome api callback. + return function(...args) { + + if (replaceArgs.length) { + args = replaceArgs; + } + const err = checkChromeError(stack); + // setTimeout fixes error context. + setTimeout( cb.bind(null, err, ...args), 0 ); + + }; + + } + window.apis.antiCensorRu = { version: chrome.runtime.getManifest().version, @@ -99,7 +164,7 @@ _periodicUpdateAlarmReason: 'Периодичное обновление PAC-скрипта Антизапрет', - pushToStorageAsync(cb) { + pushToStorageAsync(cb = throwIfError) { console.log('Pushing to storage...'); @@ -123,29 +188,10 @@ }, - /* - pullFromStorage(cb) { + syncWithPacProviderAsync( + key = this.currentPacProvierKey, cb = throwIfError) { - chrome.storage.local.get(null, (storage) => { - - const err = checkChromeError(); - if (!err) { - console.log('Pulled from storage:', storage); - for(const key of Object.keys(storage)) { - this[key] = storage[key]; - } - } - - console.log('Synced with storage, any callback?', !!cb); - cb && cb(err, storage); - - }); - }, - */ - - syncWithPacProviderAsync(key, cb) { - - if( !key || typeof(key) === 'function' ) { + if( typeof(key) === 'function' ) { cb = key; key = this.currentPacProviderKey; } @@ -239,7 +285,7 @@ }, - installPacAsync(key, cb) { + installPacAsync(key, cb = throwIfError) { console.log('Installing PAC...'); if (!key) { @@ -253,7 +299,7 @@ }, - clearPacAsync(cb) { + clearPacAsync(cb = throwIfError) { cb = asyncLogGroup('Cearing alarms and PAC...', cb); chrome.alarms.clearAll( @@ -279,7 +325,11 @@ // ON EACH LAUNCH, STARTUP, RELOAD, UPDATE, ENABLE chrome.storage.local.get(null, (oldStorage) => { - checkChromeError(); + const err = checkChromeError(); + if (err) { + throw err; + } + /* Event handlers that ALWAYS work (even if installation is not done or failed). @@ -296,7 +346,7 @@ 'Periodic PAC update triggered:', new Date().toLocaleString('ru-RU') ); - antiCensorRu.syncWithPacProviderAsync(/* Swallows errors. */); + antiCensorRu.syncWithPacProviderAsync(() => {/* swallow */}); } } @@ -358,7 +408,7 @@ // UPDATE & MIGRATION console.log('Extension updated.'); if (!ifAlarmTriggered) { - antiCensorRu.pushToStorageAsync(/* Swallows errors. */); + antiCensorRu.pushToStorageAsync(); } /* @@ -374,55 +424,7 @@ }); - function asyncLogGroup(...args) { - - const cb = args.pop() || (() => {}); - console.group(...args); - return function(...cbArgs) { - - console.groupEnd(); - console.log('Group finished.'); - cb(...cbArgs); - - }; - - } - - function checkChromeError(betterStack) { - - // Chrome API calls your cb in a context different from the point of API - // method invokation. - const err = chrome.runtime.lastError || chrome.extension.lastError || null; - if (err) { - const args = ['API returned error:', err]; - if (betterStack) { - args.push('\n' + betterStack); - } - console.warn(...args); - } - return err; - - } - - function chromified(cb, ...replaceArgs) { - - const stack = (new Error()).stack; - // Take error first callback and convert it to chrome api callback. - return function(...args) { - - if (replaceArgs.length) { - args = replaceArgs; - } - const err = checkChromeError(stack); - if (cb) { - setTimeout( cb.bind(null, err, ...args), 0 ); - } - - }; - - } - - function setPac(pacData, cb) { + function setPac(pacData, cb = throwIfError) { const config = { mode: 'pac_script', @@ -452,15 +454,14 @@ } - function httpGet(url, cb) { + function httpGet(url, cb = mandatory()) { const start = Date.now(); fetch(url).then( (res) => { const textCb = - (err) => cb && res.text() - .then( (text) => cb(err, text), cb ); + (err) => res.text().then( (text) => cb(err, text), cb ); const status = res.status; if ( !( status >= 200 && status < 300 || status === 304 ) ) { res.clarification = { @@ -477,14 +478,14 @@ err.clarification = { message: 'Что-то не так с сетью, проверьте соединение.', }; - cb && cb(err); + cb(err); } ); } - function getIpsFor(host, cb) { + function getIpsFor(host, cb = mandatory()) { const types = [1, 28]; const promises = types.map( @@ -546,7 +547,7 @@ } - function updatePacProxyIps(provider, cb) { + function updatePacProxyIps(provider, cb = throwIfError) { cb = asyncLogGroup( 'Getting IP for '+ provider.proxyHosts.join(', ') + '...', @@ -585,7 +586,7 @@ } - function setPacScriptFromProvider(provider, cb) { + function setPacScriptFromProvider(provider, cb = throwIfError) { cb = asyncLogGroup( 'Getting pac script from provider...', provider.pacUrl, diff --git a/extensions/chromium/minimalistic-pac-setter/extension/40-context-menus.js b/extensions/chromium/minimalistic-pac-setter/extension/40-context-menus.js index d7e1fd2..222bce4 100644 --- a/extensions/chromium/minimalistic-pac-setter/extension/40-context-menus.js +++ b/extensions/chromium/minimalistic-pac-setter/extension/40-context-menus.js @@ -26,13 +26,13 @@ ); createMenuLinkEntry( - 'Открыть веб прокси (не наш)', - (tab) => 'https://kproxy.com' + 'Через Google Translate', + (tab) => 'https://translate.google.com/translate?hl=&sl=en&tl=ru&anno=2&sandbox=1&u=' + tab.url ); createMenuLinkEntry( 'Другие варианты разблокировки', - (tab) => 'https://rebrand.ly/unblock#' + tab.url + (tab) => 'https://rebrand.ly/ac-unblock#' + tab.url ); createMenuLinkEntry( 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 6a94245..ae93aba 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 @@ -89,16 +89,20 @@ chrome.runtime.getBackgroundPage( (backgroundPage) =>
${message} - [Ещё подробнее]` + + [Ещё подробнее] + ` ); getStatus().querySelector('.link-button').onclick = function() { const div = document.createElement('div'); div.innerHTML = ` - Более подробную информацию можно узнать из логов фоновой страницы:
- - chrome://extensions › - Это расширение › Отладка страниц: фоновая страница › Console (DevTools) + Более подробную информацию можно узнать из логов фоновой страницы: +
+ + chrome://extensions › Это расширение › Отладка страниц: фоновая + страница › Console (DevTools)
Ещё: ` + JSON.stringify({err: err, stack: err.stack}); getStatus().replaceChild(div, this); @@ -137,7 +141,8 @@ chrome.runtime.getBackgroundPage( (backgroundPage) => const _firstChild = ul.firstChild; for( const providerKey of Object.keys(antiCensorRu.pacProviders).sort() ) { const li = document.createElement('li'); - li.innerHTML = ` + li.innerHTML = ` + [обновить]`; @@ -159,7 +164,9 @@ chrome.runtime.getBackgroundPage( (backgroundPage) => for(const radio of radios) { radio.onclick = function(event) { - if (event.target.id === (antiCensorRu.currentPacProviderKey || 'none')) { + if ( + event.target.id === (antiCensorRu.currentPacProviderKey || 'none') + ) { return false; } const pacKey = event.target.id; @@ -194,7 +201,10 @@ chrome.runtime.getBackgroundPage( (backgroundPage) => box.onclick = function() { const id = this.id.replace('if-on-', ''); - backgroundPage.apis.errorHandlers.switch(this.checked ? 'on' : 'off', id); + backgroundPage.apis.errorHandlers.switch( + this.checked ? 'on' : 'off', + id + ); }; conpanel.appendChild(li); diff --git a/extensions/chromium/minimalistic-pac-setter/extension/pages/view-error/index.html b/extensions/chromium/minimalistic-pac-setter/extension/pages/view-error/index.html index 8344142..c75679e 100644 --- a/extensions/chromium/minimalistic-pac-setter/extension/pages/view-error/index.html +++ b/extensions/chromium/minimalistic-pac-setter/extension/pages/view-error/index.html @@ -7,15 +7,13 @@ margin: 0; padding: 0; width: 100%; - height: 100%; + min-height: 100%; } html { background: black url('./err.jpg') repeat; background-position: left; color: white; - } - body { - background: linear-gradient(to bottom, black 45em, transparent); + padding-bottom: 10em; } header { padding-top: 1em; @@ -52,21 +50,28 @@ -
-

Информация об ошибке

-
-
-
    -
  1. Тип: ext-error
  2. -
  3. -
  4. -
  5. - - - -
  6. -
-
+
+
+

Информация об ошибке

+
+
+
    +
  1. Тип: ext-error
  2. +
  3. +
  4. +
  5. +
  6. + + + +
  7. +
+
+
diff --git a/extensions/chromium/minimalistic-pac-setter/extension/pages/view-error/index.js b/extensions/chromium/minimalistic-pac-setter/extension/pages/view-error/index.js index c11b596..94ec6e1 100644 --- a/extensions/chromium/minimalistic-pac-setter/extension/pages/view-error/index.js +++ b/extensions/chromium/minimalistic-pac-setter/extension/pages/view-error/index.js @@ -1,13 +1,17 @@ 'use strict'; - +/* global Raven:false, hljs:false */ function errorJsonReplacer(key, value) { - if ( !['Error', 'ErrorEvent'].includes( value.constructor.name ) ) { + if (!( value && value.constructor + && ['Error', 'Event'].some( + (suff) => value.constructor.name.endsWith(suff) + ) + )) { return value; } const alt = {}; - Object.getOwnPropertyNames(value).forEach(function (key) { + Object.getOwnPropertyNames(value).forEach(function(key) { alt[key] = value[key]; }, value); @@ -34,34 +38,57 @@ function errorJsonReplacer(key, value) { } -chrome.runtime.getBackgroundPage( (backgroundPage) => - backgroundPage.apis.errorHandlers.installListenersOn(window, 'ErrView', () => { +chrome.runtime.getBackgroundPage( (bgPage) => + bgPage.apis.errorHandlers.installListenersOn(window, 'ErrView', () => { Raven.config('https://bc534321358f455b9ae861740c2d3af8@sentry.io/116007', { release: chrome.runtime.getManifest().version, autoBreadcrumbs: false, }).install(); - const errId = window.location.hash.slice(1); - const err = backgroundPage.apis.errorHandlers.idToErr[errId]; - const json = JSON.stringify(err, errorJsonReplacer, 2); - document.getElementById('output').innerHTML = hljs.highlight('json', json).value; + const originalComment = document.querySelector('#comment').value.trim(); + let json; + let err; + { + const output = document.getElementById('output'); + const errId = window.location.hash.slice(1); + const idToErr = bgPage.apis.errorHandlers.idToErr; + err = errId && idToErr[errId] || idToErr; + if (!Object.keys(err).length) { + output.innerHTML = 'Ошибок не найдено. ' + + 'Оставьте, пожалуйста, подробный комментарий.'; + } else { + json = JSON.stringify(err, errorJsonReplacer, 2); + output.innerHTML = hljs.highlight('json', json).value; + } + } document.addEventListener('ravenSuccess', () => alert('Готово')); - document.addEventListener('ravenFailure', () => alert('Ошибка sentry.io! (подробности недоступны)')); + document.addEventListener('ravenFailure', + () => alert('Ошибка sentry.io! (подробности недоступны)')); document.getElementById('raven-report').onclick = () => { const e = err.error || err; - const extra = JSON.parse(json); - const comment = document.getElementById('comment').value; - if (comment.trim()) { + let comment = document.getElementById('comment').value.trim(); + if (comment === originalComment) { + comment = ''; + } + if (!comment && !json) { + return alert('Посылать нечего, так как вы не оставили комментария.'); + } + const extra = json && JSON.parse(json) || {}; + if (comment) { extra.comment = comment; } Raven.captureException(e, { extra: extra, onSuccess: () => alert('Готово'), - onError: (err) => { throw err; } + onError: (err) => { + + throw err; + + }, }); }; @@ -70,8 +97,7 @@ chrome.runtime.getBackgroundPage( (backgroundPage) => const title = err.message || err; chrome.tabs.create({ - active: true, - url: 'https://rebrand.ly/ac-search-issues?q=' + encodeURIComponent(title) + url: 'https://rebrand.ly/ac-search-issues?q=' + encodeURIComponent(title), }); }; @@ -90,11 +116,16 @@ ${json} Версия: ${chrome.runtime.getManifest().version} `; chrome.tabs.create({ - active: true, - url: `https://rebrand.ly/ac-new-issue?title=${encodeURIComponent(title)}&body=${encodeURIComponent(body)}` + url: `https://rebrand.ly/ac-new-issue?title=${encodeURIComponent(title)}&body=${encodeURIComponent(body)}`, }); }; + document.querySelector('main').style.display = ''; + document.body.style.background = + 'linear-gradient(to bottom, black ' + + document.querySelector('textarea').offsetTop + + 'px, transparent)'; + }) );