TagSpaces is an offline, open source, document manager with tagging support
TS8 - FF web clipper IO routines
Borislav Sapundzhiev97e79cd51298
diff --git a/data/mozilla/capture.js b/data/mozilla/capture.jsnew file mode 100644index 000000000..fea3b626e--- /dev/null+++ b/data/mozilla/capture.js@@ -0,0 +1,112 @@+/* Copyright (c) 2012-2015 The TagSpaces Authors. All rights reserved.+ * Use of this source code is governed by a AGPL3 license that+ * can be found in the LICENSE file. */+/* global */+'use strict';+const { getTabContentWindow, getActiveTab } = require('sdk/tabs/utils');+const { getMostRecentBrowserWindow } = require('sdk/window/utils');+const { pathFor } = require('sdk/system');+var {Cc, Ci} = require("chrome");++exports.captureTab = function(tab=getActiveTab(getMostRecentBrowserWindow())) {++ let contentWindow = getTabContentWindow(tab);+ let { document } = contentWindow;++ let w = contentWindow.innerWidth;+ let h = contentWindow.innerHeight;+ let x = contentWindow.scrollX;+ let y = contentWindow.scrollY;++ let canvas = document.createElementNS('http://www.w3.org/1999/xhtml', 'canvas');++ canvas.width = w;+ canvas.height = h;++ let ctx = canvas.getContext('2d');+ ctx.drawWindow(contentWindow, x, y, w, h, '#000');+ let dataURL = canvas.toDataURL();++ canvas = null;++ return dataURL;+};++exports.dataURItoBlob = function (dataURI) {+ var win = getMostRecentBrowserWindow();+ // convert base64 to raw binary data held in a string+ var byteString = win.atob(dataURI.split(',')[1]);+ return byteString;+};++function getSaveLocationDialog (name) {++ var picker = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);+ var win = getMostRecentBrowserWindow();+ var dir = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);+ dir.initWithPath(pathFor('DfltDwnld').toString());++ picker.init(win, "Save File as", Ci.nsIFilePicker.modeSave);+ picker.appendFilters(Ci.nsIFilePicker.filterAll | Ci.nsIFilePicker.filterText | Ci.nsIFilePicker.filterHTML);++ picker.defaultString = name;+ picker.displayDirectory = dir;++ var rv = picker.show();+ if (rv == Ci.nsIFilePicker.returnOK || rv == Ci.nsIFilePicker.returnReplace) {+ return picker.file.path;+ }+ return null;+}++exports.saveContentToFile = function (name, content) {++ var filePath = getSaveLocationDialog(name);+ var aFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);+ aFile.initWithPath(filePath);+ //aFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0600);+ var foStream = Cc["@mozilla.org/network/file-output-stream;1"].+ createInstance(Ci.nsIFileOutputStream);++ foStream.init(aFile, 0x02 | 0x08 | 0x20, 666, 0); // readwrite, create, truncate++ var converter = Cc["@mozilla.org/intl/converter-output-stream;1"].+ createInstance(Ci.nsIConverterOutputStream);+ converter.init(foStream, "UTF-8", 0, 0);+ converter.writeString(content);+ converter.close();+};++exports.saveContentToBinaryFile = function (name, content) {++ var filePath = getSaveLocationDialog(name);+ var aFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);+ aFile.initWithPath(filePath);+ //aFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0600);+ var stream = Cc["@mozilla.org/network/safe-file-output-stream;1"].+ createInstance(Ci.nsIFileOutputStream);+ stream.init(aFile, 0x04 | 0x08 | 0x20, 600, 0); // readwrite, create, truncate++ stream.write(content, content.length);+ if (stream instanceof Ci.nsISafeOutputStream) {+ stream.finish();+ } else {+ stream.close();+ }+};++exports.saveURLToFile = function(name, url) {+ var pageWorker = require("sdk/page-worker");+ // This content script sends header titles from the page to the add-on:+ var script = "self.postMessage(document.body.innerHTML)";+ // Create a page worker that loads Wikipedia:+ pageWorker.Page({+ contentURL: url,+ contentScript: script,+ contentScriptWhen: "ready",+ onMessage: function(message) {+ exports.saveContentToFile(name, message);+ this.destroy();+ }+ });+};diff --git a/data/mozilla/main.js b/data/mozilla/main.jsindex 22e33d8f4..485145b96 100644--- a/data/mozilla/main.js+++ b/data/mozilla/main.js@@ -21,6 +21,7 @@ var tabs = require("sdk/tabs");var unload = require('sdk/system/unload');var Panel = require('sdk/panel').Panel;var selection = require("sdk/selection");+var capture = require("capture");//var userstyles = require("userstyles");var {Cc, Ci, Cu@@ -123,13 +124,10 @@ function initToobarButton() {contentScriptFile: [data.url('libs/jquery/jquery-2.1.1.min.js'),data.url('libs/select2/select2.min.js'),- //data.url('libs/filesaver.js/FileSaver.js'),data.url('libs/dompurify/purify.js'),data.url('mozilla/popup.js')],- //contentScriptWhen: "ready",onShow: function() {- var ttt = tabs.activeTab.title;tagspacesPanel.port.emit("show", selection.html, tabs.activeTab.title);},onHide: function() {@@ -146,19 +144,21 @@ function initToobarButton() {openTagSpacesInNewTab();});- tagspacesPanel.port.on('saveSelectionAsHtml', function(content) {- tagspacesPanel.hide();- console.log("saveSelectionAsHtml: " + content);+ tagspacesPanel.port.on('saveSelectionAsHtml', function(name, content) {+ tagspacesPanel.hide();+ capture.saveContentToFile(name, content);});- tagspacesPanel.port.on('saveAsMHTML', function(content){+ tagspacesPanel.port.on('saveAsMHTML', function(name) {tagspacesPanel.hide();- console.log("saveAsMHTML: " + content);+ capture.saveURLToFile(name, tabs.activeTab.url.toString());});- tagspacesPanel.port.on('saveScreenshot', function(content){+ tagspacesPanel.port.on('saveScreenshot', function(name) {tagspacesPanel.hide();- console.log("saveScreenshot" + content);+ var screenCastData = capture.captureTab();+ var content = capture.dataURItoBlob(screenCastData);+ capture.saveContentToBinaryFile(name, content);});diff --git a/data/mozilla/popup.js b/data/mozilla/popup.jsindex f1ff9b623..d83525bf5 100644--- a/data/mozilla/popup.js+++ b/data/mozilla/popup.js@@ -14,34 +14,45 @@function init(selection) {console.log("Mozilla Popup init...");-- loadSettingsLocalStorage();-$("#startTagSpaces").on("click", function(e) {self.port.emit('openNewTab', e.toString());});-$("#saveAsMhtml").on('click', saveAsMHTML);+ $("#saveSelectionAsHtml").on("click", saveSelectionAsHtml);+ $("#saveScreenshot").on("click", saveScreenshot);+ }- $("#saveSelectionAsHtml").on("click", function() {-- if(currentSelection) {- var content = prepareContent(currentSelection);- self.port.emit('saveSelectionAsHtml', content);+ function saveSelectionAsHtml() {+ if (currentSelection) {+ var content = prepareContent(currentSelection);++ if (tags) {+ tags = tags.split(",").join(" ");+ self.port.emit('saveSelectionAsHtml', $('#title').val() + ' [' + tags + '].html', content);} else {- alert("Please select text");+ self.port.emit('saveSelectionAsHtml', $('#title').val() + '.html', content);}- });-- $("#saveScreenshot").on("click", saveScreenshot);+ } else {+ alert("Please select text");+ }}function saveAsMHTML() {- self.port.emit('saveAsMHTML');+ if (tags) {+ tags = tags.split(",").join(" ");+ self.port.emit('saveAsMHTML', $('#title').val() + ' [' + tags + '].html');+ } else {+ self.port.emit('saveAsMHTML', $('#title').val() + '.html');+ }}function saveScreenshot() {- self.port.emit('saveScreenshot');+ if (tags) {+ tags = tags.split(",").join(" ");+ self.port.emit('saveScreenshot', $('#title').val() + ' [' + tags + '].png');+ } else {+ self.port.emit('saveScreenshot', $('#title').val() + '.png');+ }}function prepareContent(uncleanedHTML) {@@ -56,10 +67,12 @@function loadSettingsLocalStorage() {try {var settings = JSON.parse(localStorage.getItem('tagSpacesSettings'));- //console.log("Settings: "+JSON.stringify(tmpSettings));+ //console.log("Settings: " + JSON.stringify(settings));if (settings !== null) {htmlTemplate = settings.newHTMLFileContent;tagLibrary = settings.tagGroups;+ } else {+ htmlTemplate = "<html><body></body></html>";}console.log("Loaded settings from local storage: " + JSON.stringify(tagLibrary));} catch (ex) {@@ -88,12 +101,11 @@self.port.on("show", function(selection, title) {//self.postMessage('show', "ddd1");+ loadSettingsLocalStorage();currentSelection = selection;var tagList = extractAllTags(tagLibrary);- console.log("currentSelection: " + currentSelection);-+$('#title').val(title);-$('#title').focus();$('#tags').select2('data', null);@@ -104,7 +116,6 @@minimumInputLength: 2,selectOnBlur: true});-});self.port.on("hide", function(){