Selaa lähdekoodia

better managment of forbidden pages (fix #162)

Gildas 7 vuotta sitten
vanhempi
sitoutus
be5aa6bcb8

+ 99 - 99
README.MD

@@ -1,99 +1,99 @@
-# SingleFile
-SingleFile is a WebExtension compatible with Chrome, Firefox (Desktop and Mobile), and Opera that helps you to save a complete web page into a single HTML file.
-
-## Table of Contents
- - [Demo](#demo)
- - [Install](#install)
- - [Getting started](#getting-started)
- - [Additional notes](#additional-notes)
- - [Statistics (Firefox)](#statistics-firefox)
- - [Command Line Interface](#command-line-interface)
- - [Known issues](#known-issues)
- - [Troubleshooting unknown issues](#troubleshooting-unknown-issues)
- - [Release notes](#release-notes)
- - [Contributors](#contributors)
- - [Icons](#icons)
- - [Code derived from third party projects](#code-derived-from-third-party-projects)
- - [License](#license)
-
-## Demo
-https://www.youtube.com/watch?v=RdV8DBW5b0g
-
-## Install
-SingleFile can be installed on:
- - Firefox via the Firefox Add-ons website: https://addons.mozilla.org/en-US/firefox/addon/single-file,
- - Chrome via the Chrome Web Store: https://chrome.google.com/extensions/detail/mpiodijhokgodhhofbcjdecpffjipkle,
- - Opera and forks of Firefox/Chrome by downloading the zip file (https://github.com/gildas-lormeau/SingleFile/archive/master.zip) and installing it manually.
-
-## Getting started
-- Wait until the page is fully loaded, you may need to scroll down the entire page to be sure all elements are loaded.
-- Click on the SingleFile button in the extension toolbar to save the page.
-
-## Additional notes
- - To save multiple tabs, select them first and click on the SingleFile button.
- - Open the context menu by right-clicking the SingleFile button in the extension toolbar or on the webpage. It allows you to save:
-   - the current tab,
-   - or the selected content,
-   - or the selected frame.
- - You can also process multiple tabs in one click and save:
-   - the selected tabs,
-   - or the unpinned tabs,
-   - or all the tabs.
- - The context menu also allows you to activate the auto-save of:
-   - the current tab,
-   - or the unpinned tabs,
-   - or all the tabs.
- - With auto-save active, pages are automatically saved every time after being loaded (or before being unloaded if not).
- - Right-click on the SingleFile button and select "Options"/"Manage extension" to open the options page.
- - The default save folder is the download folder configured in your browser, cf. about:preferences in Firefox and chrome://settings in Chrome.
- - You can use the shortcut Ctrl+Shift+Y to save the current tab or selected tabs. Go to chrome://extensions/shortcuts to change it in Chrome.
- - See the extension help in the options page for more detailed information about the options and technical notes.
-
-## Statistics (Firefox)
-See https://addons.mozilla.org/en-US/firefox/addon/single-file/statistics/?last=90
-
-## Command Line Interface
- - You can save web pages to HTML from the command line interface. See here for more info: https://github.com/gildas-lormeau/SingleFile/blob/master/cli/README.MD.
- - You can also convert MAFF pages to HTML. For this, you must use specific versions of SingleFile CLI and Firefox. See here for more info: https://github.com/gildas-lormeau/SingleFile/blob/master/maff2html/README.MD.
-
-## Known Issues
-- All browsers:
-  - For security reasons, you cannot save pages hosted on https://chrome.google.com or https://addons.mozilla.org.
-  - The last saved path cannot be remembered by default. To circumvent this limitation, disable the option "Misc > Save pages in background".
-  - The following characters are replaced with _ in filenames: ~, \\, ?, %, *, :, |, ", <, >  
-- Chrome/Opera:
-  - You must enable the option "Allow access to file URLs" in the extension page to display the infobar when viewing a saved page, or to save a page stored on the filesystem.
-  - If the filename of a saved page looks like "56833935-156b-4d8c-a00f-19599c6513d3", disable the option "Misc > Save pages in background". Reinstalling the browser may also fix this issue.
-
-## Troubleshooting unknown issues
-Please follow these steps if you find an unknown issue:
-- Save the page in incognito.
-- If saving page in incognito did not fix the issue, reset SingleFile options.
-- If resetting options did not fix the issue, restart the browser.
-- If restarting the browser did not fix the issue, try to disable all other extensions to see if there is a conflict.
-- If there is a conflict then try to determine against which extension(s).
-- Please report the issue with a short description on how to reproduce it here: https://github.com/gildas-lormeau/SingleFile/issues.
-
-## Release notes
-See https://addons.mozilla.org/en-US/firefox/addon/single-file/versions
-
-## Contributors
-- Chinese translation done by yfdyh000 (https://github.com/yfdyh000), KrasnayaPloshchad (https://github.com/KrasnayaPloshchad)
-- Japanese translation done by Shitennouji(四天王寺) (https://github.com/Shitennouji)
-- Polish translation done by xesarni (https://github.com/xesarni)
-- Russian translation done by rstp14 (https://github.com/rstp14)
-
-## Code derived from third party projects
-- csstree: https://github.com/csstree/csstree
-- postcss-media-query-parser: https://github.com/dryoma/postcss-media-query-parser
-- UglifyCSS: https://github.com/fmarcia/UglifyCSS
-- parse-srcset: https://github.com/albell/parse-srcset
-- parse-css-font: https://github.com/jedmao/parse-css-font
-
-## Icons
-- Icon made by [Kiranshastry](https://www.flaticon.com/authors/kiranshastry) from  [Flaticon](https://www.flaticon.com/) is licensed by [CC 3.0 BY](http://creativecommons.org/licenses/by/3.0/)
-
-## License
-SingleFile is licensed under AGPL and GPL. Code derived from third-party projects is licensed under MIT. Please contact me at gildas.lormeau &lt;at&gt; gmail.com if you are interested in licensing the SingleFile code for a commercial service or product.
-
-Suggestions are welcome :)
+# SingleFile
+SingleFile is a WebExtension compatible with Chrome, Firefox (Desktop and Mobile), and Opera that helps you to save a complete web page into a single HTML file.
+
+## Table of Contents
+ - [Demo](#demo)
+ - [Install](#install)
+ - [Getting started](#getting-started)
+ - [Additional notes](#additional-notes)
+ - [Statistics (Firefox)](#statistics-firefox)
+ - [Command Line Interface](#command-line-interface)
+ - [Known issues](#known-issues)
+ - [Troubleshooting unknown issues](#troubleshooting-unknown-issues)
+ - [Release notes](#release-notes)
+ - [Contributors](#contributors)
+ - [Icons](#icons)
+ - [Code derived from third party projects](#code-derived-from-third-party-projects)
+ - [License](#license)
+
+## Demo
+https://www.youtube.com/watch?v=RdV8DBW5b0g
+
+## Install
+SingleFile can be installed on:
+ - Firefox via the Firefox Add-ons website: https://addons.mozilla.org/en-US/firefox/addon/single-file,
+ - Chrome via the Chrome Web Store: https://chrome.google.com/extensions/detail/mpiodijhokgodhhofbcjdecpffjipkle,
+ - Opera and forks of Firefox/Chrome by downloading the zip file (https://github.com/gildas-lormeau/SingleFile/archive/master.zip) and installing it manually.
+
+## Getting started
+- Wait until the page is fully loaded, you may need to scroll down the entire page to be sure all elements are loaded.
+- Click on the SingleFile button in the extension toolbar to save the page.
+
+## Additional notes
+ - To save multiple tabs, select them first and click on the SingleFile button.
+ - Open the context menu by right-clicking the SingleFile button in the extension toolbar or on the webpage. It allows you to save:
+   - the current tab,
+   - or the selected content,
+   - or the selected frame.
+ - You can also process multiple tabs in one click and save:
+   - the selected tabs,
+   - or the unpinned tabs,
+   - or all the tabs.
+ - The context menu also allows you to activate the auto-save of:
+   - the current tab,
+   - or the unpinned tabs,
+   - or all the tabs.
+ - With auto-save active, pages are automatically saved every time after being loaded (or before being unloaded if not).
+ - Right-click on the SingleFile button and select "Options"/"Manage extension" to open the options page.
+ - The default save folder is the download folder configured in your browser, cf. about:preferences in Firefox and chrome://settings in Chrome.
+ - You can use the shortcut Ctrl+Shift+Y to save the current tab or selected tabs. Go to chrome://extensions/shortcuts to change it in Chrome.
+ - See the extension help in the options page for more detailed information about the options and technical notes.
+
+## Statistics (Firefox)
+See https://addons.mozilla.org/en-US/firefox/addon/single-file/statistics/?last=90
+
+## Command Line Interface
+ - You can save web pages to HTML from the command line interface. See here for more info: https://github.com/gildas-lormeau/SingleFile/blob/master/cli/README.MD.
+ - You can also convert MAFF pages to HTML. For this, you must use specific versions of SingleFile CLI and Firefox. See here for more info: https://github.com/gildas-lormeau/SingleFile/blob/master/maff2html/README.MD.
+
+## Known Issues
+- All browsers:
+  - For security reasons, you cannot save pages hosted on https://chrome.google.com or https://addons.mozilla.org and some other Mozilla domains. When this happens, the label "🛇" is displayed on top of the SingleFile icon.
+  - The last saved path cannot be remembered by default. To circumvent this limitation, disable the option "Misc > Save pages in background".
+  - The following characters are replaced with _ in filenames: ~, \\, ?, %, *, :, |, ", <, >  
+- Chrome/Opera:
+  - You must enable the option "Allow access to file URLs" in the extension page to display the infobar when viewing a saved page, or to save a page stored on the filesystem.
+  - If the filename of a saved page looks like "56833935-156b-4d8c-a00f-19599c6513d3", disable the option "Misc > Save pages in background". Reinstalling the browser may also fix this issue.
+
+## Troubleshooting unknown issues
+Please follow these steps if you find an unknown issue:
+- Save the page in incognito.
+- If saving page in incognito did not fix the issue, reset SingleFile options.
+- If resetting options did not fix the issue, restart the browser.
+- If restarting the browser did not fix the issue, try to disable all other extensions to see if there is a conflict.
+- If there is a conflict then try to determine against which extension(s).
+- Please report the issue with a short description on how to reproduce it here: https://github.com/gildas-lormeau/SingleFile/issues.
+
+## Release notes
+See https://addons.mozilla.org/en-US/firefox/addon/single-file/versions
+
+## Contributors
+- Chinese translation done by yfdyh000 (https://github.com/yfdyh000), KrasnayaPloshchad (https://github.com/KrasnayaPloshchad)
+- Japanese translation done by Shitennouji(四天王寺) (https://github.com/Shitennouji)
+- Polish translation done by xesarni (https://github.com/xesarni)
+- Russian translation done by rstp14 (https://github.com/rstp14)
+
+## Code derived from third party projects
+- csstree: https://github.com/csstree/csstree
+- postcss-media-query-parser: https://github.com/dryoma/postcss-media-query-parser
+- UglifyCSS: https://github.com/fmarcia/UglifyCSS
+- parse-srcset: https://github.com/albell/parse-srcset
+- parse-css-font: https://github.com/jedmao/parse-css-font
+
+## Icons
+- Icon made by [Kiranshastry](https://www.flaticon.com/authors/kiranshastry) from  [Flaticon](https://www.flaticon.com/) is licensed by [CC 3.0 BY](http://creativecommons.org/licenses/by/3.0/)
+
+## License
+SingleFile is licensed under AGPL and GPL. Code derived from third-party projects is licensed under MIT. Please contact me at gildas.lormeau &lt;at&gt; gmail.com if you are interested in licensing the SingleFile code for a commercial service or product.
+
+Suggestions are welcome :)

+ 18 - 8
extension/core/bg/core.js

@@ -74,27 +74,37 @@ singlefile.core = (() => {
 			const tabId = tab.id;
 			options.tabId = tabId;
 			try {
-				singlefile.ui.button.onInitialize(tabId, options, 1);
 				if (options.autoSave) {
 					const options = await singlefile.config.getOptions(tab.url, true);
 					if (singlefile.autosave.isEnabled(tab)) {
 						await singlefile.tabs.sendMessage(tab.id, { autoSavePage: true, options });
 					}
 				} else {
+					singlefile.ui.button.onInitialize(tabId, options, 1);
 					const mergedOptions = await singlefile.config.getOptions(tab.url);
 					Object.keys(options).forEach(key => mergedOptions[key] = options[key]);
+					let scriptsInjected;
 					if (!mergedOptions.removeFrames) {
-						await browser.tabs.executeScript(tab.id, { code: frameScript, allFrames: true, runAt: "document_start" });
+						try {
+							await browser.tabs.executeScript(tab.id, { code: frameScript, allFrames: true, runAt: "document_start" });
+							const code = await getContentScript(mergedOptions);
+							await browser.tabs.executeScript(tab.id, { code, allFrames: false, runAt: "document_idle" });
+							scriptsInjected = true;
+						} catch (error) {
+							// ignored
+						}
 					}
-					const code = await getContentScript(mergedOptions);
-					await browser.tabs.executeScript(tab.id, { code, allFrames: false, runAt: "document_idle" });
-					if (mergedOptions.frameId) {
-						await singlefile.tabs.sendMessage(tab.id, { saveFrame: true, options: mergedOptions }, { frameId: mergedOptions.frameId });
+					if (scriptsInjected) {
+						if (mergedOptions.frameId) {
+							await singlefile.tabs.sendMessage(tab.id, { saveFrame: true, options: mergedOptions }, { frameId: mergedOptions.frameId });
+						} else {
+							await singlefile.tabs.sendMessage(tab.id, { savePage: true, options: mergedOptions });
+						}
+						singlefile.ui.button.onInitialize(tabId, options, 2);
 					} else {
-						await singlefile.tabs.sendMessage(tab.id, { savePage: true, options: mergedOptions });
+						singlefile.ui.button.onForbiddenDomain(tabId, options);
 					}
 				}
-				singlefile.ui.button.onInitialize(tabId, options, 2);
 			} catch (error) {
 				console.log(error); // eslint-disable-line no-console
 				singlefile.ui.button.onError(tabId, options);

+ 1 - 15
extension/core/bg/util.js

@@ -24,21 +24,7 @@ singlefile.util = (() => {
 
 	const FORBIDDEN_DOMAINS = [
 		"chrome.google.com/webstore/",
-		"accounts-static.cdn.mozilla.net",
-		"accounts.firefox.com",
-		"addons.cdn.mozilla.net",
-		"addons.mozilla.org",
-		"api.accounts.firefox.com",
-		"content.cdn.mozilla.net",
-		"content.cdn.mozilla.net",
-		"discovery.addons.mozilla.org",
-		"input.mozilla.org",
-		"install.mozilla.org",
-		"oauth.accounts.firefox.com",
-		"profile.accounts.firefox.com",
-		"support.mozilla.org",
-		"sync.services.mozilla.com",
-		"testpilot.firefox.com"
+		"addons.mozilla.org"
 	];
 
 	return { isAllowedURL };

+ 13 - 14
extension/ui/bg/ui-button.js

@@ -51,6 +51,7 @@ singlefile.ui.button = (() => {
 		onInitialize,
 		onProgress,
 		onEnd,
+		onForbiddenDomain,
 		onError,
 		refresh: async tab => {
 			if (tab.id) {
@@ -110,6 +111,10 @@ singlefile.ui.button = (() => {
 		refresh(tabId, getProperties(options, BUTTON_ERROR_BADGE_MESSAGE, [229, 4, 12, 255]));
 	}
 
+	function onForbiddenDomain(tabId, options) {
+		refresh(tabId, getProperties(options, "🛇", [255, 65, 34, 255], "This page cannot be saved by SingleFile"));
+	}
+
 	function onCancelled(tabId, options) {
 		refresh(tabId, getProperties(options, "", DEFAULT_COLOR, BUTTON_DEFAULT_TOOLTIP_MESSAGE));
 	}
@@ -126,21 +131,14 @@ singlefile.ui.button = (() => {
 	}
 
 	async function refreshTab(tab) {
-		const properties = getCurrentProperties(tab.id, { autoSave: await singlefile.autosave.isEnabled(tab) });
+		const options = { autoSave: await singlefile.autosave.isEnabled(tab) }
+		const properties = getCurrentProperties(tab.id, options);
 		await refresh(tab.id, properties, true);
-		if (browser.browserAction && browser.browserAction.enable && browser.browserAction.disable) {
-			if (singlefile.util.isAllowedURL(tab.url)) {
-				try {
-					await browser.browserAction.enable(tab.id);
-				} catch (error) {
-					/* ignored */
-				}
-			} else {
-				try {
-					await browser.browserAction.disable(tab.id);
-				} catch (error) {
-					/* ignored */
-				}
+		if (!singlefile.util.isAllowedURL(tab.url)) {
+			try {
+				await onForbiddenDomain(tab.id, options);
+			} catch (error) {
+				/* ignored */
 			}
 		}
 	}
@@ -199,6 +197,7 @@ singlefile.ui.button = (() => {
 		if (browser.browserAction[browserActionMethod]) {
 			const parameter = JSON.parse(JSON.stringify(browserActionParameter));
 			parameter.tabId = tabId;
+			console.log(browserActionMethod, parameter);
 			await browser.browserAction[browserActionMethod](parameter);
 		}
 	}

+ 0 - 2
lib/browser-polyfill/chrome-browser-polyfill.js

@@ -29,8 +29,6 @@
 				onClicked: {
 					addListener: listener => nativeAPI.browserAction.onClicked.addListener(listener)
 				},
-				enable: tabId => nativeAPI.browserAction.enable(tabId),
-				disable: tabId => nativeAPI.browserAction.disable(tabId),
 				setBadgeText: options => new Promise((resolve, reject) => {
 					if (nativeAPI.runtime.lastError) {
 						reject(nativeAPI.runtime.lastError);