Only top window should have type `main_frame`

This commit is contained in:
Deathamns 2014-11-11 07:10:32 +01:00
parent 2024ba1bcd
commit 8edb628e1e
3 changed files with 28 additions and 21 deletions

View File

@ -28,9 +28,10 @@
/******************************************************************************/ /******************************************************************************/
// because Safari if (vAPI.safari) {
if (location.protocol !== "http:" && location.protocol !== "https:") { if (!/^https?:/.test(location.protocol)) {
throw "uBlock> contentscript-end.js > Skipping page... " + location.protocol + location.host; throw "uBlock> contentscript-end.js > Skipping page: " + location.protocol + location.host;
}
} }
/******************************************************************************/ /******************************************************************************/

View File

@ -675,7 +675,7 @@ if (self.chrome) {
this.connector = function(request) { this.connector = function(request) {
var callback = function(response) { var callback = function(response) {
if (request.message.requestId && response !== undefined) { if (response !== undefined) {
request.target.page.dispatchMessage( request.target.page.dispatchMessage(
request.name, request.name,
{ {

View File

@ -65,7 +65,7 @@ if (self.chrome) {
if (this.port) { if (this.port) {
this.port.disconnect(); this.port.disconnect();
this.port.onMessage.removeListener(messagingConnector); this.port.onMessage.removeListener(messagingConnector);
this.port = this.channels = this.listeners = this.connectorId = null; this.port = this.channels = this.listeners = null;
} }
}, },
channel: function(channelName, callback) { channel: function(channelName, callback) {
@ -114,7 +114,7 @@ if (self.chrome) {
requestId: 0, requestId: 0,
connectorId: uniqueId(), connectorId: uniqueId(),
setup: function() { setup: function() {
this._connector = function(msg) { this.connector = function(msg) {
// messages from the background script are sent to every frame, // messages from the background script are sent to every frame,
// so we need to check the connectorId to accept only // so we need to check the connectorId to accept only
// what is meant for the current context // what is meant for the current context
@ -123,7 +123,7 @@ if (self.chrome) {
messagingConnector(msg.message); messagingConnector(msg.message);
} }
}; };
safari.self.addEventListener('message', this._connector, false); safari.self.addEventListener('message', this.connector, false);
this.channels['vAPI'] = { this.channels['vAPI'] = {
listener: function(msg) { listener: function(msg) {
@ -134,9 +134,9 @@ if (self.chrome) {
}; };
}, },
close: function() { close: function() {
if (this._connector) { if (this.connector) {
safari.self.removeEventListener('message', this._connector, false); safari.self.removeEventListener('message', this.connector, false);
this.channels = this.listeners = null; this.connector = this.channels = this.listeners = null;
} }
}, },
channel: function(channelName, callback) { channel: function(channelName, callback) {
@ -148,7 +148,7 @@ if (self.chrome) {
portName: channelName, portName: channelName,
listener: typeof callback === 'function' ? callback : null, listener: typeof callback === 'function' ? callback : null,
send: function(message, callback) { send: function(message, callback) {
if (!vAPI.messaging._connector) { if (!vAPI.messaging.connector) {
vAPI.messaging.setup(); vAPI.messaging.setup();
} }
@ -178,7 +178,10 @@ if (self.chrome) {
}); });
} }
else { else {
safari.self.tab.dispatchMessage(vAPI.messaging.connectorId, message); safari.self.tab.dispatchMessage(
vAPI.messaging.connectorId,
message
);
} }
}, },
close: function() { close: function() {
@ -197,6 +200,7 @@ if (self.chrome) {
var beforeLoadEvent = document.createEvent('Event'); var beforeLoadEvent = document.createEvent('Event');
beforeLoadEvent.initEvent('beforeload'); beforeLoadEvent.initEvent('beforeload');
var frameId = window === window.top ? 0 : Date.now() % 1E5;
var linkHelper = document.createElement('a'); var linkHelper = document.createElement('a');
var onBeforeLoad = function(e, details) { var onBeforeLoad = function(e, details) {
if (e.url && e.url.slice(0, 5) === 'data:') { if (e.url && e.url.slice(0, 5) === 'data:') {
@ -256,8 +260,8 @@ if (self.chrome) {
// tabId is determined in the background script // tabId is determined in the background script
// details.tabId = null; // details.tabId = null;
details.frameId = 0; details.frameId = frameId;
details.parentFrameId = window === window.top ? -1 : 0; details.parentFrameId = frameId ? 0 : -1;
details.timeStamp = Date.now(); details.timeStamp = Date.now();
var response = safari.self.tab.canLoad(e, details); var response = safari.self.tab.canLoad(e, details);
@ -265,11 +269,11 @@ if (self.chrome) {
if (!response) { if (!response) {
if (details.type === 'main_frame') { if (details.type === 'main_frame') {
window.stop(); window.stop();
throw new Error;
} }
else { else {
e.preventDefault(); e.preventDefault();
} }
return false; return false;
} }
// local mirroring, response is a data: URL here // local mirroring, response is a data: URL here
@ -333,8 +337,8 @@ if (self.chrome) {
var onContextMenu = function(e) { var onContextMenu = function(e) {
var details = { var details = {
tagName: e.target.tagName.toLowerCase(), tagName: e.target.tagName.toLowerCase(),
pageUrl: window.location.href, pageUrl: location.href,
insideFrame: window.top !== window insideFrame: window !== window.top
}; };
details.editable = details.tagName === 'textarea' || details.tagName === 'input'; details.editable = details.tagName === 'textarea' || details.tagName === 'input';
@ -364,10 +368,12 @@ if (self.chrome) {
self.addEventListener('contextmenu', onContextMenu, true); self.addEventListener('contextmenu', onContextMenu, true);
// 'main_frame' simulation // 'main_frame' simulation
onBeforeLoad(beforeLoadEvent, { if (frameId === 0) {
url: window.location.href, onBeforeLoad(beforeLoadEvent, {
type: 'main_frame' url: location.href,
}); type: 'main_frame'
});
}
// « // «
} }
// » footer // » footer