Browse Source

Only top window should have type `main_frame`

pull/2/head
Deathamns 10 years ago
committed by gorhill
parent
commit
d9c9627758
  1. 34
      src/js/vapi-client.js

34
src/js/vapi-client.js

@ -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) {
safari.self.removeEventListener('message', this._connector, false);
this.channels = this.listeners = null;
if (this.connector) {
safari.self.removeEventListener('message', this.connector, false);
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.parentFrameId = window === window.top ? -1 : 0;
details.frameId = frameId;
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,
insideFrame: window.top !== window
pageUrl: location.href,
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
if (frameId === 0) {
onBeforeLoad(beforeLoadEvent, { onBeforeLoad(beforeLoadEvent, {
url: window.location.href,
url: location.href,
type: 'main_frame' type: 'main_frame'
}); });
}
// « // «
} }
// » footer // » footer

Loading…
Cancel
Save