Skip to content

Commit

Permalink
Revert "MV3: Strip noscript tags in <head> element to prevent breakag…
Browse files Browse the repository at this point in the history
…e. See #477"

I thought we could just strip `<noscript>` and save a couple cycles, but
I didn't realize this is operating on the live page, so better to just
remove what's invalid and leave the rest (for all the people running the
Zotero Connector with JavaScript disabled, which may or may not even be
possible...).

This reverts commit f1be738.
  • Loading branch information
dstillman committed Jun 5, 2024
1 parent f1be738 commit bf9eca4
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/browserExt/translateSandbox/translateSandboxManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,24 @@ Zotero.SandboxedTranslateManager = {
});
},
setDocument: (doc) => {
// Strip noscript tags in head because they may cause DOMParser closing the
// head tag early and breaking translators.
// Only <link>, <meta> and <style> tags are allowed in <noscript> tags in head.
// If any other tag is included, web parsers are supposed to immediately break
// the <head> tag and any subsequent content of the <head> element is included
// in the <body> starting with the invalid content of the <noscript> tag.
// This is the behaviour of DOMParser and breaks EM translator meta tag detection
// on at least some pages, so we have to do this custom stupid handling.
// Also we could probably just strip these tags completely since we would
// generally be interested in translating the live page with all its changes
// but we're being safe.
for (let noscriptTag of doc.head.querySelectorAll('noscript')) {
noscriptTag.remove();
let tags = noscriptTag.innerHTML.match(/<[^/>]*>/g);
if (!tags) continue;
for (let tag of tags) {
if (!this.noscriptHeadAllowedTags.some(allowedTag => tag.startsWith(`<${allowedTag}`))) {
noscriptTag.remove();
break;
}
}
}
return this.frame.sendMessage('Translate.setDocument', [doc.documentElement.outerHTML, doc.location.href]);
},
Expand Down

0 comments on commit bf9eca4

Please sign in to comment.