Now icon may have a badge. SetTitle semaphor added. Removed unused files.

This commit is contained in:
Ilya Ig. Petrov 2016-02-08 20:43:23 +05:00
parent 0a06037af6
commit 9dd4724167
5 changed files with 128 additions and 133 deletions

View File

@ -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: ['<all_urls>'] }
);
chrome.webRequest.onErrorOccurred.addListener(
blockInform,
{ urls: ['<all_urls>'] }
);

View File

@ -8,55 +8,143 @@ function getHostname(url) {
return a.hostname; return a.hostname;
} }
function blockInform(details) { function updateTitle(requestDetails, cb) {
if (details.tabId !== -1 && window.antiCensorRu.pacProvider && window.antiCensorRu.pacProvider.proxyIps && window.antiCensorRu.pacProvider.proxyIps[ details.ip ]) {
chrome.pageAction.setIcon({ chrome.pageAction.getTitle(
path: '/icons/ribbon32.png', { tabId: requestDetails.tabId },
tabId: details.tabId title => {
});
chrome.pageAction.getTitle( var ifTitleSetAlready = /\n/.test(title);
{ tabId: details.tabId }, var proxyHost = window.antiCensorRu.pacProvider.proxyIps[ requestDetails.ip ];
title => { var hostname = getHostname(requestDetails.url);
var ifShouldUpdateTitle = false;
var indent = ' ';
var proxyTitle = 'Прокси:';
var ifTitleSetAlready = !/\n/.test(title); // Initially title equals extension name.
var proxyHost = window.antiCensorRu.pacProvider.proxyIps[ details.ip ]; if (!ifTitleSetAlready) {
var hostname = getHostname(details.url); title = 'Разблокированы:\n'+ indent + hostname +'\n'+ proxyTitle +'\n'+ indent + proxyHost;
var ifShouldUpdateTitle = false; ifShouldUpdateTitle = true;
var indent = ' ';
// Initially title equals extension name. function setIcon(iconOpts) {
if (ifTitleSetAlready) { iconOpts.tabId = requestDetails.tabId;
title = 'Разблокированы:\n'+ indent + hostname +'\nПрокси:\n'+ indent + proxyHost; 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 = '%';
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; 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) if (hostsProxiesPair[0].indexOf(hostname) === -1) {
chrome.pageAction.setTitle({ title = title.replace(proxyTitle, indent + hostname +'\n'+ proxyTitle);
title: title, ifShouldUpdateTitle = true;
tabId: details.tabId }
});
} }
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 ) )
); );
chrome.pageAction.show(details.tabId); 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.webRequest.onCompleted.addListener( chrome.webRequest.onCompleted.addListener(

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.5 KiB

View File

@ -1,7 +1,7 @@
{ {
"manifest_version": 2, "manifest_version": 2,
"name": "Ненавязчивый РосКомНадзор", "name": "Ненавязчивый РосКомНадзор NEW",
"description": "Аргументы против цензуры: https://git.io/vEkI9", "description": "Аргументы против цензуры: https://git.io/vEkI9",
"version": "0.0.0.10", "version": "0.0.0.10",
"icons": { "icons": {

View File

@ -272,7 +272,7 @@ function asyncLogGroup() {
var cb = args.pop(); var cb = args.pop();
console.group.apply(console, args); console.group.apply(console, args);
return function() { return function() {
console.log('Finished'); console.log('Finished');
console.groupEnd(); console.groupEnd();
var _cb = cb || (() => {}); var _cb = cb || (() => {});
return _cb.apply(this, arguments); return _cb.apply(this, arguments);