Skip to content

Commit f328687

Browse files
committed
implement user styles in webext platform
1 parent 0d892a8 commit f328687

File tree

4 files changed

+119
-7
lines changed

4 files changed

+119
-7
lines changed

platform/chromium/vapi-background.js

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -655,8 +655,8 @@ vAPI.messaging.listen = function(listenerName, callback) {
655655
/******************************************************************************/
656656

657657
vAPI.messaging.onPortMessage = (function() {
658-
var messaging = vAPI.messaging;
659-
var toAuxPending = {};
658+
var messaging = vAPI.messaging,
659+
toAuxPending = {};
660660

661661
// Use a wrapper to avoid closure and to allow reuse.
662662
var CallbackWrapper = function(port, request, timeout) {
@@ -703,8 +703,8 @@ vAPI.messaging.onPortMessage = (function() {
703703
};
704704

705705
var toAux = function(details, portFrom) {
706-
var port, portTo;
707-
var chromiumTabId = toChromiumTabId(details.toTabId);
706+
var port, portTo,
707+
chromiumTabId = toChromiumTabId(details.toTabId);
708708

709709
// TODO: This could be an issue with a lot of tabs: easy to address
710710
// with a port name to tab id map.
@@ -761,6 +761,32 @@ vAPI.messaging.onPortMessage = (function() {
761761
wrapper.callback(details.msg);
762762
};
763763

764+
var toFramework = function(msg, sender) {
765+
var tabId = sender && sender.tab && sender.tab.id;
766+
if ( !tabId ) { return; }
767+
switch ( msg.what ) {
768+
case 'userCSS':
769+
if ( msg.toRemove ) {
770+
chrome.tabs.removeCSS(tabId, {
771+
code: msg.toRemove,
772+
cssOrigin: 'user',
773+
frameId: sender.frameId,
774+
matchAboutBlank: true
775+
});
776+
}
777+
if ( msg.toAdd ) {
778+
chrome.tabs.insertCSS(tabId, {
779+
code: msg.toAdd,
780+
cssOrigin: 'user',
781+
frameId: sender.frameId,
782+
matchAboutBlank: true,
783+
runAt: 'document_start'
784+
});
785+
}
786+
break;
787+
}
788+
};
789+
764790
return function(request, port) {
765791
// Auxiliary process to auxiliary process
766792
if ( request.toTabId !== undefined ) {
@@ -774,15 +800,22 @@ vAPI.messaging.onPortMessage = (function() {
774800
return;
775801
}
776802

803+
// Content process to main process: framework handler.
804+
// No callback supported/needed for now.
805+
if ( request.channelName === 'vapi-background' ) {
806+
toFramework(request.msg, port.sender);
807+
return;
808+
}
809+
777810
// Auxiliary process to main process: prepare response
778811
var callback = messaging.NOOPFUNC;
779812
if ( request.auxProcessId !== undefined ) {
780813
callback = callbackWrapperFactory(port, request).callback;
781814
}
782815

783816
// Auxiliary process to main process: specific handler
784-
var r = messaging.UNHANDLED;
785-
var listener = messaging.listeners[request.channelName];
817+
var r = messaging.UNHANDLED,
818+
listener = messaging.listeners[request.channelName];
786819
if ( typeof listener === 'function' ) {
787820
r = listener(request.msg, port.sender, callback);
788821
}

platform/webext/manifest.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
],
3838
"js":[
3939
"js/vapi-client.js",
40+
"js/vapi-usercss.js",
4041
"js/contentscript.js"
4142
],
4243
"run_at":"document_start",

platform/webext/vapi-usercss.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*******************************************************************************
2+
3+
uBlock Origin - a browser extension to block requests.
4+
Copyright (C) 2017 Raymond Hill
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see {http://www.gnu.org/licenses/}.
18+
19+
Home: https://github.com/gorhill/uBlock
20+
*/
21+
22+
'use strict';
23+
24+
// For content pages
25+
26+
/******************************************************************************/
27+
28+
(function() {
29+
if ( typeof vAPI !== 'object' ) { return; }
30+
31+
vAPI.userCSS = {
32+
_userCSS: '',
33+
_disabled: false,
34+
_send: function(toRemove, toAdd) {
35+
vAPI.messaging.send('vapi-background', {
36+
what: 'userCSS',
37+
toRemove: toRemove,
38+
toAdd: toAdd
39+
});
40+
},
41+
add: function(cssText) {
42+
if ( cssText === '' ) { return; }
43+
var before = this._userCSS,
44+
after = before;
45+
if ( after !== '' ) { after += '\n'; }
46+
after += cssText;
47+
this._userCSS = after;
48+
if ( this._disabled ) { return; }
49+
this._send(before, after);
50+
},
51+
remove: function(cssText) {
52+
if ( cssText === '' || this._userCSS === '' ) { return; }
53+
var before = this._userCSS,
54+
after = before;
55+
after = before.replace(cssText, '').trim();
56+
this._userCSS = after;
57+
if ( this._disabled ) { return; }
58+
this._send(before, after);
59+
},
60+
toggle: function(state) {
61+
if ( state === undefined ) {
62+
state = this._disabled;
63+
}
64+
if ( state !== this._disabled ) { return; }
65+
this._disabled = !state;
66+
if ( this._userCSS === '' ) { return; }
67+
var toAdd, toRemove;
68+
if ( state ) {
69+
toAdd = this._userCSS;
70+
} else {
71+
toRemove = this._userCSS;
72+
}
73+
this._send(toRemove, toAdd);
74+
}
75+
};
76+
vAPI.hideNode = vAPI.unhideNode = function(){};
77+
})();

tools/make-webext.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ cp -R platform/chromium/img $DES/webextension/
2323
cp platform/chromium/*.html $DES/webextension/
2424
cp platform/chromium/*.json $DES/webextension/
2525
cp platform/webext/polyfill.js $DES/webextension/js/
26+
cp platform/webext/vapi-usercss.js $DES/webextension/js/
27+
cp platform/webext/manifest.json $DES/webextension/
2628
cp LICENSE.txt $DES/webextension/
2729

2830
cp platform/webext/background.html $DES/webextension/
2931
cp platform/webext/from-legacy.js $DES/webextension/js/
30-
cp platform/webext/manifest.json $DES/webextension/
3132
cp platform/webext/bootstrap.js $DES/
3233
cp platform/webext/chrome.manifest $DES/
3334
cp platform/webext/install.rdf $DES/

0 commit comments

Comments
 (0)