1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-24 03:12:46 +01:00

code review: handle tab gone + decrease user css overhead

This commit is contained in:
gorhill 2017-09-05 19:51:16 -04:00
parent 2660bee0d2
commit 1a3df881d0
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
2 changed files with 57 additions and 60 deletions

View File

@ -643,41 +643,44 @@ vAPI.setIcon = (function() {
}
];
return function(tabId, iconStatus, badge) {
tabId = toChromiumTabId(tabId);
if ( tabId === 0 ) { return; }
var onTabReady = function(tab, status, badge) {
if ( vAPI.lastError() || !tab ) { return; }
if ( browserAction.setIcon !== undefined ) {
browserAction.setIcon(
{
tabId: tabId,
path: iconPaths[iconStatus === 'on' ? 1 : 0]
},
function onIconReady() {
if ( vAPI.lastError() ) { return; }
chrome.browserAction.setBadgeText({
tabId: tabId,
text: badge
});
if ( badge !== '' ) {
chrome.browserAction.setBadgeBackgroundColor({
tabId: tabId,
color: '#666'
});
}
}
);
browserAction.setIcon({
tabId: tab.id,
path: iconPaths[status === 'on' ? 1 : 0]
});
browserAction.setBadgeText({
tabId: tab.id,
text: badge
});
if ( badge !== '' ) {
browserAction.setBadgeBackgroundColor({
tabId: tab.id,
color: '#666'
});
}
}
if ( browserAction.setTitle !== undefined ) {
browserAction.setTitle({
tabId: tabId,
tabId: tab.id,
title: titleTemplate.replace(
'{badge}',
iconStatus === 'on' ? (badge !== '' ? badge : '0') : 'off'
status === 'on' ? (badge !== '' ? badge : '0') : 'off'
)
});
}
};
return function(tabId, iconStatus, badge) {
tabId = toChromiumTabId(tabId);
if ( tabId === 0 ) { return; }
chrome.tabs.get(tabId, function(tab) {
onTabReady(tab, iconStatus, badge);
});
if ( vAPI.contextMenu instanceof Object ) {
vAPI.contextMenu.onMustUpdate(tabId);
@ -831,14 +834,22 @@ vAPI.messaging.onPortMessage = (function() {
if ( vAPI.supportsUserStylesheets === true ) {
details.cssOrigin = 'user';
}
if ( msg.toRemove ) {
details.code = msg.toRemove;
chrome.tabs.removeCSS(tabId, details);
}
if ( msg.toAdd ) {
details.code = msg.toAdd;
var fn;
if ( msg.add ) {
details.runAt = 'document_start';
chrome.tabs.insertCSS(tabId, details);
fn = chrome.tabs.insertCSS;
} else {
fn = chrome.tabs.removeCSS;
}
var css = msg.css;
if ( typeof css === 'string' ) {
details.code = css;
fn(tabId, details);
return;
}
for ( var i = 0, n = css.length; i < n; i++ ) {
details.code = css[i];
fn(tabId, details);
}
break;
}

View File

@ -29,48 +29,34 @@
if ( typeof vAPI !== 'object' ) { return; }
vAPI.userCSS = {
_userCSS: '',
_userCSS: new Set(),
_disabled: false,
_send: function(toRemove, toAdd) {
_send: function(add, css) {
vAPI.messaging.send('vapi-background', {
what: 'userCSS',
toRemove: toRemove,
toAdd: toAdd
add: add,
css: css
});
},
add: function(cssText) {
if ( cssText === '' ) { return; }
var before = this._userCSS,
after = before;
if ( after !== '' ) { after += '\n'; }
after += cssText;
this._userCSS = after;
if ( cssText === '' || this._userCSS.has(cssText) ) { return; }
this._userCSS.add(cssText);
if ( this._disabled ) { return; }
this._send(before, after);
this._send(true, cssText);
},
remove: function(cssText) {
if ( cssText === '' || this._userCSS === '' ) { return; }
var before = this._userCSS,
after = before;
after = before.replace(cssText, '').trim();
this._userCSS = after;
if ( this._disabled ) { return; }
this._send(before, after);
if ( cssText === '' ) { return; }
if ( this._userCSS.delete(cssText) && !this._disabled ) {
this._send(true, cssText);
this._send(false, cssText);
}
},
toggle: function(state) {
if ( state === undefined ) {
state = this._disabled;
}
if ( state === undefined ) { state = this._disabled; }
if ( state !== this._disabled ) { return; }
this._disabled = !state;
if ( this._userCSS === '' ) { return; }
var toAdd, toRemove;
if ( state ) {
toAdd = this._userCSS;
} else {
toRemove = this._userCSS;
}
this._send(toRemove, toAdd);
if ( this._userCSS.size === 0 ) { return; }
this._send(state, Array.from(this._userCSS));
}
};
vAPI.hideNode = vAPI.unhideNode = function(){};