diff --git a/assets/resources/scriptlets.js b/assets/resources/scriptlets.js index 6514f39bb..bc7dd84fc 100644 --- a/assets/resources/scriptlets.js +++ b/assets/resources/scriptlets.js @@ -668,12 +668,18 @@ /// no-floc.js // https://github.com/uBlockOrigin/uBlock-issues/issues/1553 (function() { - if ( document.interestCohort instanceof Function === false ) { return; } - document.interestCohort = new Proxy(document.interestCohort, { - apply: function() { - return Promise.reject(); + if ( Document instanceof Object === false ) { return; } + if ( Document.prototype.interestCohort instanceof Function === false ) { + return; + } + Document.prototype.interestCohort = new Proxy( + Document.prototype.interestCohort, + { + apply: function() { + return Promise.reject(); + } } - }); + ); })(); diff --git a/src/js/scriptlet-filtering.js b/src/js/scriptlet-filtering.js index 1f50c9b10..2ef28ef3c 100644 --- a/src/js/scriptlet-filtering.js +++ b/src/js/scriptlet-filtering.js @@ -382,6 +382,10 @@ return out.join('\n'); }; + api.hasScriptlet = function(hostname, exceptionBit, scriptlet) { + return scriptletDB.hasStr(hostname, exceptionBit, scriptlet); + }; + api.injectNow = function(details) { if ( typeof details.frameId !== 'number' ) { return; } const request = { diff --git a/src/js/static-ext-filtering.js b/src/js/static-ext-filtering.js index 3a07c3c4e..4c49a0ee4 100644 --- a/src/js/static-ext-filtering.js +++ b/src/js/static-ext-filtering.js @@ -173,6 +173,35 @@ } } + hasStr(hostname, exceptionBit, value) { + let found = false; + for (;;) { + let iHn = this.hostnameToSlotIdMap.get(hostname); + if ( iHn !== undefined ) { + do { + const strId = this.hostnameSlots[iHn+0]; + if ( this.strSlots[strId >>> this.nBits] === value ) { + if ( (strId & exceptionBit) !== 0 ) { + return false; + } + found = true; + } + iHn = this.hostnameSlots[iHn+1]; + } while ( iHn !== 0 ); + } + if ( hostname === '' ) { break; } + const pos = hostname.indexOf('.'); + if ( pos !== -1 ) { + hostname = hostname.slice(pos + 1); + } else if ( hostname !== '*' ) { + hostname = '*'; + } else { + hostname = ''; + } + } + return found; + } + toSelfie() { return { hostnameToSlotIdMap: Array.from(this.hostnameToSlotIdMap), diff --git a/src/js/traffic.js b/src/js/traffic.js index 89b836e4d..0f383aa0d 100644 --- a/src/js/traffic.js +++ b/src/js/traffic.js @@ -53,6 +53,9 @@ window.addEventListener('webextFlavor', function() { vAPI.webextFlavor.major < 59; }, { once: true }); +// https://github.com/uBlockOrigin/uBlock-issues/issues/1553 +const supportsFloc = document.interestCohort instanceof Function; + /******************************************************************************/ // Intercept and filter web requests. @@ -540,7 +543,7 @@ const onHeadersReceived = function(details) { } // At this point we have a HTML document. - + const filteredHTML = µb.canFilterResponseData && filterDocument(fctxt, details) === true; @@ -551,6 +554,9 @@ const onHeadersReceived = function(details) { if ( injectCSP(fctxt, pageStore, responseHeaders) === true ) { modifiedHeaders = true; } + if ( supportsFloc && foilFloc(fctxt, responseHeaders) ) { + modifiedHeaders = true; + } // https://bugzilla.mozilla.org/show_bug.cgi?id=1376932 // Prevent document from being cached by the browser if we modified it, @@ -1012,6 +1018,23 @@ const injectCSP = function(fctxt, pageStore, responseHeaders) { /******************************************************************************/ +// https://github.com/uBlockOrigin/uBlock-issues/issues/1553 +// https://github.com/WICG/floc#opting-out-of-computation + +const foilFloc = function(fctxt, responseHeaders) { + const hn = fctxt.getHostname(); + if ( µBlock.scriptletFilteringEngine.hasScriptlet(hn, 1, 'no-floc') === false ) { + return false; + } + responseHeaders.push({ + name: 'Permissions-Policy', + value: 'interest-cohort=()' } + ); + return true; +}; + +/******************************************************************************/ + // https://github.com/gorhill/uBlock/issues/1163 // "Block elements by size". // https://github.com/gorhill/uBlock/issues/1390#issuecomment-187310719