Skip to content

Commit

Permalink
better manage platform-specific code for user-stylesheet support
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed Apr 26, 2018
1 parent 6a01203 commit e5f2e97
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 24 deletions.
10 changes: 9 additions & 1 deletion platform/chromium/vapi-cachestorage.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*******************************************************************************
uBlock Origin - a browser extension to block requests.
Copyright (C) 2016-2017 The uBlock Origin authors
Copyright (C) 2016-2018 The uBlock Origin authors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -39,6 +39,14 @@
// indexedDB.

vAPI.cacheStorage = (function() {

// Firefox-specific: we use indexedDB because chrome.storage.local() has
// poor performance in Firefox. See:
// https://bugzilla.mozilla.org/show_bug.cgi?id=1371255
if ( vAPI.webextFlavor.soup.has('firefox') === false ) {
return vAPI.cacheStorage;
}

const STORAGE_NAME = 'uBlock0CacheStorage';
var db;
var pending = [];
Expand Down
32 changes: 32 additions & 0 deletions platform/chromium/vapi-usercss.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*******************************************************************************
uBlock Origin - a browser extension to block requests.
Copyright (C) 2018 Raymond Hill
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see {http://www.gnu.org/licenses/}.
Home: https://github.com/gorhill/uBlock
*/

'use strict';

// This file can be replaced by platform-specific code. If a platform is
// known to NOT support user stylsheets, vAPI.supportsUserStylesheets can be
// set to `false`.

// Chromium 66 and above supports user stylesheets:
// https://github.com/gorhill/uBlock/issues/3588

vAPI.supportsUserStylesheets =
/\bChrom(?:e|ium)\/(?:6[6789]|[789]|1\d\d)/.test(navigator.userAgent);
16 changes: 3 additions & 13 deletions platform/chromium/vapi-usercss.pseudo.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,10 @@

'use strict';

// For content pages
// Packaging this file is optional: it is not necessary to package it if the
// platform is known to support user stylesheets.

// Abort execution if our global vAPI object does not exist.
// https://github.com/chrisaljoudi/uBlock/issues/456
// https://github.com/gorhill/uBlock/issues/2029

// https://github.com/gorhill/uBlock/issues/3588
// Chromium 66+ supports user stylesheets. Testing support against a regex is
// (unfortunately) necessary from content scripts.

if (
typeof vAPI === 'object' &&
/\bEdge\/\d+|\bChrom(?:e|ium)\/(?:[45][0-9]|6[0-5])/.test(navigator.userAgent)
) {
if ( typeof vAPI === 'object' && vAPI.userStylesheet === undefined ) {
// >>>>>>>> start of HUGE-IF-BLOCK

/******************************************************************************/
Expand Down
8 changes: 3 additions & 5 deletions platform/chromium/vapi-usercss.real.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@

'use strict';

// For content pages
// Packaging this file is optional: it is not necessary to package it if the
// platform is known to not support user stylesheets.

if (
typeof vAPI === 'object' &&
vAPI.userStylesheet === undefined
) {
if ( typeof vAPI === 'object' && vAPI.supportsUserStylesheets ) {
// >>>>>>>> start of HUGE-IF-BLOCK

/******************************************************************************/
Expand Down
32 changes: 32 additions & 0 deletions platform/debian/vapi-usercss.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*******************************************************************************
uBlock Origin - a browser extension to block requests.
Copyright (C) 2018 Raymond Hill
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see {http://www.gnu.org/licenses/}.
Home: https://github.com/gorhill/uBlock
*/

'use strict';

// This file can be replaced by platform-specific code. If a platform is
// known to NOT support user stylsheets, vAPI.supportsUserStylesheets can be
// set to `false`.

// Chromium 66 and above supports user stylesheets:
// https://github.com/gorhill/uBlock/issues/3588

vAPI.supportsUserStylesheets =
/\bChrom(?:e|ium)\/(?:6[6789]|[789]|1\d\d)|\bFirefox\/\d/.test(navigator.userAgent);
26 changes: 26 additions & 0 deletions platform/webext/vapi-usercss.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*******************************************************************************
uBlock Origin - a browser extension to block requests.
Copyright (C) 2018 Raymond Hill
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see {http://www.gnu.org/licenses/}.
Home: https://github.com/gorhill/uBlock
*/

'use strict';

// User stylesheets are always supported with Firefox/webext .

vAPI.supportsUserStylesheets = true;
7 changes: 5 additions & 2 deletions tools/make-chromium.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@ cp platform/chromium/*.json $DES/
cp LICENSE.txt $DES/

echo "*** uBlock0.chromium: concatenating content scripts"
cat $DES/js/vapi-usercss.pseudo.js > /tmp/contentscript.js
cat $DES/js/vapi-usercss.js > /tmp/contentscript.js
echo >> /tmp/contentscript.js
grep -v "^'use strict';$" $DES/js/vapi-usercss.real.js >> /tmp/contentscript.js
echo >> /tmp/contentscript.js
grep -v "^'use strict';$" $DES/js/vapi-usercss.pseudo.js >> /tmp/contentscript.js
echo >> /tmp/contentscript.js
grep -v "^'use strict';$" $DES/js/contentscript.js >> /tmp/contentscript.js
mv /tmp/contentscript.js $DES/js/contentscript.js
rm $DES/js/vapi-usercss.pseudo.js
rm $DES/js/vapi-usercss.js
rm $DES/js/vapi-usercss.real.js
rm $DES/js/vapi-usercss.pseudo.js

# Chrome store-specific
cp -R $DES/_locales/nb $DES/_locales/no
Expand Down
9 changes: 6 additions & 3 deletions tools/make-webext.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,19 @@ cp LICENSE.txt $DES/

cp platform/webext/manifest.json $DES/
cp platform/webext/polyfill.js $DES/js/
cp platform/webext/vapi-usercss.js $DES/js/
cp platform/webext/vapi-webrequest.js $DES/js/
cp platform/webext/vapi-cachestorage.js $DES/js/

echo "*** uBlock0.webext: concatenating content scripts"
cat $DES/js/vapi-usercss.real.js > /tmp/contentscript.js
cat $DES/js/vapi-usercss.js > /tmp/contentscript.js
echo >> /tmp/contentscript.js
grep -v "^'use strict';$" $DES/js/vapi-usercss.real.js >> /tmp/contentscript.js
echo >> /tmp/contentscript.js
grep -v "^'use strict';$" $DES/js/contentscript.js >> /tmp/contentscript.js
mv /tmp/contentscript.js $DES/js/contentscript.js
rm $DES/js/vapi-usercss.pseudo.js
rm $DES/js/vapi-usercss.js
rm $DES/js/vapi-usercss.real.js
rm $DES/js/vapi-usercss.pseudo.js

# Webext-specific
rm $DES/img/icon_128.png
Expand Down

0 comments on commit e5f2e97

Please sign in to comment.