Skip to content
This repository has been archived by the owner on Sep 9, 2022. It is now read-only.

Commit

Permalink
this fixes #510
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed Mar 20, 2015
1 parent 4588297 commit 12d2ab5
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 70 deletions.
150 changes: 84 additions & 66 deletions platform/chromium/vapi-background.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,92 @@ vAPI.noTabId = '-1';

/******************************************************************************/

var onCreatedNavigationTarget = function(details) {
vAPI.tabs.onPopup({
openerTabId: details.sourceTabId,
openerURL: '',
targetURL: details.url,
targetTabId: details.tabId
});
};
vAPI.tabs.registerListeners = function() {
var onNavigationClient = this.onNavigation || noopFunc;
var onPopupClient = this.onPopup || noopFunc;

// https://developer.chrome.com/extensions/webNavigation
// [onCreatedNavigationTarget ->]
// onBeforeNavigate ->
// onCommitted ->
// onDOMContentLoaded ->
// onCompleted

var popupCandidates = Object.create(null);

var PopupCandidate = function(details) {
this.targetTabId = details.tabId;
this.openerTabId = details.sourceTabId;
this.targetURL = details.url;
this.selfDestructionTimer = null;
};

/******************************************************************************/
PopupCandidate.prototype.selfDestruct = function() {
if ( this.selfDestructionTimer !== null ) {
clearTimeout(this.selfDestructionTimer);
}
delete popupCandidates[this.targetTabId];
};

vAPI.tabs.registerListeners = function() {
if ( typeof this.onNavigation === 'function' ) {
chrome.webNavigation.onCommitted.addListener(this.onNavigation);
}
PopupCandidate.prototype.launchSelfDestruction = function() {
if ( this.selfDestructionTimer !== null ) {
clearTimeout(this.selfDestructionTimer);
}
this.selfDestructionTimer = setTimeout(this.selfDestruct.bind(this), 1000);
};

var popupCandidateCreate = function(details) {
var popup = popupCandidates[details.tabId];
// This really should not happen...
if ( popup !== undefined ) {
return;
}
return popupCandidates[details.tabId] = new PopupCandidate(details);
};

var popupCandidateTest = function(details) {
var popup = popupCandidates[details.tabId];
if ( popup === undefined ) {
return;
}
popup.targetURL = details.url;
if ( onPopupClient(popup) !== true ) {
return;
}
popup.selfDestruct();
return true;
};

var popupCandidateDestroy = function(details) {
var popup = popupCandidates[details.tabId];
if ( popup instanceof PopupCandidate ) {
popup.launchSelfDestruction();
}
};

var onCreatedNavigationTarget = function(details) {
//console.debug('onCreatedNavigationTarget: popup candidate', details.tabId);
popupCandidateCreate(details);
popupCandidateTest(details);
};

var onBeforeNavigate = function(details) {
//console.debug('onBeforeNavigate: popup candidate', details.tabId);
popupCandidateTest(details);
};

var onCommitted = function(details) {
//console.debug('onCommitted: popup candidate', details.tabId);
if ( popupCandidateTest(details) === true ) {
return;
}
popupCandidateDestroy(details);
onNavigationClient(details);
};

chrome.webNavigation.onCreatedNavigationTarget.addListener(onCreatedNavigationTarget);
chrome.webNavigation.onBeforeNavigate.addListener(onBeforeNavigate);
chrome.webNavigation.onCommitted.addListener(onCommitted);

if ( typeof this.onUpdated === 'function' ) {
chrome.tabs.onUpdated.addListener(this.onUpdated);
Expand All @@ -95,9 +166,6 @@ vAPI.tabs.registerListeners = function() {
chrome.tabs.onRemoved.addListener(this.onClosed);
}

if ( typeof this.onPopup === 'function' ) {
chrome.webNavigation.onCreatedNavigationTarget.addListener(onCreatedNavigationTarget);
}
};

/******************************************************************************/
Expand Down Expand Up @@ -521,56 +589,6 @@ vAPI.net.registerListeners = function() {
},
this.onHeadersReceived.extra
);

// Intercept root frame requests.
// This is where we identify and block popups early, whenever possible.

var onBeforeSendHeaders = function(details) {
// Do not block behind the scene requests.
if ( vAPI.isNoTabId(details.tabId) ) {
return;
}

// Only root document.
if ( details.parentFrameId !== -1 ) {
return;
}

var referrer = headerValue(details.requestHeaders, 'referer');
if ( referrer === '' ) {
return;
}

var result = vAPI.tabs.onPopup({
openerTabId: undefined,
openerURL: referrer,
targetTabId: details.tabId,
targetURL: details.url
});

if ( result ) {
return { 'cancel': true };
}
};

var headerValue = function(headers, name) {
var i = headers.length;
while ( i-- ) {
if ( headers[i].name.toLowerCase() === name ) {
return headers[i].value;
}
}
return '';
};

chrome.webRequest.onBeforeSendHeaders.addListener(
onBeforeSendHeaders,
{
'urls': [ 'http://*/*', 'https://*/*' ],
'types': [ 'main_frame' ]
},
[ 'blocking', 'requestHeaders' ]
);
};

/******************************************************************************/
Expand Down
22 changes: 18 additions & 4 deletions src/js/devtool-log.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

(function() {

'use strict';

/******************************************************************************/

var messager = vAPI.messaging.channel('devtool-log.js');
Expand All @@ -49,9 +51,19 @@ var prettyRequestTypes = {

/******************************************************************************/

var escapeHTML = function(s) {
return s.replace(reEscapeLeftBracket, '<')
.replace(reEscapeRightBracket, '>');
};

var reEscapeLeftBracket = /</g;
var reEscapeRightBracket = />/g;

/******************************************************************************/

var renderURL = function(url, filter) {
if ( filter.charAt(0) !== 's' ) {
return url;
return escapeHTML(url);
}
// make a regex out of the filter
var reText = filter.slice(3);
Expand Down Expand Up @@ -79,11 +91,13 @@ var renderURL = function(url, filter) {
var renderedURL = url;

if ( matches && matches[0].length ) {
renderedURL = url.slice(0, matches.index) +
renderedURL = escapeHTML(url.slice(0, matches.index)) +
'<b>' +
url.slice(matches.index, re.lastIndex) +
escapeHTML(url.slice(matches.index, re.lastIndex)) +
'</b>' +
url.slice(re.lastIndex);
escapeHTML(url.slice(re.lastIndex));
} else {
renderedURL = escapeHTML(renderedURL);
}

return renderedURL;
Expand Down

0 comments on commit 12d2ab5

Please sign in to comment.