From 9dd4724167cfded3ba9f7c1a07a170f8ff58f5bf Mon Sep 17 00:00:00 2001 From: "Ilya Ig. Petrov" Date: Mon, 8 Feb 2016 20:43:23 +0500 Subject: [PATCH] Now icon may have a badge. SetTitle semaphor added. Removed unused files. --- .../extnesion/block-informer - Copy.js | 93 ---------- .../extnesion/block-informer.js | 164 ++++++++++++++---- .../extnesion/icons/ribbon-partly32.png | Bin 1527 -> 0 bytes .../extnesion/manifest.json | 2 +- .../sync-pac-script-with-pac-provider.js | 2 +- 5 files changed, 128 insertions(+), 133 deletions(-) delete mode 100755 extensions/chromium/minimalistic-pac-setter/extnesion/block-informer - Copy.js delete mode 100755 extensions/chromium/minimalistic-pac-setter/extnesion/icons/ribbon-partly32.png diff --git a/extensions/chromium/minimalistic-pac-setter/extnesion/block-informer - Copy.js b/extensions/chromium/minimalistic-pac-setter/extnesion/block-informer - Copy.js deleted file mode 100755 index ae7dd00..0000000 --- a/extensions/chromium/minimalistic-pac-setter/extnesion/block-informer - Copy.js +++ /dev/null @@ -1,93 +0,0 @@ -'use strict'; - -// Shows user PageAction icon if any part of the current site is being blocked and proxied. - -function getHostname(url) { - var a = document.createElement('a'); - a.href = url; - return a.hostname; -} - -function blockInform(details) { - if (details.tabId !== -1 && window.antiCensorRu.pacProvider && window.antiCensorRu.pacProvider.proxyIps && window.antiCensorRu.pacProvider.proxyIps[ details.ip ]) { - - console.log(details.url, details.type, details); - var ifMainFrame = details.type === 'main_frame'; - var doesSetIconEraseTitlePromise = new Promise( - (resolve, reject) => { - if (false && ifMainFrame) - chrome.pageAction.setIcon({ - path: '/icons/ribbon32.png', - tabId: details.tabId - }, - () => { - chrome.pageAction.show(details.tabId); - resolve(); - } - ); - else resolve(); - } - ); - doesSetIconEraseTitlePromise - .then( - res => chrome.pageAction.getTitle( - { tabId: details.tabId }, - title => { - - var ifTitleSetAlready = /\n/.test(title); // Initially title equals extension name. - var proxyHost = window.antiCensorRu.pacProvider.proxyIps[ details.ip ]; - var hostname = getHostname(details.url).trim(); - var ifShouldUpdateTitle = !ifTitleSetAlready; - var indent = ' '; - - if (!ifTitleSetAlready) { - console.log('Main frame?', ifMainFrame); - console.log(ifTitleSetAlready, title); - console.log('Setting for', details.url, details.type); - if (false && !ifMainFrame) - chrome.pageAction.setIcon({ - path: '/icons/ribbon-partly32.png', - tabId: details.tabId - }); - title = 'Разблокированы:\n'+ indent + hostname +'\nПрокси:\n'+ indent + proxyHost; - } else { - title = title.replace(/Прокси:([\s\S]+)/, '{0}\n$&'); - var proxies = RegExp.$1; - if (proxies.indexOf(proxyHost) == -1) { - title = title.replace(/Прокси:[\s\S]+/, '$&\n'+ indent + proxyHost); - ifShouldUpdateTitle = true; - } - - var ifHostListedAlready = title.split(/\r?\n/g).some( - line => line.trim() === hostname - ); - if (!ifHostListedAlready) { - title = title.replace('{0}', indent + hostname); - ifShouldUpdateTitle = true; - } else if (ifShouldUpdateTitle) - title = title.replace('\n{0}', ''); - } - - if (ifShouldUpdateTitle) { - chrome.pageAction.setTitle({ - title: title, - tabId: details.tabId - }); - - } - } - ) - ); - - } -} - -chrome.webRequest.onCompleted.addListener( - blockInform, - { urls: [''] } -); - -chrome.webRequest.onErrorOccurred.addListener( - blockInform, - { urls: [''] } -); diff --git a/extensions/chromium/minimalistic-pac-setter/extnesion/block-informer.js b/extensions/chromium/minimalistic-pac-setter/extnesion/block-informer.js index c233f5b..81a3e5e 100755 --- a/extensions/chromium/minimalistic-pac-setter/extnesion/block-informer.js +++ b/extensions/chromium/minimalistic-pac-setter/extnesion/block-informer.js @@ -8,55 +8,143 @@ function getHostname(url) { return a.hostname; } -function blockInform(details) { - if (details.tabId !== -1 && window.antiCensorRu.pacProvider && window.antiCensorRu.pacProvider.proxyIps && window.antiCensorRu.pacProvider.proxyIps[ details.ip ]) { +function updateTitle(requestDetails, cb) { - chrome.pageAction.setIcon({ - path: '/icons/ribbon32.png', - tabId: details.tabId - }); + chrome.pageAction.getTitle( + { tabId: requestDetails.tabId }, + title => { - chrome.pageAction.getTitle( - { tabId: details.tabId }, - title => { + var ifTitleSetAlready = /\n/.test(title); + var proxyHost = window.antiCensorRu.pacProvider.proxyIps[ requestDetails.ip ]; + var hostname = getHostname(requestDetails.url); + var ifShouldUpdateTitle = false; + var indent = ' '; + var proxyTitle = 'Прокси:'; + + // Initially title equals extension name. + if (!ifTitleSetAlready) { + title = 'Разблокированы:\n'+ indent + hostname +'\n'+ proxyTitle +'\n'+ indent + proxyHost; + ifShouldUpdateTitle = true; - var ifTitleSetAlready = !/\n/.test(title); - var proxyHost = window.antiCensorRu.pacProvider.proxyIps[ details.ip ]; - var hostname = getHostname(details.url); - var ifShouldUpdateTitle = false; - var indent = ' '; + function setIcon(iconOpts) { + iconOpts.tabId = requestDetails.tabId; + chrome.pageAction.setIcon(iconOpts); + chrome.pageAction.show(requestDetails.tabId); + } + var iconPath = '/icons/ribbon32.png'; + + if (requestDetails.type === 'main_frame') + setIcon({path: iconPath}); + else { + var canvas = document.createElement('canvas'); + var iconSize = 19; + canvas.width = canvas.height = iconSize; + var img = document.createElement('img'); + img.onload = () => { + var context = canvas.getContext('2d'); + context.drawImage(img, 0, 0, iconSize, iconSize); + var badgeText = '%'; - // Initially title equals extension name. - if (ifTitleSetAlready) { - title = 'Разблокированы:\n'+ indent + hostname +'\nПрокси:\n'+ indent + proxyHost; + context.fillStyle = 'red'; + context.fillRect(0, 9, context.measureText(badgeText).width + 1, iconSize); + + context.fillStyle = 'white'; + context.font = '11px Arial'; + context.fillText('%', 0, iconSize - 1); + + setIcon({imageData: context.getImageData(0, 0, iconSize, iconSize)}) + }; + img.src = iconPath; + } + } else { + var hostsProxiesPair = title.split(proxyTitle); + + if (hostsProxiesPair[1].indexOf(proxyHost) === -1) { + title = title.replace(hostsProxiesPair[1], hostsProxiesPair[1] +'\n'+ indent + proxyHost); ifShouldUpdateTitle = true; - } else { - title = title.replace(/Прокси:([\s\S]+)/, '{0}\n$&'); - var proxies = RegExp.$1; - if (proxies.indexOf(proxyHost) == -1) { - title = title.replace(/Прокси:[\s\S]+/, '$&\n'+ indent + proxyHost); - ifShouldUpdateTitle = true; - } - - var ifHostListedAlready = title.indexOf(hostname) != -1; - if (!ifHostListedAlready) { - title = title.replace('{0}', indent + hostname); - ifShouldUpdateTitle = true; - } else if (ifShouldUpdateTitle) - title = title.replace('{0}\n', ''); } - if (ifShouldUpdateTitle) - chrome.pageAction.setTitle({ - title: title, - tabId: details.tabId - }); + if (hostsProxiesPair[0].indexOf(hostname) === -1) { + title = title.replace(proxyTitle, indent + hostname +'\n'+ proxyTitle); + ifShouldUpdateTitle = true; + } } + + if (ifShouldUpdateTitle) + chrome.pageAction.setTitle({ + title: title, + tabId: requestDetails.tabId + }); + + return cb(); + } + ) +} + +var previousUpdateTitleFinished = Promise.resolve(); +/* + previousUpdateTitleFinished works like a semaphor for updateTitle(). + What? Semaphor? In JavaScript? Are you kidding? + + Look at this: + + function getTitle(cb) { + chrome.pageAction.getTitle( + { tabId: details.tabId }, + cb + ); + } + + function setTitle(title) { + console.log(title); + chrome.pageAction.setTitle({ + title: title +' new!', + tabId: details.tabId + }); + chrome.pageAction.show(details.tabId); + } + + var updateTitle = (details, cb) => { + //var ID = parseInt(Math.random()*100); + //console.log(ID, 'START'); + getTitle( title => { + setTitle(title); + cb && cb(title); + //console.log(ID, 'FINISH'); + }); + } + updateTitle(details); + + Load some massive page. + The logs will be: + + FooBar Extension + (6) FooBar Extension new! + (2) FooBar Extension new! new! + FooBar Extension new! new! new! + FooBar Extension new! new! new! new! + + Notice the (6) and (2) denoting the number of repeated log messages. + Uncommenting logs reveals that START->FINISH pairs are messed. + However, if getTitle is called strictly after setTitle, we are ok: + + previousUpdateTitleFinished = previousUpdateTitleFinished.then( + () => new Promise( resolve => updateTitle( details, resolve ) ) + ); + + Well, I hope it works, because once I caught a bug. +**/ + +function blockInform(details) { + + if (details.tabId !== -1 && window.antiCensorRu.pacProvider && window.antiCensorRu.pacProvider.proxyIps && window.antiCensorRu.pacProvider.proxyIps[ details.ip ]) { + + previousUpdateTitleFinished = previousUpdateTitleFinished.then( + () => new Promise( resolve => updateTitle( details, resolve ) ) ); - chrome.pageAction.show(details.tabId); - } + } chrome.webRequest.onCompleted.addListener( diff --git a/extensions/chromium/minimalistic-pac-setter/extnesion/icons/ribbon-partly32.png b/extensions/chromium/minimalistic-pac-setter/extnesion/icons/ribbon-partly32.png deleted file mode 100755 index df694f79e9f7f8621c6c400404f0c5f9f51cece9..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1527 zcmV8W9O-s)3k* zDOQPqxJsb-0yF|rX($}XLV3pzzY(~@Pg1mwKp!9phzIHb4^RnQ0nX{FTpa6o!W|H|2lfC%Vt}92Rr#S<#uZ~A z9xo}{2|xn+#b|GtrMq2Hv?uP`0BhD89K>pj27GA!#{$dlWd+s%308ePurdbn8@eiQ zmlVx5Vz8uWId^P;q-Y7i^A6ECU6lhsVG{-{w*FZTS@zwmzzD$aX!n*BZ749WF?L;* zmr9B@-6c+v6m4vy22?>$1J)UE%7Ea-^%*;kZXL&}1Mk!OYQw2gbyYqAY`2bGv7^-H z?OSln1oMh2f4pmVcBBbKK8Ce0Mug7{0C7egs}AOpa-$Sro}_3!+?Yg|Z#dmMud8O-$z67HwR4albaXmh?!f7hxXwoNf#FU; zE#xFXprloo!;1#b3a3jPCvXYSPom*?Qwof>Jy3U+h_))ItAC@v{l_y z+^S2hv#(ouz}Z|cDOyG)bTJSV00YP!6mj-{>#Cd|&49$PQR}@MU6n6{JaL$njwM01CQi@Q2hhT0n~$2ZBz)YmT#@9ylHr`GP)wm~!XxOR05&IK!U(l7Z7A z?!B(cYa25l%sJXZFT1{bPqx?Dcdk|EFy*<&HsLi5k9D5&Pqpi@-|p*v{X9z!E$8fp z5q<-L22>hw)_{%G&P&aJWhR(*F`%FVk}DwDfC6XVnzZqzq-dt3Xy&66wwcv}$27sL z5k&LV;qT6}Lim=+nTyN{Pn_x0b#YYJ!2|$8kYYeE8rUvDPn?l9aYusz`Mok5`abV9 z(w1k0ASLLDt7~dJ11hWFez6&xGhm7dRnhjzgsM$_N9UEcOa>setb`ZO>;s@i@UT4n z6&rdm&?0$ZLS41!jw_YW$%MU*5ZufMSt5WJcGf_e2_Kno#)L`}LM9ZOu%QO}XC2?J zSQ!!Xi#Fml4FK{Vc!m;(Z`_P;J|$9INq|5txUc1>?j#q?1Q_u`-jO`F9ZrDMSHz)*UVB|Y3qZdxX_|w7&f6cJXY9o z1>m5gZeJlyrDOvoMY{%M0`$9bdKEARASC)&F<{DVIu+jVJMGh{U*|d2+wX$^*H}ih z05}S?v1M-+Z9uTD@N527n=dzSf192=R&*`DG(%Ff!;+$PYz^)(c~*=-+(uHgVql?D zo_6_9GE07{kBHuQS_k!c?t28_Q0MfpcFvJJIt2A09fEow&^+lA7kt0qB7Y9}GSYit z+gEQeth5M#wQ1vdZ)nywq8j>&4)O!Q?nvZ= z0>3wqmwWxJO`8bNNm8_2U6q#r4VPAF%VcH^dxwzdi%K36eXQ(1c}FuiCs^bk0CRw! z>w_actJDA~U+m8AAYc3yfRzI#v$#G__(oUdk&X%E125a{)UpzkmZeYi{?#ru9+)GZ)8*A49M+;@M5$=Q@1i`+I(zTHlYrfk^3i6eFfk dpV~EP!2jvjP2Lzz{(%4h002ovPDHLkV1i;$wj=-m diff --git a/extensions/chromium/minimalistic-pac-setter/extnesion/manifest.json b/extensions/chromium/minimalistic-pac-setter/extnesion/manifest.json index 470badd..c0a4490 100755 --- a/extensions/chromium/minimalistic-pac-setter/extnesion/manifest.json +++ b/extensions/chromium/minimalistic-pac-setter/extnesion/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 2, - "name": "Ненавязчивый РосКомНадзор", + "name": "Ненавязчивый РосКомНадзор NEW", "description": "Аргументы против цензуры: https://git.io/vEkI9", "version": "0.0.0.10", "icons": { diff --git a/extensions/chromium/minimalistic-pac-setter/extnesion/sync-pac-script-with-pac-provider.js b/extensions/chromium/minimalistic-pac-setter/extnesion/sync-pac-script-with-pac-provider.js index 485f38c..a8294ec 100755 --- a/extensions/chromium/minimalistic-pac-setter/extnesion/sync-pac-script-with-pac-provider.js +++ b/extensions/chromium/minimalistic-pac-setter/extnesion/sync-pac-script-with-pac-provider.js @@ -272,7 +272,7 @@ function asyncLogGroup() { var cb = args.pop(); console.group.apply(console, args); return function() { - console.log('Finished'); + console.log('Finished'); console.groupEnd(); var _cb = cb || (() => {}); return _cb.apply(this, arguments);