mirror of
https://github.com/gorhill/uBlock.git
synced 2024-10-06 09:37:12 +02:00
code review: handle tab gone + decrease user css overhead
This commit is contained in:
parent
2660bee0d2
commit
1a3df881d0
@ -643,41 +643,44 @@ vAPI.setIcon = (function() {
|
|||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
return function(tabId, iconStatus, badge) {
|
var onTabReady = function(tab, status, badge) {
|
||||||
tabId = toChromiumTabId(tabId);
|
if ( vAPI.lastError() || !tab ) { return; }
|
||||||
if ( tabId === 0 ) { return; }
|
|
||||||
|
|
||||||
if ( browserAction.setIcon !== undefined ) {
|
if ( browserAction.setIcon !== undefined ) {
|
||||||
browserAction.setIcon(
|
browserAction.setIcon({
|
||||||
{
|
tabId: tab.id,
|
||||||
tabId: tabId,
|
path: iconPaths[status === 'on' ? 1 : 0]
|
||||||
path: iconPaths[iconStatus === 'on' ? 1 : 0]
|
});
|
||||||
},
|
browserAction.setBadgeText({
|
||||||
function onIconReady() {
|
tabId: tab.id,
|
||||||
if ( vAPI.lastError() ) { return; }
|
text: badge
|
||||||
chrome.browserAction.setBadgeText({
|
});
|
||||||
tabId: tabId,
|
if ( badge !== '' ) {
|
||||||
text: badge
|
browserAction.setBadgeBackgroundColor({
|
||||||
});
|
tabId: tab.id,
|
||||||
if ( badge !== '' ) {
|
color: '#666'
|
||||||
chrome.browserAction.setBadgeBackgroundColor({
|
});
|
||||||
tabId: tabId,
|
}
|
||||||
color: '#666'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( browserAction.setTitle !== undefined ) {
|
if ( browserAction.setTitle !== undefined ) {
|
||||||
browserAction.setTitle({
|
browserAction.setTitle({
|
||||||
tabId: tabId,
|
tabId: tab.id,
|
||||||
title: titleTemplate.replace(
|
title: titleTemplate.replace(
|
||||||
'{badge}',
|
'{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 ) {
|
if ( vAPI.contextMenu instanceof Object ) {
|
||||||
vAPI.contextMenu.onMustUpdate(tabId);
|
vAPI.contextMenu.onMustUpdate(tabId);
|
||||||
@ -831,14 +834,22 @@ vAPI.messaging.onPortMessage = (function() {
|
|||||||
if ( vAPI.supportsUserStylesheets === true ) {
|
if ( vAPI.supportsUserStylesheets === true ) {
|
||||||
details.cssOrigin = 'user';
|
details.cssOrigin = 'user';
|
||||||
}
|
}
|
||||||
if ( msg.toRemove ) {
|
var fn;
|
||||||
details.code = msg.toRemove;
|
if ( msg.add ) {
|
||||||
chrome.tabs.removeCSS(tabId, details);
|
|
||||||
}
|
|
||||||
if ( msg.toAdd ) {
|
|
||||||
details.code = msg.toAdd;
|
|
||||||
details.runAt = 'document_start';
|
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;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -29,48 +29,34 @@
|
|||||||
if ( typeof vAPI !== 'object' ) { return; }
|
if ( typeof vAPI !== 'object' ) { return; }
|
||||||
|
|
||||||
vAPI.userCSS = {
|
vAPI.userCSS = {
|
||||||
_userCSS: '',
|
_userCSS: new Set(),
|
||||||
_disabled: false,
|
_disabled: false,
|
||||||
_send: function(toRemove, toAdd) {
|
_send: function(add, css) {
|
||||||
vAPI.messaging.send('vapi-background', {
|
vAPI.messaging.send('vapi-background', {
|
||||||
what: 'userCSS',
|
what: 'userCSS',
|
||||||
toRemove: toRemove,
|
add: add,
|
||||||
toAdd: toAdd
|
css: css
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
add: function(cssText) {
|
add: function(cssText) {
|
||||||
if ( cssText === '' ) { return; }
|
if ( cssText === '' || this._userCSS.has(cssText) ) { return; }
|
||||||
var before = this._userCSS,
|
this._userCSS.add(cssText);
|
||||||
after = before;
|
|
||||||
if ( after !== '' ) { after += '\n'; }
|
|
||||||
after += cssText;
|
|
||||||
this._userCSS = after;
|
|
||||||
if ( this._disabled ) { return; }
|
if ( this._disabled ) { return; }
|
||||||
this._send(before, after);
|
this._send(true, cssText);
|
||||||
},
|
},
|
||||||
remove: function(cssText) {
|
remove: function(cssText) {
|
||||||
if ( cssText === '' || this._userCSS === '' ) { return; }
|
if ( cssText === '' ) { return; }
|
||||||
var before = this._userCSS,
|
if ( this._userCSS.delete(cssText) && !this._disabled ) {
|
||||||
after = before;
|
this._send(true, cssText);
|
||||||
after = before.replace(cssText, '').trim();
|
this._send(false, cssText);
|
||||||
this._userCSS = after;
|
}
|
||||||
if ( this._disabled ) { return; }
|
|
||||||
this._send(before, after);
|
|
||||||
},
|
},
|
||||||
toggle: function(state) {
|
toggle: function(state) {
|
||||||
if ( state === undefined ) {
|
if ( state === undefined ) { state = this._disabled; }
|
||||||
state = this._disabled;
|
|
||||||
}
|
|
||||||
if ( state !== this._disabled ) { return; }
|
if ( state !== this._disabled ) { return; }
|
||||||
this._disabled = !state;
|
this._disabled = !state;
|
||||||
if ( this._userCSS === '' ) { return; }
|
if ( this._userCSS.size === 0 ) { return; }
|
||||||
var toAdd, toRemove;
|
this._send(state, Array.from(this._userCSS));
|
||||||
if ( state ) {
|
|
||||||
toAdd = this._userCSS;
|
|
||||||
} else {
|
|
||||||
toRemove = this._userCSS;
|
|
||||||
}
|
|
||||||
this._send(toRemove, toAdd);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
vAPI.hideNode = vAPI.unhideNode = function(){};
|
vAPI.hideNode = vAPI.unhideNode = function(){};
|
||||||
|
Loading…
Reference in New Issue
Block a user