From 622d0a7e9dbd44c2d52d054829232fa3da18bf9d Mon Sep 17 00:00:00 2001 From: gorhill Date: Sun, 3 May 2015 07:10:05 -0400 Subject: [PATCH] provide iframe URL in placeholders --- src/js/contentscript-end.js | 50 +++++++++++++++++++++++++++------ src/js/messaging.js | 6 +++- src/js/start.js | 56 +++++++++++++++++++++++++------------ 3 files changed, 84 insertions(+), 28 deletions(-) diff --git a/src/js/contentscript-end.js b/src/js/contentscript-end.js index a2e786e..466caa2 100644 --- a/src/js/contentscript-end.js +++ b/src/js/contentscript-end.js @@ -157,10 +157,6 @@ var collapser = (function() { 'iframe': 'src', 'img': 'src' }; - var srcValues = { - 'iframe': 'about:blank', - 'img': 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7' - }; var PendingRequest = function(target) { this.id = requestId++; @@ -187,7 +183,8 @@ var collapser = (function() { return; } var collapse = response.collapse; - var bgImg = response.backgroundImage; + var placeholders = response.placeholders; + var background = placeholders.background; var i = requests.length; var request, entry, target, tagName; while ( i-- ) { @@ -206,9 +203,12 @@ var collapser = (function() { target.style.setProperty('display', 'none', 'important'); } else { tagName = target.localName; - target.setAttribute(srcProps[tagName], srcValues[tagName]); + target.setAttribute( + srcProps[tagName], + placeholders[tagName].replace('{{url}}', request.url) + ); target.style.setProperty('border', '1px solid rgba(0,0,0,0.05)', 'important'); - target.style.setProperty('background', bgImg, 'important'); + target.style.setProperty('background', background, 'important'); } } @@ -240,13 +240,45 @@ var collapser = (function() { } }; + var iframeSourceModified = function(mutations) { + var i = mutations.length; + while ( i-- ) { + addFrameNode(mutations[i].target, true); + } + process(); + }; + var iframeSourceObserver = new MutationObserver(iframeSourceModified); + var iframeSourceObserverOptions = { + attributes: true, + attributeFilter: [ 'src' ] + }; + + var addFrameNode = function(iframe, dontObserve) { + // https://github.com/gorhill/uBlock/issues/162 + // Be prepared to deal with possible change of src attribute. + if ( dontObserve !== true ) { + iframeSourceObserver.observe(iframe, iframeSourceObserverOptions); + } + // https://github.com/chrisaljoudi/uBlock/issues/174 + // Do not remove fragment from src URL + var src = iframe.src; + if ( src.lastIndexOf('http', 0) !== 0 ) { + return; + } + var req = new PendingRequest(iframe); + newRequests.push(new BouncingRequest(req.id, 'iframe', src)); + }; + var addNode = function(target) { var tagName = target.localName; + if ( tagName === 'iframe' ) { + addFrameNode(target); + return; + } var prop = srcProps[tagName]; if ( prop === undefined ) { return; } - // https://github.com/chrisaljoudi/uBlock/issues/174 // Do not remove fragment from src URL var src = target[prop]; @@ -462,7 +494,7 @@ var nodeListsAddedHandler = function(nodeLists) { }; // This fixes http://acid3.acidtests.org/ - if ( document.body ) { + if ( !document.body ) { return; } diff --git a/src/js/messaging.js b/src/js/messaging.js index ffec8ad..79bfffe 100644 --- a/src/js/messaging.js +++ b/src/js/messaging.js @@ -510,7 +510,11 @@ var onMessage = function(request, sender, callback) { requests: evaluateURLs(tabId, request.requests) }; if ( !response.collapse ) { - response.backgroundImage = vAPI.localStorage.getItem('placeholderBackgroundImage'); + response.placeholders = { + background: vAPI.localStorage.getItem('placeholderBackground'), + iframe: vAPI.localStorage.getItem('placeholderDocument'), + img: vAPI.localStorage.getItem('placeholderImage') + }; } break; diff --git a/src/js/start.js b/src/js/start.js index 84060d9..f5e79b2 100644 --- a/src/js/start.js +++ b/src/js/start.js @@ -62,6 +62,37 @@ var jobCallback = function() { /******************************************************************************/ +var defaultLocalUserSettings = { + placeholderBackground: [ + 'linear-gradient(0deg,', + 'rgba(0,0,0,0.02) 25%,', + 'rgba(0,0,0,0.05) 25%,', + 'rgba(0,0,0,0.05) 75%,', + 'rgba(0,0,0,0.02) 75%,', + 'rgba(0,0,0,0.02)', + ') center center / 10px 10px repeat scroll,', + 'linear-gradient(', + '90deg,', + 'rgba(0,0,0,0.02) 25%,', + 'rgba(0,0,0,0.05) 25%,', + 'rgba(0,0,0,0.05) 75%,', + 'rgba(0,0,0,0.02) 75%,', + 'rgba(0,0,0,0.02)', + ') center center / 10px 10px repeat scroll' + ].join(''), + placeholderDocument: [ + 'data:text/html,', + encodeURIComponent(''), + '{{url}}', + encodeURIComponent('') + ].join(''), + placeholderImage: 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7' +}; + +/******************************************************************************/ + var onAllDone = function() { µm.webRequest.start(); @@ -78,24 +109,13 @@ var onAllDone = function() { // for launch time. µm.assets.remoteFetchBarrier -= 1; - if ( vAPI.localStorage.getItem('placeholderBackgroundImage') === null ) { - vAPI.localStorage.setItem('placeholderBackgroundImage', [ - 'linear-gradient(0deg,', - 'rgba(0,0,0,0.02) 25%,', - 'rgba(0,0,0,0.05) 25%,', - 'rgba(0,0,0,0.05) 75%,', - 'rgba(0,0,0,0.02) 75%,', - 'rgba(0,0,0,0.02)', - ') center center / 10px 10px repeat scroll,', - 'linear-gradient(', - '90deg,', - 'rgba(0,0,0,0.02) 25%,', - 'rgba(0,0,0,0.05) 25%,', - 'rgba(0,0,0,0.05) 75%,', - 'rgba(0,0,0,0.02) 75%,', - 'rgba(0,0,0,0.02)', - ') center center / 10px 10px repeat scroll' - ].join('')); + for ( var key in defaultLocalUserSettings ) { + if ( defaultLocalUserSettings.hasOwnProperty(key) === false ) { + continue; + } + if ( vAPI.localStorage.getItem(key) === null ) { + vAPI.localStorage.setItem(key, defaultLocalUserSettings[key]); + } } };