diff --git a/platform/chromium/vapi-background.js b/platform/chromium/vapi-background.js index 68ee1af15..8a561d390 100644 --- a/platform/chromium/vapi-background.js +++ b/platform/chromium/vapi-background.js @@ -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; } diff --git a/platform/webext/vapi-usercss.js b/platform/webext/vapi-usercss.js index aea2a2bf5..609bb6d36 100644 --- a/platform/webext/vapi-usercss.js +++ b/platform/webext/vapi-usercss.js @@ -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(){};