Merge pull request #392 from Deathamns/ports/safari

Safari updates
This commit is contained in:
Raymond Hill 2014-12-01 20:04:33 -02:00
commit 0fe69618e3
13 changed files with 260 additions and 231 deletions

1
.gitignore vendored
View file

@ -1,4 +1,3 @@
*.bak *.bak
*.pem *.pem
/meta/safariextz/certs/
/dist/build/ /dist/build/

View file

@ -1,91 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Author</key>
<string>Raymond Hill</string>
<key>Builder Version</key>
<string>534.57.2</string>
<key>CFBundleDisplayName</key>
<string>µBlock</string>
<key>CFBundleIdentifier</key>
<string>net.gorhill.uBlock</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleShortVersionString</key>
<string>0.7.0.10</string>
<key>CFBundleVersion</key>
<string>1456132</string>
<key>Chrome</key>
<dict>
<key>Database Quota</key>
<real>52428800</real>
<key>Global Page</key>
<string>background.html</string>
<key>Popovers</key>
<array>
<dict>
<key>Filename</key>
<string>popup.html</string>
<key>Height</key>
<real>310</real>
<key>Identifier</key>
<string>popover</string>
<key>Width</key>
<real>180</real>
</dict>
</array>
<key>Toolbar Items</key>
<array>
<dict>
<key>Identifier</key>
<string>toolbarItem</string>
<key>Image</key>
<string>img/icon16.png</string>
<key>Label</key>
<string>µBlock</string>
<key>Popover</key>
<string>popover</string>
</dict>
</array>
</dict>
<key>Content</key>
<dict>
<key>Scripts</key>
<dict>
<key>End</key>
<array>
<string>js/contentscript-end.js</string>
</array>
<key>Start</key>
<array>
<string>js/vapi-client.js</string>
<string>js/contentscript-start.js</string>
</array>
</dict>
<key>Whitelist</key>
<array>
<string>http://*/*</string>
<string>https://*/*</string>
</array>
</dict>
<key>Description</key>
<string>Finally, an efficient blocker. Easy on CPU and memory.</string>
<key>ExtensionInfoDictionaryVersion</key>
<string>1.0</string>
<key>Permissions</key>
<dict>
<key>Website Access</key>
<dict>
<key>Include Secure Pages</key>
<true/>
<key>Level</key>
<string>All</string>
</dict>
</dict>
<!-- <key>Update Manifest URL</key>
<string>https://github.com/gorhill/uBlockupdate_safariextz.plist</string> -->
<key>Website</key>
<string>https://github.com/gorhill/uBlock</string>
</dict>
</plist>

View file

@ -1,22 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>DefaultValue</key>
<false/>
<key>FalseValue</key>
<false/>
<key>Key</key>
<string>open_prefs</string>
<key>Secure</key>
<false/>
<key>Title</key>
<string>Click to see the Preferences</string>
<key>TrueValue</key>
<true/>
<key>Type</key>
<string>CheckBox</string>
</dict>
</array>
</plist>

View file

@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Author</key>
<string>Raymond Hill</string>
<key>Builder Version</key>
<string>534.57.2</string>
<key>CFBundleDisplayName</key>
<string>µBlock</string>
<key>CFBundleIdentifier</key>
<string>net.gorhill.uBlock</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleShortVersionString</key>
<string>0.7.2.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>Chrome</key>
<dict>
<key>Database Quota</key>
<real>52428800</real>
<key>Global Page</key>
<string>background.html</string>
<key>Popovers</key>
<array>
<dict>
<key>Filename</key>
<string>popup.html</string>
<key>Height</key>
<real>310</real>
<key>Identifier</key>
<string>popover</string>
<key>Width</key>
<real>180</real>
</dict>
</array>
<key>Toolbar Items</key>
<array>
<dict>
<key>Identifier</key>
<string>toolbarItem</string>
<key>Image</key>
<string>img/icon16.png</string>
<key>Label</key>
<string>µBlock</string>
<key>Popover</key>
<string>popover</string>
</dict>
</array>
</dict>
<key>Content</key>
<dict>
<key>Scripts</key>
<dict>
<key>End</key>
<array>
<string>js/contentscript-end.js</string>
</array>
<key>Start</key>
<array>
<string>js/vapi-client.js</string>
<string>js/contentscript-start.js</string>
</array>
</dict>
<key>Whitelist</key>
<array>
<string>http://*/*</string>
<string>https://*/*</string>
</array>
</dict>
<key>Description</key>
<string>Finally, an efficient blocker. Easy on CPU and memory.</string>
<key>ExtensionInfoDictionaryVersion</key>
<string>1.0</string>
<key>Permissions</key>
<dict>
<key>Website Access</key>
<dict>
<key>Include Secure Pages</key>
<true/>
<key>Level</key>
<string>All</string>
</dict>
</dict>
<!-- <key>Update Manifest URL</key>
<string>https://github.com/gorhill/uBlockupdate_safariextz.plist</string> -->
<key>Website</key>
<string>https://github.com/gorhill/uBlock</string>
</dict>
</plist>

