From 54f10dd9826250c23dfc0487523584c42777bddf Mon Sep 17 00:00:00 2001 From: Ellis Tsung Date: Sat, 21 Apr 2018 23:51:04 -0400 Subject: [PATCH] Update Safari platform for 1.16 - Reuse some chromium platform files - Use new `vapi-usercss.pseudo.js` (buildscript removes useragent check, which fails for Safari) - Add webextFlavor to vapi-common (relatively bare) --- platform/safari/Info.plist | 2 +- platform/safari/is-webrtc-supported.html | 8 ---- platform/safari/is-webrtc-supported.js | 52 ------------------------ platform/safari/options_ui.html | 11 ----- platform/safari/options_ui.js | 47 --------------------- platform/safari/vapi-background.js | 2 +- platform/safari/vapi-common.js | 34 +++++++++++++++- platform/safari/vapi-webrequest.js | 7 +++- tools/make-safari.sh | 40 ++++++++++++------ 9 files changed, 68 insertions(+), 135 deletions(-) delete mode 100644 platform/safari/is-webrtc-supported.html delete mode 100644 platform/safari/is-webrtc-supported.js delete mode 100644 platform/safari/options_ui.html delete mode 100644 platform/safari/options_ui.js diff --git a/platform/safari/Info.plist b/platform/safari/Info.plist index b6a76e7c0908f..a1b0cdb95f401 100644 --- a/platform/safari/Info.plist +++ b/platform/safari/Info.plist @@ -66,8 +66,8 @@ Start + js/vapi.js js/vapi-client.js - js/vapi-usercss.js js/contentscript.js End diff --git a/platform/safari/is-webrtc-supported.html b/platform/safari/is-webrtc-supported.html deleted file mode 100644 index 15c155583b91e..0000000000000 --- a/platform/safari/is-webrtc-supported.html +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - diff --git a/platform/safari/is-webrtc-supported.js b/platform/safari/is-webrtc-supported.js deleted file mode 100644 index 5a3eea1e7f25f..0000000000000 --- a/platform/safari/is-webrtc-supported.js +++ /dev/null @@ -1,52 +0,0 @@ -/******************************************************************************* - - uBlock Origin - a browser extension to block requests. - Copyright (C) 2015 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 -*/ - -// https://github.com/gorhill/uBlock/issues/533#issuecomment-164292868 -// If WebRTC is supported, there won't be an exception if we -// try to instanciate a peer connection object. - -// https://github.com/gorhill/uBlock/issues/533#issuecomment-168097594 -// Because Chromium leaks WebRTC connections after they have been closed -// and forgotten, we need to test for WebRTC support inside an iframe, this -// way the closed and forgottetn WebRTC connections are properly garbage -// collected. - -(function() { - 'use strict'; - - var pc = null; - try { - var PC = self.RTCPeerConnection || self.webkitRTCPeerConnection; - if ( PC ) { - pc = new PC(null); - } - } catch (ex) { - console.error(ex); - } - if ( pc !== null ) { - pc.close(); - } - - window.top.postMessage( - pc !== null ? 'webRTCSupported' : 'webRTCNotSupported', - window.location.origin - ); -})(); diff --git a/platform/safari/options_ui.html b/platform/safari/options_ui.html deleted file mode 100644 index d94c001efb4b5..0000000000000 --- a/platform/safari/options_ui.html +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - -Dashboard - - diff --git a/platform/safari/options_ui.js b/platform/safari/options_ui.js deleted file mode 100644 index 1e7f552b12bd9..0000000000000 --- a/platform/safari/options_ui.js +++ /dev/null @@ -1,47 +0,0 @@ -/******************************************************************************* - - µBlock - a browser extension to block requests. - Copyright (C) 2015 The µBlock 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 - 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 -*/ - -/******************************************************************************/ - -(function() { - -/******************************************************************************/ - -'use strict'; - -vAPI.messaging.send( - 'default', - { - what: 'gotoURL', - details: { - url: 'dashboard.html', - select: true, - index: -1 - } - } -); -window.close(); - -/******************************************************************************/ - -})(); - -/******************************************************************************/ diff --git a/platform/safari/vapi-background.js b/platform/safari/vapi-background.js index 72a055f35f385..8f4a66776ae94 100644 --- a/platform/safari/vapi-background.js +++ b/platform/safari/vapi-background.js @@ -166,7 +166,7 @@ vAPI.tabs = { /******************************************************************************/ vAPI.isBehindTheSceneTabId = function(tabId) { - return tabId.toString() === '-1'; + return tabId < 0; }; vAPI.noTabId = '-1'; diff --git a/platform/safari/vapi-common.js b/platform/safari/vapi-common.js index a3d095211e839..1007ea58c9c30 100644 --- a/platform/safari/vapi-common.js +++ b/platform/safari/vapi-common.js @@ -1,7 +1,7 @@ /******************************************************************************* - µBlock - a browser extension to block requests. - Copyright (C) 2014 The µBlock authors + uBlock Origin - a browser extension to block requests. + Copyright (C) 2014-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 @@ -33,6 +33,36 @@ var vAPI = self.vAPI = self.vAPI || {}; vAPI.setTimeout = vAPI.setTimeout || self.setTimeout.bind(self); +/******************************************************************************/ +vAPI.webextFlavor = { + major: 0, + soup: new Set() +}; + +(function() { + var ua = navigator.userAgent, + flavor = vAPI.webextFlavor, + soup = flavor.soup; + var dispatch = function() { + window.dispatchEvent(new CustomEvent('webextFlavor')); + }; + + soup.add('ublock'); + + // Whether this is a dev build. + if ( /^\d+\.\d+\.\d+\D/.test(safari.extension.displayVersion) ) { + soup.add('devbuild'); + } + + var match = /\bSafari\/(\d+)/.exec(ua); + if ( match !== null ) { + flavor.major = parseInt(match[1], 10) || 0; + soup.add('apple').add('safari'); + } + + vAPI.setTimeout(dispatch, 97); +})() + /******************************************************************************/ // http://www.w3.org/International/questions/qa-scripts#directions diff --git a/platform/safari/vapi-webrequest.js b/platform/safari/vapi-webrequest.js index 74af97752d0ba..87ec8cde46450 100644 --- a/platform/safari/vapi-webrequest.js +++ b/platform/safari/vapi-webrequest.js @@ -27,7 +27,12 @@ /******************************************************************************/ -vAPI.net = {}; +vAPI.net = { + onBeforeRequest: {}, + onBeforeMaybeSpuriousCSPReport: {}, + onHeadersReceived: {}, + nativeCSPReportFiltering: false +}; vAPI.net.registerListeners = function() { var µb = µBlock, diff --git a/tools/make-safari.sh b/tools/make-safari.sh index 3622980ea92a5..648b6ad447653 100755 --- a/tools/make-safari.sh +++ b/tools/make-safari.sh @@ -21,17 +21,40 @@ cp -R src/_locales "$DES"/ cp src/*.html "$DES"/ mv $DES/img/icon_128.png "$DES"/Icon.png cp platform/safari/*.js "$DES"/js/ -cp platform/safari/*.html "$DES"/ cp -R platform/safari/img "$DES"/ cp platform/safari/Info.plist "$DES"/ cp platform/safari/Settings.plist "$DES"/ cp LICENSE.txt "$DES"/ -cp platform/chromium/vapi.js "$DES"/js/ +# Use some chromium scripts +echo -n '*** uBlock0.safariextension: Copying chromium files...' +chromium_files=(vapi.js is-webrtc-supported.{html,js} options_ui.{html,js}) +for file in "${chromium_files[@]}"; do + file=platform/chromium/"$file" + if [[ ! -e "$file" ]]; then + >&2 echo "ERROR: $file not found." + exit 1 + fi + if [[ "$file" == *.js ]]; then + cp "$file" "$DES"/js/ + else + cp "$file" "$DES"/ + fi +done +echo ' ✔' -# Use chrome's usercss polyfill -echo "*** uBlock0.safariextension: Concatenating content scripts..." -cat platform/chromium/vapi-usercss.js > /tmp/contentscript.js +# Detect OS for sed arguments +if [[ "$OSTYPE" == "darwin"* ]]; then + sedargs=('-i' '') +else + sedargs=('-i') +fi + +# Use pseudo usercss polyfill +echo -n "*** uBlock0.safariextension: Concatenating content scripts..." +cat platform/chromium/vapi-usercss.pseudo.js > /tmp/contentscript.js +# Delete browser check from usercss +sed "${sedargs[@]}" -e '1,/Edge/{/Edge/d;}' -e '1,/ &&/ s///' /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 @@ -46,13 +69,6 @@ echo -n '*** uBlock0.safariextension: Generating Info.plist...' python tools/make-safari-meta.py "$DES"/ echo ' ✔' -# Detect OS for sed arguments -if [[ "$OSTYPE" == "darwin"* ]]; then - declare -a sedargs=('-i' '') -else - declare -a sedargs=('-i') -fi - # https://github.com/el1t/uBlock-Safari/issues/15 echo -n '*** uBlock0.safariextension: Correcting ctrl to ⌘ in messages...' for filename in "$DES"/_locales/*.json; do