Improved: HTML filter (#257)

This commit is contained in:
nobody 2021-01-30 06:45:21 +01:00
parent fba55c215b
commit e597707eae
No known key found for this signature in database
GPG Key ID: 8F6DE3D614FCFD7A
2 changed files with 16 additions and 8 deletions

View File

@ -73,10 +73,11 @@ manipulateDOM._removeCrossOriginAndIntegrityAttr = function (details) {
encoder = new TextEncoder(); encoder = new TextEncoder();
isFirstData = true; isFirstData = true;
filter = browser.webRequest.filterResponseData(details.requestId); filter = browser.webRequest.filterResponseData(details.requestId);
let data = [];
header.value = 'text/html; charset=UTF-8'; header.value = 'text/html; charset=UTF-8';
//Note that this will not work if the '<script crossorigin="anonymous" src="dfgsfgd.com">' string is divided into two chunks, but we want to flush this data asap. // Note that should work if the '<script crossorigin="anonymous" src="dfgsfgd.com">' string is divided into two chunks.
filter.ondata = evt => { filter.ondata = evt => {
if (isFirstData) { if (isFirstData) {
if (!charset) { if (!charset) {
@ -87,20 +88,26 @@ manipulateDOM._removeCrossOriginAndIntegrityAttr = function (details) {
} }
decoder = new TextDecoder(charset); decoder = new TextDecoder(charset);
} }
isFirstData = false;
data.push(evt.data);
}
filter.onstop = evt => {
let str = '';
for (let buffer of data) {
str += decoder.decode(buffer, {stream: true});
}
str += decoder.decode(); // end-of-stream
//remove crossorigin and integrity attributes //remove crossorigin and integrity attributes
let str = decoder.decode(evt.data, {stream: true}).replace(/<(link|script)[^>]+>/ig, m => { str = str.replace(/<(link|script)[^>]+>/ig, m => {
if (cdnDomainsRE.test(m)) { if (cdnDomainsRE.test(m)) {
return m.replace(/\s+(integrity|crossorigin)(="[^"]*"|='[^']*'|=[^"'`=>\s]+|)/ig, ''); return m.replace(/\s+(integrity|crossorigin)(="[^"]*"|='[^']*'|=[^"'`=>\s]+|)/ig, '');
} }
return m; return m;
}); });
filter.write(encoder.encode(str)); filter.write(encoder.encode(str));
isFirstData = false;
}
filter.onstop = evt => {
let str = decoder.decode(); //end-of-stream
filter.write(encoder.encode(str));
filter.close(); filter.close();
} }
} }

View File

@ -32,6 +32,7 @@
<li>Updated: ember.js v3.24.0 -> v3.24.1 (<a href="https://codeberg.org/nobody/LocalCDN/issues/251">#251</a>)</li> <li>Updated: ember.js v3.24.0 -> v3.24.1 (<a href="https://codeberg.org/nobody/LocalCDN/issues/251">#251</a>)</li>
<li>Updated: instantsearch.js v4.12.0 -> v4.13.1 (<a href="https://codeberg.org/nobody/LocalCDN/issues/252">#252</a>)</li> <li>Updated: instantsearch.js v4.12.0 -> v4.13.1 (<a href="https://codeberg.org/nobody/LocalCDN/issues/252">#252</a>)</li>
<li>Added: Missing resources for Highlight.js (<a href="https://codeberg.org/nobody/LocalCDN/issues/253">#253</a>)</li> <li>Added: Missing resources for Highlight.js (<a href="https://codeberg.org/nobody/LocalCDN/issues/253">#253</a>)</li>
<li>Improved: HTML filter (<a href="https://codeberg.org/nobody/LocalCDN/issues/257">#257</a>)</li>
</ul> </ul>
<div id="generator-section"> <div id="generator-section">
<div class="topic-label"> <div class="topic-label">