View file

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>DefaultValue</key>
<false/>
<key>FalseValue</key>
<false/>
<key>Key</key>
<string>open_prefs</string>
<key>Secure</key>
<false/>
<key>Title</key>
<string>Click to see the Preferences</string>
<key>TrueValue</key>
<true/>
<key>Type</key>
<string>CheckBox</string>
</dict>
</array>
</plist>

View file

@ -10,9 +10,9 @@
<key>Developer Identifier</key> <key>Developer Identifier</key>
<string></string> <string></string>
<key>CFBundleShortVersionString</key> <key>CFBundleShortVersionString</key>
<string>0.7.0.10</string> <string>0.7.0.11</string>
<key>CFBundleVersion</key> <key>CFBundleVersion</key>
<string>1456132</string> <string>1</string>
<key>URL</key> <key>URL</key>
<string>https://.../uBlock.safariextz</string> <string>https://.../uBlock.safariextz</string>
</dict> </dict>

View file

@ -36,6 +36,13 @@ vAPI.safari = true;
/******************************************************************************/ /******************************************************************************/
vAPI.app = {
name: 'µBlock',
version: '0.7.2.0'
};
/******************************************************************************/
// addContentScriptFromURL allows whitelisting, // addContentScriptFromURL allows whitelisting,
// so load sitepaching this way, instead of adding it to the Info.plist // so load sitepaching this way, instead of adding it to the Info.plist
@ -132,6 +139,10 @@ vAPI.storage = {
callback(); callback();
}, },
getBytesInUse: function(keys, callback) { getBytesInUse: function(keys, callback) {
if (typeof callback !== 'function') {
return;
}
var key, size = 0; var key, size = 0;
if (keys === null) { if (keys === null) {
@ -183,12 +194,9 @@ vAPI.tabs.registerListeners = function() {
safari.application.addEventListener('navigate', this.onNavigation, true); safari.application.addEventListener('navigate', this.onNavigation, true);
} }
// ??
/* if (typeof this.onUpdated === 'function') { } */
// onClosed handled in the main tab-close event // onClosed handled in the main tab-close event
// onPopup is handled in window.open on web-pages? // onUpdated handled via monitoring the history.pushState on web-pages
/* if (typeof onPopup === 'function') { } */ // onPopup is handled in window.open on web-pages
}; };
/******************************************************************************/ /******************************************************************************/
@ -230,6 +238,9 @@ vAPI.tabs.get = function(tabId, callback) {
title: tab.title title: tab.title
}); });
}; };
/******************************************************************************/
// properties of the details object: // properties of the details object:
// url: 'URL', // the address that will be opened // url: 'URL', // the address that will be opened
// tabId: 1, // the tab is used if set, instead of creating a new one // tabId: 1, // the tab is used if set, instead of creating a new one
@ -237,8 +248,6 @@ vAPI.tabs.get = function(tabId, callback) {
// active: false, // opens the tab in background - true and undefined: foreground // active: false, // opens the tab in background - true and undefined: foreground
// select: true // if a tab is already opened with that url, then select it instead of opening a new one // select: true // if a tab is already opened with that url, then select it instead of opening a new one
/******************************************************************************/
vAPI.tabs.open = function(details) { vAPI.tabs.open = function(details) {
if (!details.url) { if (!details.url) {
return null; return null;
@ -293,13 +302,19 @@ vAPI.tabs.open = function(details) {
/******************************************************************************/ /******************************************************************************/
vAPI.tabs.close = function(tab) { vAPI.tabs.remove = function(tabIds) {
if (!(tab instanceof SafariBrowserTab)) { if (tabIds instanceof SafariBrowserTab) {
tab = this.stack[tab]; tabIds = this.getTabId(tabIds);
} }
if (tab) { if (!Array.isArray(tabIds)) {
tab.close(); tabIds = [tabIds];
}
for (var i = 0; i < tabIds.length; i++) {
if (this.stack[tabIds[i]]) {
this.stack[tabIds[i]].close();
}
} }
}; };
@ -560,10 +575,32 @@ vAPI.messaging.broadcast = function(message) {
/******************************************************************************/ /******************************************************************************/
vAPI.net = {} safari.application.addEventListener('beforeNavigate', function(e) {
if (!vAPI.tabs.expectPopUpFrom || e.url === 'about:blank') {
return;
}
var details = {
url: e.url,
tabId: vAPI.tabs.getTabId(e.target),
sourceTabId: vAPI.tabs.expectPopUpFrom
};
vAPI.tabs.expectPopUpFrom = null;
if (vAPI.tabs.onPopup(details)) {
e.preventDefault();
if (vAPI.tabs.stack[details.sourceTabId]) {
vAPI.tabs.stack[details.sourceTabId].activate();
}
}
}, true);
/******************************************************************************/ /******************************************************************************/
vAPI.net = {}
vAPI.net.registerListeners = function() { vAPI.net.registerListeners = function() {
var onBeforeRequest = this.onBeforeRequest; var onBeforeRequest = this.onBeforeRequest;
@ -592,19 +629,28 @@ vAPI.net.registerListeners = function() {
return e.message; return e.message;
} }
// blocking unwanted pop-ups // when the URL changes, but the document doesn't
if (e.message.type === 'popup') { if (e.message.type === 'popstate') {
if (typeof vAPI.tabs.onPopup === 'function') { vAPI.tabs.onUpdated(
e.message.type = 'main_frame'; vAPI.tabs.getTabId(e.target),
e.message.sourceTabId = vAPI.tabs.getTabId(e.target); {url: e.message.url},
{url: e.message.url}
if (vAPI.tabs.onPopup(e.message)) { );
e.message = false;
return; return;
} }
// blocking unwanted pop-ups
else if (e.message.type === 'popup') {
if (e.message.url === 'about:blank') {
vAPI.tabs.expectPopUpFrom = vAPI.tabs.getTabId(e.target);
e.message = true;
return;
} }
e.message = true; e.message = !vAPI.tabs.onPopup({
url: e.message.url,
tabId: 0,
sourceTabId: vAPI.tabs.getTabId(e.target)
});
return; return;
} }
@ -620,7 +666,7 @@ vAPI.net.registerListeners = function() {
// truthy return value will allow the request, // truthy return value will allow the request,
// except when redirectUrl is present // except when redirectUrl is present
if (block && typeof block === 'object') { if (block && typeof block === 'object') {
if (block.cancel) { if (block.cancel === true) {
e.message = false; e.message = false;
} }
else if (e.message.type === 'script' else if (e.message.type === 'script'
@ -638,7 +684,11 @@ vAPI.net.registerListeners = function() {
return e.message; return e.message;
}; };
safari.application.addEventListener('message', this.onBeforeRequest.callback, true); safari.application.addEventListener(
'message',
this.onBeforeRequest.callback,
true
);
} }
}; };

View file

@ -30,7 +30,7 @@
/******************************************************************************/ /******************************************************************************/
self.vAPI = self.vAPI || {}; self.vAPI = self.vAPI || {};
self.vAPI.safari = true; vAPI.safari = true;
/******************************************************************************/ /******************************************************************************/
@ -39,11 +39,15 @@ var messagingConnector = function(response) {
return; return;
} }
var channels = vAPI.messaging.channels;
var channel, listener; var channel, listener;
if ( response.broadcast === true ) { if ( response.broadcast === true ) {
for ( channel in vAPI.messaging.channels ) { for ( channel in channels ) {
listener = vAPI.messaging.channels[channel].listener; if ( channels.hasOwnProperty(channel) === false ) {
continue;
}
listener = channels[channel].listener;
if ( typeof listener === 'function' ) { if ( typeof listener === 'function' ) {
listener(response.msg); listener(response.msg);
} }
@ -58,7 +62,7 @@ var messagingConnector = function(response) {
} }
if ( !listener ) { if ( !listener ) {
channel = vAPI.messaging.channels[response.portName]; channel = channels[response.portName];
listener = channel && channel.listener; listener = channel && channel.listener;
} }
@ -171,9 +175,16 @@ vAPI.messaging = {
/******************************************************************************/ /******************************************************************************/
// content scripts are loaded into extension pages by default, but they shouldn't vAPI.canExecuteContentScript = function() {
return /^https?:/.test(location.protocol);
};
if (location.protocol === "safari-extension:") { /******************************************************************************/
// This file can be included into extensin pages,
// but the following code should run only in content pages.
if (location.protocol === 'safari-extension:') {
return; return;
} }
@ -209,7 +220,7 @@ var onBeforeLoad = function(e, details) {
linkHelper.href = details ? details.url : e.url; linkHelper.href = details ? details.url : e.url;
if (!/^https?:/.test(linkHelper.protocol)) { if (!(/^https?:/.test(linkHelper.protocol) || (details && details.type === 'popup'))) {
return; return;
} }
@ -319,28 +330,51 @@ var firstMutation = function() {
// the extension context is unable to reach the page context, // the extension context is unable to reach the page context,
// also this only works when Content Security Policy allows inline scripts // also this only works when Content Security Policy allows inline scripts
var tmpJS = document.createElement('script'); var tmpJS = document.createElement('script');
var tmpScript = ["(function() {", var tmpScript = ['(function() {',
"var block = function(u, t) {", 'var block = function(u, t) {',
"var e = document.createEvent('CustomEvent'),", 'var e = document.createEvent("CustomEvent"),',
"d = {url: u, type: t};", 'd = {url: u, type: t};',
"e.initCustomEvent('" + randEventName + "', !1, !1, d);", 'e.initCustomEvent("' + randEventName + '", !1, !1, d);',
"dispatchEvent(e);", 'dispatchEvent(e);',
"return d.url === !1;", 'return d.url === !1;',
"}, wo = open, xo = XMLHttpRequest.prototype.open;", '}, wo = open, xo = XMLHttpRequest.prototype.open;',
"open = function(u) {", 'open = function(u) {',
"return block(u, 'popup') ? null : wo.apply(this, [].slice.call(arguments));", 'return block(u, "popup") ? null : wo.apply(this, arguments);',
"};", '};',
"XMLHttpRequest.prototype.open = function(m, u, s) {", 'XMLHttpRequest.prototype.open = function(m, u, s) {',
"return xo.apply(this, block(u, 'xmlhttprequest') ? ['HEAD', u, s] : [].slice.call(arguments));", 'return xo.apply(this, block(u, "xmlhttprequest") ? ["HEAD", u, s] : arguments);',
"};" '};'
]; ];
if (vAPI.sitePatch if (frameId === 0) {
&& !safari.self.tab.canLoad(beforeLoadEvent, {isWhiteListed: location.href})) { tmpScript.push(
'var pS = history.pushState, rS = history.replaceState,',
'onpopstate = function(e) {',
'if (!e || e.state !== null) block(location.href, "popstate");',
'};',
'window.addEventListener("popstate", onpopstate, true);',
'history.pushState = function() {',
'var r = pS.apply(this, arguments);',
'onpopstate();',
'return r;',
'};',
'history.replaceState = function() {',
'var r = pR.apply(this, arguments);',
'onpopstate();',
'return r;',
'};'
);
}
var block = safari.self.tab.canLoad(beforeLoadEvent, {
isWhiteListed: location.href
});
if (vAPI.sitePatch && !block) {
tmpScript.push('(' + vAPI.sitePatch + ')();'); tmpScript.push('(' + vAPI.sitePatch + ')();');
} }
tmpScript.push("})();"); tmpScript.push('})();');
tmpJS.textContent = tmpScript.join(''); tmpJS.textContent = tmpScript.join('');
document.documentElement.removeChild(document.documentElement.appendChild(tmpJS)); document.documentElement.removeChild(document.documentElement.appendChild(tmpJS));
}; };
@ -358,7 +392,7 @@ var onContextMenu = function(e) {
details.editable = details.tagName === 'textarea' || details.tagName === 'input'; details.editable = details.tagName === 'textarea' || details.tagName === 'input';
if ('checked' in e.target) { if (e.target.hasOwnProperty('checked')) {
details.checked = e.target.checked; details.checked = e.target.checked;
} }
@ -366,7 +400,7 @@ var onContextMenu = function(e) {
details.linkUrl = e.target.href; details.linkUrl = e.target.href;
} }
if ('src' in e.target) { if (e.target.hasOwnProperty('src')) {
details.srcUrl = e.target.src; details.srcUrl = e.target.src;
if (details.tagName === 'img') { if (details.tagName === 'img') {
@ -394,12 +428,6 @@ if (frameId === 0) {
/******************************************************************************/ /******************************************************************************/
self.vAPI.canExecuteContentScript = function() {
return /^https?:/.test(location.protocol);
};
/******************************************************************************/
})(); })();
/******************************************************************************/ /******************************************************************************/

View file

@ -55,7 +55,6 @@ vAPI.download = function(details) {
a.dispatchEvent(new MouseEvent('click')); a.dispatchEvent(new MouseEvent('click'));
return; return;
} }
var messager = vAPI.messaging.channel('_download'); var messager = vAPI.messaging.channel('_download');
messager.send({ messager.send({
what: 'gotoURL', what: 'gotoURL',

View file

@ -1,43 +0,0 @@
/*******************************************************************************
µBlock - a Chromium browser extension to block requests.
Copyright (C) 2014 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
*/
// Can be included anywhere if it's needed
/* global self */
/******************************************************************************/
(function(){
'use strict';
/******************************************************************************/
self.vAPI = self.vAPI || {};
self.vAPI.app = {
name: 'µBlock',
version: '0.7.2.0'
};
/******************************************************************************/
})();

View file

@ -80,15 +80,12 @@ vAPI.tabs.onPopup = function(details) {
// Blocked // Blocked
// Safari blocks before the pop-up opens, so there is no window to remove.
// TODO: Can't this test be done within the platform-specific code?
if ( vAPI.safari ) {
return true;
}
// It is a popup, block and remove the tab. // It is a popup, block and remove the tab.
µBlock.unbindTabFromPageStats(details.tabId); µBlock.unbindTabFromPageStats(details.tabId);
vAPI.tabs.remove(details.tabId); vAPI.tabs.remove(details.tabId);
// for Safari
return true;
}; };
vAPI.tabs.registerListeners(); vAPI.tabs.registerListeners();

View file

@ -15,8 +15,7 @@ cp -R src/lib $DES/
cp -R src/_locales $DES/ cp -R src/_locales $DES/
cp src/*.html $DES/ cp src/*.html $DES/
cp src/img/icon_128.png $DES/Icon.png cp src/img/icon_128.png $DES/Icon.png
cp platform/vapi-appinfo.js $DES/js/ cp platform/safari/*.js $DES/js/
cp meta/safariextz/*.js $DES/js/ cp platform/safari/Info.plist $DES/
cp meta/safariextz/Info.plist $DES/ cp platform/safari/Settings.plist $DES/
cp meta/safariextz/Settings.plist $DES/
echo "*** uBlock.safariextension: Package done." echo "*** uBlock.safariextension: Package done."