Переглянути джерело

move companion code into "single-file-companion"

Gildas 3 роки тому
батько
коміт
cf37cce692

+ 2 - 43
companion/README.MD

@@ -1,45 +1,4 @@
 # SingleFile Companion
 # SingleFile Companion
-SingleFile Companion is a program that runs outside the browser. It can:
- - make the saving process more transparent when the auto-save is active in SingleFile,  
- - auto-save pages in an anonymous session in a separate browser,
- - save pages in another directory than the download directory.
- 
- It uses the [Native Messaging API](https://developer.mozilla.org/docs/Mozilla/Add-ons/WebExtensions/Native_messaging) to communicate with SingleFile.
 
 
-## Install
-
- - Install [Node.js](https://nodejs.org)
-
- - Download the [SingleFile project](https://github.com/gildas-lormeau/SingleFile/archive/master.zip) zip file and unzip it somewhere on your disk.
-
- - Run `npm install` in the root folder of SingleFile.
-
- - In the `companion` folder of SingleFile, go into the subfolder corresponding to your OS and your browser. For example, if you use Chome on Linux:
-
-`cd companion`
-
-`cd linux`
-
-`cd chrome`
-
- - Make `install.sh` executable (Linux/Unix/BSD etc.).
-
-`chmod +x install.sh`
-
- - Run `install`
-
-`./install.sh` (Linux/Unix/BSD etc.)
-
-`install.bat` (Windows)
-
- - Enable the option `Destination > save with SingleFile Companion` or `Auto-save > save the page with SingleFile Companion` in SingleFile
-
-## Options
-
-The `options.json` file allows configuring SingleFile Companion. Here are the entries you can edit:
- - `savePath`: path where to save files (default: `SingleFile/companion` path)
-These options are also available when the option `Auto-save > save the page with SingleFile Companion` is enabled:
- - `backEnd`: backend used to save the page (default: `"puppeteer"`)
- - `errorFile`: path of the file where errors are stored (default: `undefined`)
- - `browserHeadless`: whether the browser is launched in headless mode (default: `true`)
- - `browserDebug`: whether the browser is launched with the developer tools opened (default: `false`)
+SingleFile Companion project has moved here:
+https://github.com/gildas-lormeau/single-file-companion

+ 0 - 132
companion/lib/messaging.js

@@ -1,132 +0,0 @@
-// https://github.com/andy-portmen/native-client/blob/master/messaging.js
-
-//  MPL-2.0 License (https://github.com/andy-portmen/native-client/blob/master/COPYING)
-
-// chrome-native-messaging module
-//
-// Defines three Transform streams:
-//
-// - Input - transform native messages to JavaScript objects
-// - Output - transform JavaScript objects to native messages
-// - Transform - transform message objects to reply objects
-// - Debug - transform JavaScript objects to lines of JSON (for debugging, obviously)
-
-const stream = require('stream');
-const util = require('util');
-
-function Input() {
-  stream.Transform.call(this);
-
-  // Transform bytes...
-  this._writableState.objectMode = false;
-  // ...into objects.
-  this._readableState.objectMode = true;
-
-  // Unparsed data.
-  this.buf = Buffer.alloc(0);
-  // Parsed length.
-  this.len = null;
-}
-
-util.inherits(Input, stream.Transform);
-
-Input.prototype._transform = function(chunk, encoding, done) {
-  // Save this chunk.
-  this.buf = Buffer.concat([this.buf, chunk]);
-
-  const self = this;
-
-  function parseBuf() {
-    // Do we have a length yet?
-    if (typeof self.len !== 'number') {
-      // Nope. Do we have enough bytes for the length?
-      if (self.buf.length >= 4) {
-        // Yep. Parse the bytes.
-        self.len = self.buf.readUInt32LE(0);
-        // Remove the length bytes from the buffer.
-        self.buf = self.buf.slice(4);
-      }
-    }
-
-    // Do we have a length yet? (We may have just parsed it.)
-    if (typeof self.len === 'number') {
-      // Yep. Do we have enough bytes for the message?
-      if (self.buf.length >= self.len) {
-        // Yep. Slice off the bytes we need.
-        const message = self.buf.slice(0, self.len);
-        // Remove the bytes for the message from the buffer.
-        self.buf = self.buf.slice(self.len);
-        // Clear the length so we know we need to parse it again.
-        self.len = null;
-        // Parse the message bytes.
-        const obj = JSON.parse(message.toString());
-        // Enqueue it for reading.
-        self.push(obj);
-        // We could have more messages in the buffer so check again.
-        parseBuf();
-      }
-    }
-  }
-
-  // Check for a parsable buffer (both length and message).
-  parseBuf();
-
-  // We're done.
-  done();
-};
-
-function Output() {
-  stream.Transform.call(this);
-
-  this._writableState.objectMode = true;
-  this._readableState.objectMode = false;
-}
-
-util.inherits(Output, stream.Transform);
-
-Output.prototype._transform = function(chunk, encoding, done) {
-  const len = Buffer.alloc(4);
-  const buf = Buffer.from(JSON.stringify(chunk), 'utf8');
-
-  len.writeUInt32LE(buf.length, 0);
-
-  this.push(len);
-  this.push(buf);
-
-  done();
-};
-
-function Transform(handler) {
-  stream.Transform.call(this);
-
-  this._writableState.objectMode = true;
-  this._readableState.objectMode = true;
-
-  this.handler = handler;
-}
-
-util.inherits(Transform, stream.Transform);
-
-Transform.prototype._transform = function(msg, encoding, done) {
-  this.handler(msg, this.push.bind(this), done);
-};
-
-function Debug() {
-  stream.Transform.call(this);
-
-  this._writableState.objectMode = true;
-  this._readableState.objectMode = false;
-}
-
-util.inherits(Debug, stream.Transform);
-
-Debug.prototype._transform = function(chunk, encoding, done) {
-  this.push(JSON.stringify(chunk) + '\n');
-
-  done();
-};
-
-exports.Input = Input;
-exports.Output = Output;
-exports.Transform = Transform;
-exports.Debug = Debug;

+ 0 - 3
companion/linux/chrome/install.sh

@@ -1,3 +0,0 @@
-#!/bin/sh
-mkdir -p ~/.config/google-chrome/NativeMessagingHosts/
-jq '.path= "'$PWD'/singlefile_companion.sh"' singlefile_companion.json > ~/.config/google-chrome/NativeMessagingHosts/singlefile_companion.json

+ 0 - 11
companion/linux/chrome/singlefile_companion.json

@@ -1,11 +0,0 @@
-{
-    "name": "singlefile_companion",
-    "description": "SingleFile Companion",
-    "path": "singlefile_companion.sh",
-    "type": "stdio",
-    "allowed_origins": [
-        "chrome-extension://mpiodijhokgodhhofbcjdecpffjipkle/",
-        "chrome-extension://efnbkdcfmcmnhlkaijjjmhjjgladedno/",
-        "chrome-extension://pfocgciignlhlijcdpkbhmioobagfpmd/"
-    ]
-}

+ 0 - 2
companion/linux/chrome/singlefile_companion.sh

@@ -1,2 +0,0 @@
-#!/bin/sh
-node ../../singlefile_companion.js

+ 0 - 2
companion/linux/chrome/uninstall.sh

@@ -1,2 +0,0 @@
-#!/bin/sh
-rm -f ~/.config/google-chrome/NativeMessagingHosts/singlefile_companion.json

+ 0 - 3
companion/linux/firefox/install.sh

@@ -1,3 +0,0 @@
-#!/bin/sh
-mkdir -p ~/.mozilla/native-messaging-hosts/
-jq '.path= "'$PWD'/singlefile_companion.sh"' singlefile_companion.json > ~/.mozilla/native-messaging-hosts/singlefile_companion.json

+ 0 - 9
companion/linux/firefox/singlefile_companion.json

@@ -1,9 +0,0 @@
-{
-    "name": "singlefile_companion",
-    "description": "SingleFile Companion",
-    "path": "singlefile_companion.sh",
-    "type": "stdio",
-    "allowed_extensions": [
-        "{531906d3-e22f-4a6c-a102-8057b88a1a63}"
-    ]
-}

+ 0 - 2
companion/linux/firefox/singlefile_companion.sh

@@ -1,2 +0,0 @@
-#!/bin/sh
-node ../../singlefile_companion.js

+ 0 - 2
companion/linux/firefox/uninstall.sh

@@ -1,2 +0,0 @@
-#!/bin/sh
-rm -f ~/.mozilla/native-messaging-hosts/singlefile_companion.json

+ 0 - 3
companion/mac/chrome/install.sh

@@ -1,3 +0,0 @@
-#!/bin/sh
-mkdir -p ~/Library/Application Support/Google/Chrome/NativeMessagingHosts/
-jq '.path= "'$PWD'/singlefile_companion.sh"' singlefile_companion.json > ~/Library/Application Support/Google/Chrome/NativeMessagingHosts/singlefile_companion.json

+ 0 - 11
companion/mac/chrome/singlefile_companion.json

@@ -1,11 +0,0 @@
-{
-    "name": "singlefile_companion",
-    "description": "SingleFile Companion",
-    "path": "singlefile_companion.sh",
-    "type": "stdio",
-    "allowed_origins": [
-        "chrome-extension://mpiodijhokgodhhofbcjdecpffjipkle/",
-        "chrome-extension://efnbkdcfmcmnhlkaijjjmhjjgladedno/",
-        "chrome-extension://pfocgciignlhlijcdpkbhmioobagfpmd/"
-    ]
-}

+ 0 - 2
companion/mac/chrome/singlefile_companion.sh

@@ -1,2 +0,0 @@
-#!/bin/sh
-node ../../singlefile_companion.js

+ 0 - 2
companion/mac/chrome/uninstall.sh

@@ -1,2 +0,0 @@
-#!/bin/sh
-rm -f ~/Library/Application Support/Google/Chrome/NativeMessagingHosts/singlefile_companion.json

+ 0 - 3
companion/mac/firefox/install.sh

@@ -1,3 +0,0 @@
-#!/bin/sh
-mkdir -p ~/Library/Application Support/Mozilla/NativeMessagingHosts/
-jq '.path= "'$PWD'/singlefile_companion.sh"' singlefile_companion.json > ~/Library/Application Support/Mozilla/NativeMessagingHosts/singlefile_companion.json

+ 0 - 9
companion/mac/firefox/singlefile_companion.json

@@ -1,9 +0,0 @@
-{
-    "name": "singlefile_companion",
-    "description": "SingleFile Companion",
-    "path": "singlefile_companion.sh",
-    "type": "stdio",
-    "allowed_extensions": [
-        "{531906d3-e22f-4a6c-a102-8057b88a1a63}"
-    ]
-}

+ 0 - 2
companion/mac/firefox/singlefile_companion.sh

@@ -1,2 +0,0 @@
-#!/bin/sh
-node ../../singlefile_companion.js

+ 0 - 2
companion/mac/firefox/uninstall.sh

@@ -1,2 +0,0 @@
-#!/bin/sh
-rm -f ~/Library/Application Support/Mozilla/NativeMessagingHosts/singlefile_companion.json

+ 0 - 4
companion/options.json

@@ -1,4 +0,0 @@
-{
-    "browserHeadless": true,
-    "browserDebug": false
-}

+ 0 - 105
companion/singlefile_companion.js

@@ -1,105 +0,0 @@
-#!/usr/local/bin/node
-
-/*
- * Copyright 2010-2020 Gildas Lormeau
- * contact : gildas.lormeau <at> gmail.com
- * 
- * This file is part of SingleFile.
- *
- *   The code in this file is free software: you can redistribute it and/or 
- *   modify it under the terms of the GNU Affero General Public License 
- *   (GNU AGPL) as published by the Free Software Foundation, either version 3
- *   of the License, or (at your option) any later version.
- * 
- *   The code in this file 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.
- *
- *   As additional permission under GNU AGPL version 3 section 7, you may 
- *   distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU 
- *   AGPL normally required by section 4, provided you include this license 
- *   notice and a URL through which recipients can access the Corresponding 
- *   Source.
- */
-
-/* global require, process */
-
-const fs = require("fs");
-const path = require("path");
-const nativeMessage = require("./lib/messaging.js");
-const backEnds = {
-	jsdom: "single-file-cli/back-ends/jsdom.js",
-	puppeteer: "single-file-cli/back-ends/puppeteer.js",
-	"puppeteer-firefox": "single-file-cli/back-ends/puppeteer-firefox.js",
-	"webdriver-chromium": "single-file-cli/back-ends/webdriver-chromium.js",
-	"webdriver-gecko": "single-file-cli/back-ends/webdriver-gecko.js"
-};
-
-process.stdin
-	.pipe(new nativeMessage.Input())
-	.pipe(new nativeMessage.Transform(async function (message, push, done) {
-		try {
-			await processMessage(message);
-			push({});
-		} catch (error) {
-			push({ error });
-		}
-		done();
-		process.exit(0);
-	}))
-	.pipe(new nativeMessage.Output())
-	.pipe(process.stdout);
-
-async function processMessage(message) {
-	if (message.method == "save") {
-		return save(message.pageData);
-	}
-	if (message.method == "externalSave") {
-		return externalSave(message.pageData);
-	}
-}
-
-async function save(message) {
-	const companionOptions = require("./options.json");
-	const filename = path.resolve("../../", (companionOptions.savePath || ""), message.filename);
-	fs.writeFileSync(getFilename(filename), message.content);
-}
-
-async function externalSave(message) {
-	const companionOptions = require("./options.json");
-	const backend = require(backEnds[companionOptions.backEnd || "puppeteer"]);
-	await backend.initialize(companionOptions);
-	try {
-		const pageData = await backend.getPageData(message);
-		pageData.filename = path.resolve("../../", (companionOptions.savePath || ""), pageData.filename);
-		fs.writeFileSync(getFilename(pageData.filename), pageData.content);
-		return pageData;
-	} catch (error) {
-		if (companionOptions.errorFile) {
-			const message = "URL: " + message.url + "\nStack: " + error.stack + "\n";
-			fs.writeFileSync(companionOptions.errorFile, message, { flag: "a" });
-		}
-		throw error;
-	} finally {
-		await backend.closeBrowser();
-	}
-}
-
-function getFilename(filename, index = 1) {
-	let newFilename = filename;
-	if (index > 1) {
-		const regExpMatchExtension = /(\.[^.]+)$/;
-		const matchExtension = newFilename.match(regExpMatchExtension);
-		if (matchExtension && matchExtension[1]) {
-			newFilename = newFilename.replace(regExpMatchExtension, " - " + index + matchExtension[1]);
-		} else {
-			newFilename += " - " + index;
-		}
-	}
-	if (fs.existsSync(newFilename)) {
-		return getFilename(filename, index + 1);
-	} else {
-		return newFilename;
-	}
-}

+ 0 - 2
companion/win/chrome/install.bat

@@ -1,2 +0,0 @@
-@echo off
-reg add "HKEY_CURRENT_USER\Software\Google\Chrome\NativeMessagingHosts\singlefile_companion" /ve /t REG_SZ /d "%~dp0\singlefile_companion.json" /f

+ 0 - 2
companion/win/chrome/singlefile_companion.bat

@@ -1,2 +0,0 @@
-@echo off
-node ../../singlefile_companion.js

+ 0 - 11
companion/win/chrome/singlefile_companion.json

@@ -1,11 +0,0 @@
-{
-    "name": "singlefile_companion",
-    "description": "SingleFile Companion",
-    "path": "singlefile_companion.bat",
-    "type": "stdio",
-    "allowed_origins": [
-        "chrome-extension://mpiodijhokgodhhofbcjdecpffjipkle/",
-        "chrome-extension://efnbkdcfmcmnhlkaijjjmhjjgladedno/",
-        "chrome-extension://pfocgciignlhlijcdpkbhmioobagfpmd/"
-    ]
-}

+ 0 - 2
companion/win/chrome/uninstall.bat

@@ -1,2 +0,0 @@
-@echo off
-reg delete "HKEY_CURRENT_USER\Software\Google\Chrome\NativeMessagingHosts\singlefile_companion" /ve /f

+ 0 - 2
companion/win/firefox/install.bat

@@ -1,2 +0,0 @@
-@echo off
-reg add "HKEY_CURRENT_USER\Software\Mozilla\NativeMessagingHosts\singlefile_companion" /ve /t REG_SZ /d "%~dp0\singlefile_companion.json" /f

+ 0 - 2
companion/win/firefox/singlefile_companion.bat

@@ -1,2 +0,0 @@
-@echo off
-node ../../singlefile_companion.js

+ 0 - 9
companion/win/firefox/singlefile_companion.json

@@ -1,9 +0,0 @@
-{
-    "name": "singlefile_companion",
-    "description": "SingleFile Companion",
-    "path": "singlefile_companion.bat",
-    "type": "stdio",
-    "allowed_extensions": [
-        "{531906d3-e22f-4a6c-a102-8057b88a1a63}"
-    ]
-}

+ 0 - 2
companion/win/firefox/uninstall.bat

@@ -1,2 +0,0 @@
-@echo off
-reg delete "HKEY_CURRENT_USER\Software\Mozilla\NativeMessagingHosts\singlefile_companion" /ve /f