2014-06-24 00:42:43 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2016-03-07 15:55:04 +01:00
|
|
|
uBlock Origin - a browser extension to block requests.
|
2018-07-22 16:47:02 +02:00
|
|
|
Copyright (C) 2014-present Raymond Hill
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
2016-07-01 04:03:29 +02:00
|
|
|
'use strict';
|
|
|
|
|
2014-06-24 00:42:43 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
A PageRequestStore object is used to store net requests in two ways:
|
|
|
|
|
|
|
|
To record distinct net requests
|
|
|
|
To create a log of net requests
|
|
|
|
|
|
|
|
**/
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
µBlock.PageStore = (function() {
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const µb = µBlock;
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2014-09-14 22:20:40 +02:00
|
|
|
/******************************************************************************/
|
2014-06-24 00:42:43 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-09-14 22:20:40 +02:00
|
|
|
// To mitigate memory churning
|
2018-12-13 18:30:54 +01:00
|
|
|
const netFilteringCacheJunkyard = [];
|
2018-12-21 20:16:17 +01:00
|
|
|
const netFilteringCacheJunkyardMax = 10;
|
2014-09-14 22:20:40 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const NetFilteringResultCache = function() {
|
2017-08-03 16:18:05 +02:00
|
|
|
this.init();
|
2014-09-14 22:20:40 +02:00
|
|
|
};
|
|
|
|
|
2018-12-21 20:16:17 +01:00
|
|
|
NetFilteringResultCache.prototype = {
|
|
|
|
shelfLife: 15000,
|
|
|
|
|
|
|
|
init: function() {
|
|
|
|
this.blocked = new Map();
|
|
|
|
this.results = new Map();
|
|
|
|
this.hash = 0;
|
|
|
|
this.timer = undefined;
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
dispose: function() {
|
|
|
|
this.empty();
|
|
|
|
if ( netFilteringCacheJunkyard.length < netFilteringCacheJunkyardMax ) {
|
|
|
|
netFilteringCacheJunkyard.push(this);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
2014-09-14 22:20:40 +02:00
|
|
|
|
2018-12-21 20:16:17 +01:00
|
|
|
rememberResult: function(fctxt, result) {
|
|
|
|
if ( fctxt.tabId <= 0 ) { return; }
|
|
|
|
if ( this.results.size === 0 ) {
|
|
|
|
this.pruneAsync();
|
2017-08-03 16:18:05 +02:00
|
|
|
}
|
2018-12-21 20:16:17 +01:00
|
|
|
const key = fctxt.getDocHostname() + ' ' + fctxt.type + ' ' + fctxt.url;
|
|
|
|
this.results.set(key, {
|
|
|
|
result: result,
|
|
|
|
logData: fctxt.filter,
|
|
|
|
tstamp: Date.now()
|
|
|
|
});
|
|
|
|
if ( result !== 1 ) { return; }
|
|
|
|
const now = Date.now();
|
|
|
|
this.blocked.set(key, now);
|
|
|
|
this.hash = now;
|
|
|
|
},
|
|
|
|
|
|
|
|
rememberBlock: function(fctxt) {
|
|
|
|
if ( fctxt.tabId <= 0 ) { return; }
|
|
|
|
if ( this.blocked.size === 0 ) {
|
|
|
|
this.pruneAsync();
|
2014-07-30 07:05:35 +02:00
|
|
|
}
|
2018-12-21 20:16:17 +01:00
|
|
|
const now = Date.now();
|
|
|
|
this.blocked.set(
|
|
|
|
fctxt.getDocHostname() + ' ' + fctxt.type + ' ' + fctxt.url,
|
|
|
|
now
|
|
|
|
);
|
|
|
|
this.hash = now;
|
|
|
|
},
|
|
|
|
|
|
|
|
empty: function() {
|
|
|
|
this.blocked.clear();
|
|
|
|
this.results.clear();
|
|
|
|
this.hash = 0;
|
|
|
|
if ( this.timer !== undefined ) {
|
|
|
|
clearTimeout(this.timer);
|
|
|
|
this.timer = undefined;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
prune: function() {
|
|
|
|
const obsolete = Date.now() - this.shelfLife;
|
|
|
|
for ( const entry of this.blocked ) {
|
|
|
|
if ( entry[1] <= obsolete ) {
|
|
|
|
this.results.delete(entry[0]);
|
|
|
|
this.blocked.delete(entry[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for ( const entry of this.results ) {
|
|
|
|
if ( entry[1].tstamp <= obsolete ) {
|
|
|
|
this.results.delete(entry[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( this.blocked.size !== 0 || this.results.size !== 0 ) {
|
|
|
|
this.pruneAsync();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
pruneAsync: function() {
|
|
|
|
if ( this.timer !== undefined ) { return; }
|
|
|
|
this.timer = vAPI.setTimeout(
|
|
|
|
( ) => {
|
|
|
|
this.timer = undefined;
|
|
|
|
this.prune();
|
|
|
|
},
|
|
|
|
this.shelfLife
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
lookupResult: function(fctxt) {
|
|
|
|
return this.results.get(
|
|
|
|
fctxt.getDocHostname() + ' ' +
|
|
|
|
fctxt.type + ' ' +
|
|
|
|
fctxt.url
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
lookupAllBlocked: function(hostname) {
|
|
|
|
const result = [];
|
|
|
|
for ( const entry of this.blocked ) {
|
|
|
|
const pos = entry[0].indexOf(' ');
|
|
|
|
if ( entry[0].slice(0, pos) === hostname ) {
|
|
|
|
result[result.length] = entry[0].slice(pos + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
},
|
2014-09-14 22:20:40 +02:00
|
|
|
};
|
|
|
|
|
2018-12-21 20:16:17 +01:00
|
|
|
NetFilteringResultCache.factory = function() {
|
|
|
|
const entry = netFilteringCacheJunkyard.pop();
|
|
|
|
return entry !== undefined
|
|
|
|
? entry.init()
|
|
|
|
: new NetFilteringResultCache();
|
2014-09-14 22:20:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-04-09 00:46:08 +02:00
|
|
|
// Frame stores are used solely to associate a URL with a frame id. The
|
|
|
|
// name `pageHostname` is used because of historical reasons. A more
|
|
|
|
// appropriate name is `frameHostname` -- something to do in a future
|
|
|
|
// refactoring.
|
|
|
|
|
2014-09-14 22:20:40 +02:00
|
|
|
// To mitigate memory churning
|
2018-12-13 18:30:54 +01:00
|
|
|
const frameStoreJunkyard = [];
|
|
|
|
const frameStoreJunkyardMax = 50;
|
2014-09-14 22:20:40 +02:00
|
|
|
|
2014-07-30 07:05:35 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const FrameStore = function(frameURL) {
|
2016-03-07 15:55:04 +01:00
|
|
|
this.init(frameURL);
|
2014-07-30 07:05:35 +02:00
|
|
|
};
|
|
|
|
|
2018-12-21 20:16:17 +01:00
|
|
|
FrameStore.prototype = {
|
|
|
|
init: function(frameURL) {
|
|
|
|
const µburi = µb.URI;
|
|
|
|
this.pageHostname = µburi.hostnameFromURI(frameURL);
|
|
|
|
this.pageDomain =
|
|
|
|
µburi.domainFromHostname(this.pageHostname) || this.pageHostname;
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
dispose: function() {
|
|
|
|
this.pageHostname = this.pageDomain = '';
|
|
|
|
if ( frameStoreJunkyard.length < frameStoreJunkyardMax ) {
|
|
|
|
frameStoreJunkyard.push(this);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
};
|
2014-07-30 07:05:35 +02:00
|
|
|
|
2016-03-07 15:55:04 +01:00
|
|
|
FrameStore.factory = function(frameURL) {
|
2018-12-13 18:30:54 +01:00
|
|
|
const entry = frameStoreJunkyard.pop();
|
2014-09-14 22:20:40 +02:00
|
|
|
if ( entry === undefined ) {
|
2016-03-07 15:55:04 +01:00
|
|
|
return new FrameStore(frameURL);
|
2014-09-14 22:20:40 +02:00
|
|
|
}
|
2016-03-07 15:55:04 +01:00
|
|
|
return entry.init(frameURL);
|
2014-09-14 22:20:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2014-07-30 07:05:35 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-09-14 22:20:40 +02:00
|
|
|
// To mitigate memory churning
|
2018-12-13 18:30:54 +01:00
|
|
|
const pageStoreJunkyard = [];
|
|
|
|
const pageStoreJunkyardMax = 10;
|
2014-09-14 22:20:40 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const PageStore = function(tabId, context) {
|
2019-05-11 16:40:34 +02:00
|
|
|
this.extraData = new Map();
|
2016-10-08 16:15:31 +02:00
|
|
|
this.journal = [];
|
|
|
|
this.journalTimer = null;
|
|
|
|
this.journalLastCommitted = this.journalLastUncommitted = undefined;
|
|
|
|
this.journalLastUncommittedURL = undefined;
|
2019-05-11 16:40:34 +02:00
|
|
|
this.init(tabId, context);
|
2014-07-30 07:05:35 +02:00
|
|
|
};
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2017-11-07 17:31:19 +01:00
|
|
|
PageStore.factory = function(tabId, context) {
|
2018-12-13 18:30:54 +01:00
|
|
|
let entry = pageStoreJunkyard.pop();
|
2014-09-14 22:20:40 +02:00
|
|
|
if ( entry === undefined ) {
|
2017-11-07 17:31:19 +01:00
|
|
|
entry = new PageStore(tabId, context);
|
2014-09-14 22:20:40 +02:00
|
|
|
} else {
|
2017-11-07 17:31:19 +01:00
|
|
|
entry.init(tabId, context);
|
2014-09-14 22:20:40 +02:00
|
|
|
}
|
|
|
|
return entry;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2017-11-07 17:31:19 +01:00
|
|
|
// https://github.com/gorhill/uBlock/issues/3201
|
|
|
|
// The context is used to determine whether we report behavior change to the
|
|
|
|
// logger.
|
|
|
|
|
|
|
|
PageStore.prototype.init = function(tabId, context) {
|
2018-12-06 16:18:19 +01:00
|
|
|
const tabContext = µb.tabContextManager.mustLookup(tabId);
|
2014-06-24 00:42:43 +02:00
|
|
|
this.tabId = tabId;
|
2016-01-17 19:30:43 +01:00
|
|
|
|
|
|
|
// If we are navigating from-to same site, remember whether large
|
|
|
|
// media elements were temporarily allowed.
|
|
|
|
if (
|
|
|
|
typeof this.allowLargeMediaElementsUntil !== 'number' ||
|
|
|
|
tabContext.rootHostname !== this.tabHostname
|
|
|
|
) {
|
|
|
|
this.allowLargeMediaElementsUntil = 0;
|
|
|
|
}
|
|
|
|
|
2015-04-09 00:46:08 +02:00
|
|
|
this.tabHostname = tabContext.rootHostname;
|
2015-05-16 16:15:02 +02:00
|
|
|
this.title = tabContext.rawURL;
|
|
|
|
this.rawURL = tabContext.rawURL;
|
2016-10-08 16:15:31 +02:00
|
|
|
this.hostnameToCountMap = new Map();
|
2015-01-10 17:23:28 +01:00
|
|
|
this.contentLastModified = 0;
|
2018-02-26 19:59:16 +01:00
|
|
|
this.frames = new Map();
|
2017-05-12 16:35:11 +02:00
|
|
|
this.logData = undefined;
|
2014-07-14 17:24:59 +02:00
|
|
|
this.perLoadBlockedRequestCount = 0;
|
|
|
|
this.perLoadAllowedRequestCount = 0;
|
2015-04-05 18:03:14 +02:00
|
|
|
this.hiddenElementCount = ''; // Empty string means "unknown"
|
2015-06-10 15:23:48 +02:00
|
|
|
this.remoteFontCount = 0;
|
2018-09-01 00:47:02 +02:00
|
|
|
this.scriptCount = 0;
|
2016-01-04 16:48:28 +01:00
|
|
|
this.popupBlockedCount = 0;
|
2016-01-17 19:30:43 +01:00
|
|
|
this.largeMediaCount = 0;
|
|
|
|
this.largeMediaTimer = null;
|
2014-09-14 22:20:40 +02:00
|
|
|
this.netFilteringCache = NetFilteringResultCache.factory();
|
2016-10-14 16:06:34 +02:00
|
|
|
this.internalRedirectionCount = 0;
|
2019-05-11 16:40:34 +02:00
|
|
|
this.extraData.clear();
|
2015-01-06 14:01:15 +01:00
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
// The current filtering context is cloned because:
|
|
|
|
// - We may be called with or without the current context having been
|
|
|
|
// initialized.
|
|
|
|
// - If it has been initialized, we do not want to change the state
|
|
|
|
// of the current context.
|
|
|
|
const fctxt = µb.logger.enabled
|
|
|
|
? µBlock.filteringContext
|
|
|
|
.duplicate()
|
|
|
|
.fromTabId(tabId)
|
|
|
|
.setURL(tabContext.rawURL)
|
|
|
|
: undefined;
|
|
|
|
|
2018-12-06 16:18:19 +01:00
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/314
|
|
|
|
const masterSwitch = tabContext.getNetFilteringSwitch();
|
|
|
|
|
2018-09-03 20:06:49 +02:00
|
|
|
this.noCosmeticFiltering = µb.sessionSwitches.evaluateZ(
|
2017-11-07 17:31:19 +01:00
|
|
|
'no-cosmetic-filtering',
|
|
|
|
tabContext.rootHostname
|
|
|
|
) === true;
|
|
|
|
if (
|
2018-12-06 16:18:19 +01:00
|
|
|
masterSwitch &&
|
2017-11-07 17:31:19 +01:00
|
|
|
this.noCosmeticFiltering &&
|
2018-12-13 18:30:54 +01:00
|
|
|
µb.logger.enabled &&
|
2017-11-07 17:31:19 +01:00
|
|
|
context === 'tabCommitted'
|
|
|
|
) {
|
2018-12-13 18:30:54 +01:00
|
|
|
fctxt.setRealm('cosmetic')
|
|
|
|
.setType('dom')
|
|
|
|
.setFilter(µb.sessionSwitches.toLogData())
|
|
|
|
.toLogger();
|
2015-06-09 16:27:08 +02:00
|
|
|
}
|
2016-08-12 14:55:35 +02:00
|
|
|
|
2014-07-14 17:24:59 +02:00
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-04-09 00:46:08 +02:00
|
|
|
PageStore.prototype.reuse = function(context) {
|
2015-04-09 17:19:31 +02:00
|
|
|
// When force refreshing a page, the page store data needs to be reset.
|
2015-03-22 01:30:00 +01:00
|
|
|
|
2015-03-26 00:28:22 +01:00
|
|
|
// If the hostname changes, we can't merely just update the context.
|
2018-12-13 18:30:54 +01:00
|
|
|
const tabContext = µb.tabContextManager.mustLookup(this.tabId);
|
2015-04-09 00:46:08 +02:00
|
|
|
if ( tabContext.rootHostname !== this.tabHostname ) {
|
2015-03-26 00:28:22 +01:00
|
|
|
context = '';
|
|
|
|
}
|
|
|
|
|
2014-10-17 21:44:19 +02:00
|
|
|
// If URL changes without a page reload (more and more common), then we
|
2014-10-02 22:45:26 +02:00
|
|
|
// need to keep all that we collected for reuse. In particular, not
|
|
|
|
// doing so was causing a problem in `videos.foxnews.com`: clicking a
|
|
|
|
// video thumbnail would not work, because the frame hierarchy structure
|
|
|
|
// was flushed from memory, while not really being flushed on the page.
|
|
|
|
if ( context === 'tabUpdated' ) {
|
2015-04-07 03:26:05 +02:00
|
|
|
// As part of https://github.com/chrisaljoudi/uBlock/issues/405
|
2014-12-08 18:37:35 +01:00
|
|
|
// URL changed, force a re-evaluation of filtering switch
|
2015-05-16 16:15:02 +02:00
|
|
|
this.rawURL = tabContext.rawURL;
|
2014-10-02 22:45:26 +02:00
|
|
|
return this;
|
|
|
|
}
|
2015-04-09 00:46:08 +02:00
|
|
|
|
2014-10-02 22:45:26 +02:00
|
|
|
// A new page is completely reloaded from scratch, reset all.
|
2016-01-17 19:30:43 +01:00
|
|
|
if ( this.largeMediaTimer !== null ) {
|
|
|
|
clearTimeout(this.largeMediaTimer);
|
|
|
|
this.largeMediaTimer = null;
|
|
|
|
}
|
2014-09-14 22:20:40 +02:00
|
|
|
this.disposeFrameStores();
|
|
|
|
this.netFilteringCache = this.netFilteringCache.dispose();
|
2017-11-07 17:31:19 +01:00
|
|
|
this.init(this.tabId, context);
|
2014-06-24 00:42:43 +02:00
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2014-09-14 22:20:40 +02:00
|
|
|
// https://www.youtube.com/watch?v=dltNSbOupgE
|
|
|
|
|
2014-06-24 00:42:43 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
PageStore.prototype.dispose = function() {
|
2015-05-16 16:15:02 +02:00
|
|
|
this.tabHostname = '';
|
|
|
|
this.title = '';
|
|
|
|
this.rawURL = '';
|
2014-12-30 22:36:29 +01:00
|
|
|
this.hostnameToCountMap = null;
|
2016-01-17 19:30:43 +01:00
|
|
|
this.allowLargeMediaElementsUntil = 0;
|
|
|
|
if ( this.largeMediaTimer !== null ) {
|
|
|
|
clearTimeout(this.largeMediaTimer);
|
|
|
|
this.largeMediaTimer = null;
|
|
|
|
}
|
2014-09-14 22:20:40 +02:00
|
|
|
this.disposeFrameStores();
|
|
|
|
this.netFilteringCache = this.netFilteringCache.dispose();
|
2016-10-08 16:15:31 +02:00
|
|
|
if ( this.journalTimer !== null ) {
|
|
|
|
clearTimeout(this.journalTimer);
|
|
|
|
this.journalTimer = null;
|
|
|
|
}
|
|
|
|
this.journal = [];
|
|
|
|
this.journalLastUncommittedURL = undefined;
|
2014-09-14 22:20:40 +02:00
|
|
|
if ( pageStoreJunkyard.length < pageStoreJunkyardMax ) {
|
2014-06-24 00:42:43 +02:00
|
|
|
pageStoreJunkyard.push(this);
|
|
|
|
}
|
2014-09-14 22:20:40 +02:00
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
PageStore.prototype.disposeFrameStores = function() {
|
2018-12-13 18:30:54 +01:00
|
|
|
for ( const frameStore of this.frames.values() ) {
|
2018-02-26 19:59:16 +01:00
|
|
|
frameStore.dispose();
|
2014-09-14 22:20:40 +02:00
|
|
|
}
|
2018-02-26 19:59:16 +01:00
|
|
|
this.frames.clear();
|
2014-06-24 00:42:43 +02:00
|
|
|
};
|
|
|
|
|
2015-02-25 20:15:36 +01:00
|
|
|
PageStore.prototype.getFrame = function(frameId) {
|
2018-02-26 19:59:16 +01:00
|
|
|
return this.frames.get(frameId) || null;
|
2014-07-30 07:05:35 +02:00
|
|
|
};
|
|
|
|
|
2015-02-25 20:15:36 +01:00
|
|
|
PageStore.prototype.setFrame = function(frameId, frameURL) {
|
2018-12-13 18:30:54 +01:00
|
|
|
const frameStore = this.frames.get(frameId);
|
2018-02-26 19:59:16 +01:00
|
|
|
if ( frameStore !== undefined ) {
|
2016-03-07 15:55:04 +01:00
|
|
|
frameStore.init(frameURL);
|
2015-02-25 20:15:36 +01:00
|
|
|
} else {
|
2018-02-26 19:59:16 +01:00
|
|
|
this.frames.set(frameId, FrameStore.factory(frameURL));
|
2015-02-25 20:15:36 +01:00
|
|
|
}
|
2014-07-30 07:05:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-08-02 17:40:27 +02:00
|
|
|
PageStore.prototype.getNetFilteringSwitch = function() {
|
2015-12-02 06:59:51 +01:00
|
|
|
return µb.tabContextManager.mustLookup(this.tabId).getNetFilteringSwitch();
|
2014-08-02 17:40:27 +02:00
|
|
|
};
|
|
|
|
|
2015-02-06 05:14:12 +01:00
|
|
|
PageStore.prototype.getSpecificCosmeticFilteringSwitch = function() {
|
2016-08-12 14:55:35 +02:00
|
|
|
return this.noCosmeticFiltering !== true;
|
2015-02-06 05:14:12 +01:00
|
|
|
};
|
|
|
|
|
2015-01-22 03:46:11 +01:00
|
|
|
PageStore.prototype.toggleNetFilteringSwitch = function(url, scope, state) {
|
|
|
|
µb.toggleNetFilteringSwitch(url, scope, state);
|
|
|
|
this.netFilteringCache.empty();
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-11-08 21:53:08 +01:00
|
|
|
PageStore.prototype.injectLargeMediaElementScriptlet = function() {
|
2019-04-24 14:30:54 +02:00
|
|
|
vAPI.tabs.injectScript(this.tabId, {
|
|
|
|
file: '/js/scriptlets/load-large-media-interactive.js',
|
|
|
|
allFrames: true,
|
|
|
|
runAt: 'document_idle',
|
|
|
|
});
|
2016-11-08 21:53:08 +01:00
|
|
|
µb.contextMenu.update(this.tabId);
|
|
|
|
};
|
2016-01-17 19:30:43 +01:00
|
|
|
|
2017-09-12 17:43:43 +02:00
|
|
|
PageStore.prototype.temporarilyAllowLargeMediaElements = function(state) {
|
2016-01-17 19:30:43 +01:00
|
|
|
this.largeMediaCount = 0;
|
|
|
|
µb.contextMenu.update(this.tabId);
|
2017-09-12 17:43:43 +02:00
|
|
|
this.allowLargeMediaElementsUntil = state ? Date.now() + 86400000 : 0;
|
2016-01-17 19:30:43 +01:00
|
|
|
µb.scriptlets.injectDeep(this.tabId, 'load-large-media-all');
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-10-08 16:15:31 +02:00
|
|
|
// https://github.com/gorhill/uBlock/issues/2053
|
|
|
|
// There is no way around using journaling to ensure we deal properly with
|
|
|
|
// potentially out of order navigation events vs. network request events.
|
|
|
|
|
|
|
|
PageStore.prototype.journalAddRequest = function(hostname, result) {
|
|
|
|
if ( hostname === '' ) { return; }
|
|
|
|
this.journal.push(
|
|
|
|
hostname,
|
2017-05-12 16:35:11 +02:00
|
|
|
result === 1 ? 0x00000001 : 0x00010000
|
2016-10-08 16:15:31 +02:00
|
|
|
);
|
|
|
|
if ( this.journalTimer === null ) {
|
2018-09-01 00:47:02 +02:00
|
|
|
this.journalTimer = vAPI.setTimeout(
|
|
|
|
( ) => { this.journalProcess(true); },
|
|
|
|
µb.hiddenSettings.requestJournalProcessPeriod
|
|
|
|
);
|
2016-10-08 16:15:31 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
PageStore.prototype.journalAddRootFrame = function(type, url) {
|
|
|
|
if ( type === 'committed' ) {
|
|
|
|
this.journalLastCommitted = this.journal.length;
|
|
|
|
if (
|
|
|
|
this.journalLastUncommitted !== undefined &&
|
|
|
|
this.journalLastUncommitted < this.journalLastCommitted &&
|
|
|
|
this.journalLastUncommittedURL === url
|
|
|
|
) {
|
|
|
|
this.journalLastCommitted = this.journalLastUncommitted;
|
|
|
|
this.journalLastUncommitted = undefined;
|
|
|
|
}
|
|
|
|
} else if ( type === 'uncommitted' ) {
|
|
|
|
this.journalLastUncommitted = this.journal.length;
|
|
|
|
this.journalLastUncommittedURL = url;
|
|
|
|
}
|
|
|
|
if ( this.journalTimer !== null ) {
|
|
|
|
clearTimeout(this.journalTimer);
|
|
|
|
}
|
2018-09-01 00:47:02 +02:00
|
|
|
this.journalTimer = vAPI.setTimeout(
|
|
|
|
( ) => { this.journalProcess(true); },
|
|
|
|
µb.hiddenSettings.requestJournalProcessPeriod
|
|
|
|
);
|
2016-10-08 16:15:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
PageStore.prototype.journalProcess = function(fromTimer) {
|
|
|
|
if ( !fromTimer ) {
|
|
|
|
clearTimeout(this.journalTimer);
|
|
|
|
}
|
|
|
|
this.journalTimer = null;
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const journal = this.journal;
|
|
|
|
const now = Date.now();
|
|
|
|
let aggregateCounts = 0;
|
|
|
|
let pivot = this.journalLastCommitted || 0;
|
2016-10-08 16:15:31 +02:00
|
|
|
|
|
|
|
// Everything after pivot originates from current page.
|
2018-12-13 18:30:54 +01:00
|
|
|
for ( let i = pivot; i < journal.length; i += 2 ) {
|
|
|
|
const hostname = journal[i];
|
2018-09-01 00:47:02 +02:00
|
|
|
let hostnameCounts = this.hostnameToCountMap.get(hostname);
|
2016-10-08 16:15:31 +02:00
|
|
|
if ( hostnameCounts === undefined ) {
|
|
|
|
hostnameCounts = 0;
|
|
|
|
this.contentLastModified = now;
|
|
|
|
}
|
2018-09-01 00:47:02 +02:00
|
|
|
let count = journal[i+1];
|
2016-10-08 16:15:31 +02:00
|
|
|
this.hostnameToCountMap.set(hostname, hostnameCounts + count);
|
|
|
|
aggregateCounts += count;
|
|
|
|
}
|
|
|
|
this.perLoadBlockedRequestCount += aggregateCounts & 0xFFFF;
|
|
|
|
this.perLoadAllowedRequestCount += aggregateCounts >>> 16 & 0xFFFF;
|
|
|
|
this.journalLastCommitted = undefined;
|
|
|
|
|
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/905#issuecomment-76543649
|
|
|
|
// No point updating the badge if it's not being displayed.
|
|
|
|
if ( (aggregateCounts & 0xFFFF) && µb.userSettings.showIconBadge ) {
|
2018-05-08 15:43:25 +02:00
|
|
|
µb.updateToolbarIcon(this.tabId, 0x02);
|
2016-10-08 16:15:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Everything before pivot does not originate from current page -- we still
|
|
|
|
// need to bump global blocked/allowed counts.
|
2018-09-01 00:47:02 +02:00
|
|
|
for ( let i = 0; i < pivot; i += 2 ) {
|
2016-10-08 16:15:31 +02:00
|
|
|
aggregateCounts += journal[i+1];
|
|
|
|
}
|
|
|
|
if ( aggregateCounts !== 0 ) {
|
|
|
|
µb.localSettings.blockedRequestCount += aggregateCounts & 0xFFFF;
|
|
|
|
µb.localSettings.allowedRequestCount += aggregateCounts >>> 16 & 0xFFFF;
|
|
|
|
µb.localSettingsLastModified = now;
|
|
|
|
}
|
|
|
|
journal.length = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
PageStore.prototype.filterRequest = function(fctxt) {
|
|
|
|
fctxt.filter = undefined;
|
2017-05-12 16:35:11 +02:00
|
|
|
|
2017-08-03 16:18:05 +02:00
|
|
|
if ( this.getNetFilteringSwitch() === false ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const requestType = fctxt.type;
|
2015-01-24 18:06:22 +01:00
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
if ( requestType === 'csp_report' && this.filterCSPReport(fctxt) === 1 ) {
|
2017-10-19 15:35:28 +02:00
|
|
|
return 1;
|
2016-10-14 16:06:34 +02:00
|
|
|
}
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
if ( requestType.endsWith('font') && this.filterFont(fctxt) === 1 ) {
|
2017-10-19 15:35:28 +02:00
|
|
|
return 1;
|
2014-06-24 00:42:43 +02:00
|
|
|
}
|
2014-12-28 16:07:43 +01:00
|
|
|
|
2018-09-01 12:36:17 +02:00
|
|
|
if (
|
|
|
|
requestType === 'script' &&
|
2018-12-13 18:30:54 +01:00
|
|
|
this.filterScripting(fctxt, true) === 1
|
2018-09-01 12:36:17 +02:00
|
|
|
) {
|
|
|
|
return 1;
|
2018-09-01 00:47:02 +02:00
|
|
|
}
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const cacheableResult = this.cacheableResults.has(requestType);
|
2017-08-03 16:18:05 +02:00
|
|
|
|
|
|
|
if ( cacheableResult ) {
|
2018-12-13 18:30:54 +01:00
|
|
|
const entry = this.netFilteringCache.lookupResult(fctxt);
|
2017-08-03 16:18:05 +02:00
|
|
|
if ( entry !== undefined ) {
|
2018-12-13 18:30:54 +01:00
|
|
|
fctxt.filter = entry.logData;
|
2017-08-03 16:18:05 +02:00
|
|
|
return entry.result;
|
|
|
|
}
|
2014-09-14 22:20:40 +02:00
|
|
|
}
|
2014-12-28 16:07:43 +01:00
|
|
|
|
2016-10-14 16:06:34 +02:00
|
|
|
// Dynamic URL filtering.
|
2018-12-13 18:30:54 +01:00
|
|
|
let result = µb.sessionURLFiltering.evaluateZ(
|
|
|
|
fctxt.getTabHostname(),
|
|
|
|
fctxt.url,
|
|
|
|
requestType
|
|
|
|
);
|
|
|
|
if ( result !== 0 && µb.logger.enabled ) {
|
|
|
|
fctxt.filter = µb.sessionURLFiltering.toLogData();
|
2017-05-12 16:35:11 +02:00
|
|
|
}
|
2015-01-06 14:01:15 +01:00
|
|
|
|
2016-10-14 16:06:34 +02:00
|
|
|
// Dynamic hostname/type filtering.
|
2017-05-12 16:35:11 +02:00
|
|
|
if ( result === 0 && µb.userSettings.advancedUserEnabled ) {
|
2018-12-13 18:30:54 +01:00
|
|
|
result = µb.sessionFirewall.evaluateCellZY(
|
|
|
|
fctxt.getTabHostname(),
|
|
|
|
fctxt.getHostname(),
|
|
|
|
requestType
|
|
|
|
);
|
|
|
|
if ( result !== 0 && result !== 3 && µb.logger.enabled ) {
|
|
|
|
fctxt.filter = µb.sessionFirewall.toLogData();
|
2017-08-03 16:18:05 +02:00
|
|
|
}
|
2015-01-06 14:01:15 +01:00
|
|
|
}
|
|
|
|
|
2017-08-03 16:18:05 +02:00
|
|
|
// Static filtering has lowest precedence.
|
2017-05-12 16:35:11 +02:00
|
|
|
if ( result === 0 || result === 3 ) {
|
2018-12-13 18:30:54 +01:00
|
|
|
result = µb.staticNetFilteringEngine.matchString(fctxt);
|
|
|
|
if ( result !== 0 && µb.logger.enabled ) {
|
|
|
|
fctxt.filter = µb.staticNetFilteringEngine.toLogData();
|
2015-06-09 16:27:08 +02:00
|
|
|
}
|
2015-01-06 14:01:15 +01:00
|
|
|
}
|
|
|
|
|
2017-08-03 16:18:05 +02:00
|
|
|
if ( cacheableResult ) {
|
2018-12-18 20:00:23 +01:00
|
|
|
this.netFilteringCache.rememberResult(fctxt, result);
|
2018-12-13 18:30:54 +01:00
|
|
|
} else if ( result === 1 && this.collapsibleResources.has(requestType) ) {
|
|
|
|
this.netFilteringCache.rememberBlock(fctxt, true);
|
2017-08-03 16:18:05 +02:00
|
|
|
}
|
2014-12-28 16:07:43 +01:00
|
|
|
|
2015-01-06 14:01:15 +01:00
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
PageStore.prototype.cacheableResults = new Set([
|
|
|
|
'sub_frame',
|
|
|
|
]);
|
2017-08-03 16:18:05 +02:00
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
PageStore.prototype.collapsibleResources = new Set([
|
|
|
|
'image',
|
|
|
|
'media',
|
|
|
|
'object',
|
|
|
|
'sub_frame',
|
|
|
|
]);
|
2017-08-03 16:18:05 +02:00
|
|
|
|
2015-01-24 18:06:22 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
PageStore.prototype.filterCSPReport = function(fctxt) {
|
|
|
|
if (
|
|
|
|
µb.sessionSwitches.evaluateZ(
|
|
|
|
'no-csp-reports',
|
|
|
|
fctxt.getHostname()
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
if ( µb.logger.enabled ) {
|
|
|
|
fctxt.filter = µb.sessionSwitches.toLogData();
|
2017-10-19 15:35:28 +02:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
PageStore.prototype.filterFont = function(fctxt) {
|
|
|
|
if ( fctxt.type === 'font' ) {
|
2017-10-19 15:35:28 +02:00
|
|
|
this.remoteFontCount += 1;
|
|
|
|
}
|
2018-12-13 18:30:54 +01:00
|
|
|
if (
|
|
|
|
µb.sessionSwitches.evaluateZ(
|
|
|
|
'no-remote-fonts',
|
|
|
|
fctxt.getTabHostname()
|
|
|
|
) !== false
|
|
|
|
) {
|
|
|
|
if ( µb.logger.enabled ) {
|
|
|
|
fctxt.filter = µb.sessionSwitches.toLogData();
|
2017-10-19 15:35:28 +02:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
PageStore.prototype.filterScripting = function(fctxt, netFiltering) {
|
|
|
|
fctxt.filter = undefined;
|
2018-09-01 12:36:17 +02:00
|
|
|
if ( netFiltering === undefined ) {
|
|
|
|
netFiltering = this.getNetFilteringSwitch();
|
|
|
|
}
|
2018-09-01 00:47:02 +02:00
|
|
|
if (
|
2018-09-01 12:36:17 +02:00
|
|
|
netFiltering === false ||
|
2018-12-13 18:30:54 +01:00
|
|
|
µb.sessionSwitches.evaluateZ(
|
|
|
|
'no-scripting',
|
|
|
|
fctxt.getTabHostname()
|
|
|
|
) === false
|
2018-09-01 00:47:02 +02:00
|
|
|
) {
|
|
|
|
return 0;
|
|
|
|
}
|
2018-12-13 18:30:54 +01:00
|
|
|
if ( µb.logger.enabled ) {
|
|
|
|
fctxt.filter = µb.sessionSwitches.toLogData();
|
2018-09-01 00:47:02 +02:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-11-08 21:53:08 +01:00
|
|
|
// The caller is responsible to check whether filtering is enabled or not.
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
PageStore.prototype.filterLargeMediaElement = function(fctxt, size) {
|
|
|
|
fctxt.filter = undefined;
|
2017-05-12 16:35:11 +02:00
|
|
|
|
2016-11-08 21:53:08 +01:00
|
|
|
if ( Date.now() < this.allowLargeMediaElementsUntil ) {
|
2017-05-12 16:35:11 +02:00
|
|
|
return 0;
|
2016-11-08 21:53:08 +01:00
|
|
|
}
|
2018-12-13 18:30:54 +01:00
|
|
|
if (
|
|
|
|
µb.sessionSwitches.evaluateZ(
|
|
|
|
'no-large-media',
|
|
|
|
fctxt.getTabHostname()
|
|
|
|
) !== true
|
|
|
|
) {
|
2017-05-12 16:35:11 +02:00
|
|
|
return 0;
|
2016-11-08 21:53:08 +01:00
|
|
|
}
|
|
|
|
if ( (size >>> 10) < µb.userSettings.largeMediaSize ) {
|
2017-05-12 16:35:11 +02:00
|
|
|
return 0;
|
2016-11-08 21:53:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this.largeMediaCount += 1;
|
|
|
|
if ( this.largeMediaTimer === null ) {
|
2019-04-24 14:30:54 +02:00
|
|
|
this.largeMediaTimer = vAPI.setTimeout(( ) => {
|
|
|
|
this.largeMediaTimer = null;
|
|
|
|
this.injectLargeMediaElementScriptlet();
|
|
|
|
}, 500);
|
2016-11-08 21:53:08 +01:00
|
|
|
}
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
if ( µb.logger.enabled ) {
|
|
|
|
fctxt.filter = µb.sessionSwitches.toLogData();
|
2017-05-12 16:35:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
2016-11-08 21:53:08 +01:00
|
|
|
};
|
|
|
|
|
2017-08-03 16:18:05 +02:00
|
|
|
// https://www.youtube.com/watch?v=drW8p_dTLD4
|
2015-06-10 15:23:48 +02:00
|
|
|
|
2017-08-03 16:18:05 +02:00
|
|
|
/******************************************************************************/
|
2017-05-12 16:35:11 +02:00
|
|
|
|
2017-08-03 16:18:05 +02:00
|
|
|
PageStore.prototype.getBlockedResources = function(request, response) {
|
2018-12-13 18:30:54 +01:00
|
|
|
const normalURL = µb.normalizePageURL(this.tabId, request.frameURL);
|
|
|
|
const resources = request.resources;
|
|
|
|
const fctxt = µBlock.filteringContext;
|
|
|
|
fctxt.fromTabId(this.tabId)
|
|
|
|
.setDocOriginFromURL(normalURL);
|
2017-10-01 13:56:28 +02:00
|
|
|
// Force some resources to go through the filtering engine in order to
|
|
|
|
// populate the blocked-resources cache. This is required because for
|
|
|
|
// some resources it's not possible to detect whether they were blocked
|
|
|
|
// content script-side (i.e. `iframes` -- unlike `img`).
|
2017-08-03 16:18:05 +02:00
|
|
|
if ( Array.isArray(resources) && resources.length !== 0 ) {
|
2018-12-13 18:30:54 +01:00
|
|
|
for ( const resource of resources ) {
|
|
|
|
this.filterRequest(
|
|
|
|
fctxt.setType(resource.type)
|
|
|
|
.setURL(resource.url)
|
|
|
|
);
|
2017-05-12 16:35:11 +02:00
|
|
|
}
|
2015-06-10 15:23:48 +02:00
|
|
|
}
|
2017-10-01 13:56:28 +02:00
|
|
|
if ( this.netFilteringCache.hash === response.hash ) { return; }
|
2017-08-03 16:18:05 +02:00
|
|
|
response.hash = this.netFilteringCache.hash;
|
2018-12-13 18:30:54 +01:00
|
|
|
response.blockedResources =
|
|
|
|
this.netFilteringCache.lookupAllBlocked(fctxt.getDocHostname());
|
2015-01-24 18:06:22 +01:00
|
|
|
};
|
|
|
|
|
2014-06-24 00:42:43 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
return {
|
2014-09-14 22:20:40 +02:00
|
|
|
factory: PageStore.factory
|
2014-06-24 00:42:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|