From dadc64e0d55ef63eff8d9f2ca3063049088ffd16 Mon Sep 17 00:00:00 2001 From: Fletcher Hazlehurst Date: Sun, 11 Oct 2020 16:49:07 -0600 Subject: [PATCH 1/2] Extract SingleFile config so we can share with connector --- chrome/content/zotero/xpcom/singlefile.js | 88 +++++++++++++++++++ .../zotero/xpcom/utilities_internal.js | 74 +++------------- 2 files changed, 100 insertions(+), 62 deletions(-) create mode 100644 chrome/content/zotero/xpcom/singlefile.js diff --git a/chrome/content/zotero/xpcom/singlefile.js b/chrome/content/zotero/xpcom/singlefile.js new file mode 100644 index 0000000000..663335edb5 --- /dev/null +++ b/chrome/content/zotero/xpcom/singlefile.js @@ -0,0 +1,88 @@ +/* + ***** BEGIN LICENSE BLOCK ***** + + Copyright © 2020 Corporation for Digital Scholarship + Vienna, Virginia, USA + https://www.zotero.org + + This file is part of Zotero. + + Zotero is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Zotero 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 Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with Zotero. If not, see . + + ***** END LICENSE BLOCK ***** +*/ + +Zotero.SingleFile = { + // These are defaults from SingleFileZ + // Located in: zotero/resources/SingleFileZ/extension/core/bg/config.js + CONFIG: { + removeHiddenElements: true, + removeUnusedStyles: true, + removeUnusedFonts: true, + removeFrames: true, + removeImports: true, + removeScripts: true, + compressHTML: true, + compressCSS: false, + loadDeferredImages: true, + loadDeferredImagesMaxIdleTime: 1500, + loadDeferredImagesBlockCookies: false, + loadDeferredImagesBlockStorage: false, + loadDeferredImagesKeepZoomLevel: false, + filenameTemplate: "{page-title} ({date-iso} {time-locale}).html", + infobarTemplate: "", + includeInfobar: false, + confirmInfobarContent: false, + autoClose: false, + confirmFilename: false, + filenameConflictAction: "uniquify", + filenameMaxLength: 192, + filenameReplacedCharacters: ["~", "+", "\\\\", "?", "%", "*", ":", "|", "\"", "<", ">", "\x00-\x1f", "\x7F"], + filenameReplacementCharacter: "_", + contextMenuEnabled: true, + tabMenuEnabled: true, + browserActionMenuEnabled: true, + shadowEnabled: true, + logsEnabled: true, + progressBarEnabled: true, + maxResourceSizeEnabled: false, + maxResourceSize: 10, + removeAudioSrc: true, + removeVideoSrc: true, + displayInfobar: true, + displayStats: false, + backgroundSave: true, + autoSaveDelay: 1, + autoSaveLoad: false, + autoSaveUnload: false, + autoSaveLoadOrUnload: true, + autoSaveRepeat: false, + autoSaveRepeatDelay: 10, + removeAlternativeFonts: true, + removeAlternativeMedias: true, + removeAlternativeImages: true, + saveRawPage: false, + saveToGDrive: false, + forceWebAuthFlow: false, + extractAuthCode: true, + insertTextBody: true, + resolveFragmentIdentifierURLs: false, + userScriptEnabled: false, + saveCreatedBookmarks: false, + ignoredBookmarkFolders: [], + replaceBookmarkURL: true, + saveFavicon: true, + includeBOM: false + } +}; diff --git a/chrome/content/zotero/xpcom/utilities_internal.js b/chrome/content/zotero/xpcom/utilities_internal.js index ab2d23740b..ef6138a8ca 100644 --- a/chrome/content/zotero/xpcom/utilities_internal.js +++ b/chrome/content/zotero/xpcom/utilities_internal.js @@ -677,74 +677,24 @@ Zotero.Utilities.Internal = { SCRIPTS.forEach( script => loadSubScript('resource://zotero/SingleFileZ/' + script, sandbox) ); + // Import config and user scripts + loadSubScript('chrome://zotero/content/xpcom/singlefile.js', sandbox); + + // In the client we turn off this auto-zooming feature because it does not work + // since the hidden browser does not have a clientHeight. + Components.utils.evalInSandbox( + 'Zotero.SingleFile.CONFIG.loadDeferredImagesKeepZoomLevel = true;', + sandbox + ); await Zotero.Promise.delay(1500); // Use SingleFile to retrieve the html - // These are defaults from SingleFileZ - // Located in: resources/SingleFileZ/extension/core/bg/config.js - // Only change is removeFrames to true (often ads that take a long time) const pageData = await Components.utils.evalInSandbox( - `this.singlefile.lib.getPageData({ - removeHiddenElements: true, - removeUnusedStyles: true, - removeUnusedFonts: true, - removeFrames: true, - removeImports: true, - removeScripts: true, - compressHTML: true, - compressCSS: false, - loadDeferredImages: true, - loadDeferredImagesMaxIdleTime: 1500, - loadDeferredImagesBlockCookies: false, - loadDeferredImagesBlockStorage: false, - loadDeferredImagesKeepZoomLevel: true, - filenameTemplate: "{page-title} ({date-iso} {time-locale}).html", - infobarTemplate: "", - includeInfobar: false, - confirmInfobarContent: false, - autoClose: false, - confirmFilename: false, - filenameConflictAction: "uniquify", - filenameMaxLength: 192, - filenameReplacementCharacter: "_", - contextMenuEnabled: true, - tabMenuEnabled: true, - browserActionMenuEnabled: true, - shadowEnabled: true, - logsEnabled: true, - progressBarEnabled: true, - maxResourceSizeEnabled: false, - maxResourceSize: 10, - removeAudioSrc: true, - removeVideoSrc: true, - displayInfobar: true, - displayStats: false, - backgroundSave: true, - autoSaveDelay: 1, - autoSaveLoad: false, - autoSaveUnload: false, - autoSaveLoadOrUnload: true, - autoSaveRepeat: false, - autoSaveRepeatDelay: 10, - removeAlternativeFonts: true, - removeAlternativeMedias: true, - removeAlternativeImages: true, - saveRawPage: false, - saveToGDrive: false, - forceWebAuthFlow: false, - extractAuthCode: true, - insertTextBody: true, - resolveFragmentIdentifierURLs: false, - userScriptEnabled: false, - saveCreatedBookmarks: false, - ignoredBookmarkFolders: [], - replaceBookmarkURL: true, - saveFavicon: true, - includeBOM: false - }, + `this.singlefile.lib.getPageData( + Zotero.SingleFile.CONFIG, { fetch: ZoteroFetch } - )`, + );`, sandbox ); From ed304b56e045e969810e6395b4c66807252f92b9 Mon Sep 17 00:00:00 2001 From: Fletcher Hazlehurst Date: Sun, 11 Oct 2020 17:26:05 -0600 Subject: [PATCH 2/2] Remove crossorigin attributes in snapshots. When loading a snapshot locally, same-origin of null will not load them --- chrome/content/zotero/xpcom/singlefile.js | 21 ++++++++++++++++++- .../zotero/xpcom/utilities_internal.js | 3 ++- scripts/babel-worker.js | 5 +++-- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/chrome/content/zotero/xpcom/singlefile.js b/chrome/content/zotero/xpcom/singlefile.js index 663335edb5..52972369fd 100644 --- a/chrome/content/zotero/xpcom/singlefile.js +++ b/chrome/content/zotero/xpcom/singlefile.js @@ -78,11 +78,30 @@ Zotero.SingleFile = { extractAuthCode: true, insertTextBody: true, resolveFragmentIdentifierURLs: false, - userScriptEnabled: false, + userScriptEnabled: true, saveCreatedBookmarks: false, ignoredBookmarkFolders: [], replaceBookmarkURL: true, saveFavicon: true, includeBOM: false + }, + + runUserScripts: function () { + let modifiedElements = []; + window.dispatchEvent(new CustomEvent('single-filez-user-script-init')); + + window.addEventListener('single-filez-on-before-capture-request', () => { + const elements = document.querySelectorAll("img[crossorigin], link[crossorigin]"); + elements.forEach((element) => { + modifiedElements.push([element, element.getAttribute('crossorigin')]); + element.removeAttribute('crossorigin'); + }); + }); + + window.addEventListener('single-filez-on-after-capture-request', () => { + modifiedElements.forEach(([element, attribute]) => { + element.setAttribute('crossorigin', attribute); + }); + }); } }; diff --git a/chrome/content/zotero/xpcom/utilities_internal.js b/chrome/content/zotero/xpcom/utilities_internal.js index ef6138a8ca..a4d5eba000 100644 --- a/chrome/content/zotero/xpcom/utilities_internal.js +++ b/chrome/content/zotero/xpcom/utilities_internal.js @@ -691,7 +691,8 @@ Zotero.Utilities.Internal = { // Use SingleFile to retrieve the html const pageData = await Components.utils.evalInSandbox( - `this.singlefile.lib.getPageData( + `Zotero.SingleFile.runUserScripts(); + this.singlefile.lib.getPageData( Zotero.SingleFile.CONFIG, { fetch: ZoteroFetch } );`, diff --git a/scripts/babel-worker.js b/scripts/babel-worker.js index f8b49904d5..7bc2363004 100644 --- a/scripts/babel-worker.js +++ b/scripts/babel-worker.js @@ -55,8 +55,9 @@ async function babelWorker(ev) { // Patch single-file-helper else if (sourcefile === 'resource/SingleFileZ/lib/single-file/single-file-helper.js') { - transformed = contents.replace('addEventListener("single-filez-user-script-init"', - 'window.addEventListener("single-filez-user-script-init"'); + transformed = contents + .replace('dispatchEvent(', 'window.dispatchEvent(') + .replace(/addEventListener\(/g, 'window.addEventListener('); } // Patch index.js - This is a SingleFileZ issue. SingleFileZ does not typically use