Ver Fonte

Upload project

SCANIATM há 5 anos atrás
commit
6a41dbd56d

+ 5 - 0
chrome.manifest

@@ -0,0 +1,5 @@
+content clipboard chrome/content/ contentaccessible=yes
+skin clipboard classic/1.0 chrome/skin/
+locale clipboard en-US chrome/locale/en-US/
+locale clipboard de-DE chrome/locale/de-DE/
+overlay chrome://messenger/content/messengercompose/messengercompose.xul chrome://clipboard/content/main.xul

+ 33 - 0
chrome/content/main.xul

@@ -0,0 +1,33 @@
+<?xml version="1.0"?>
+<?xml-stylesheet href="chrome://clipboard/skin/style.css" type="text/css"?>
+<!DOCTYPE overlay SYSTEM "chrome://clipboard/locale/clipboard.dtd" >
+<overlay id="clipboard-overlay" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
+  <stringbundleset id="stringbundleset">
+    <stringbundle id="stringBundle" src="chrome://clipboard/locale/clipboard.properties"/>
+  </stringbundleset>
+  <commandset id="composeCommands">
+    <commandset id="attachFromClipboardCmds" commandupdater="true" events="clipboard" oncommandupdate="clipboardAttachment.updateCommand()">
+      <command id="attachFromClipboardCmd" oncommand="clipboardAttachment.attachFromClipboard()" />
+    </commandset>
+  </commandset>
+  <keyset id="tasksKeys">
+    <keyset id="attachFromClipboardKeys">
+      <key id="attachFromClipboardKey" modifiers="shift alt" key="v" command="attachFromClipboardCmd" />
+    </keyset>
+  </keyset>
+  <menupopup id="button-attachPopup">
+    <menuitem id="button-attachPopup_attachClipboard" insertafter="button-attachPopup_attachPageItem"
+      label="&attachFromClipboard.label;" accesskey="&attachFromClipboard.accesskey;"
+      command="attachFromClipboardCmd" />
+  </menupopup>
+  <menupopup id="menu_AttachPopup">
+    <menuitem id="menu_Attach_Clipboard" insertafter="menu_pasteNoFormatting" label="&attachFromClipboard.label;" accesskey="&attachFromClipboard.accesskey;"
+      command="attachFromClipboardCmd" key="attachFromClipboardKey" position="4" />
+  </menupopup>
+  <menupopup id="msgComposeAttachmentListContext">
+    <menuitem id="msgComposeAttachmentListContext_attachClipboard" insertafter="attachmentListContext_attachPageItem"
+      label="&attachFromClipboardContextMenu.label;" accesskey="&attachFromClipboard.accesskey;"
+      command="attachFromClipboardCmd" />
+  </menupopup>
+  <script type="application/javascript" src="chrome://clipboard/content/overlay.js"/>
+</overlay>

+ 211 - 0
chrome/content/overlay.js

@@ -0,0 +1,211 @@
+var clipboardAttachment = (function () {
+    var { classes: Cc, interfaces: Ci, utils: Cu } = Components;
+
+    Cu.import('resource://gre/modules/Services.jsm');
+    Cu.import("resource://gre/modules/NetUtil.jsm");
+
+    function getPasteImageType() {
+        var prefs = Cc["@mozilla.org/preferences-service;1"]
+            .getService(Ci.nsIPrefService)
+            .getBranch("clipboard.");
+        var value = prefs.getIntPref("paste_image_type");
+
+        return value;
+    }
+
+    function getOrderedFlavors() {
+        var flavors = [];
+
+        switch (getPasteImageType()) {
+            case 0:
+                flavors.push("image/jpeg");
+                flavors.push("image/jpg");
+                flavors.push("image/png");
+                flavors.push("image/gif");
+                break;
+            case 1:
+            default:
+                flavors.push("image/png");
+                flavors.push("image/jpeg");
+                flavors.push("image/jpg");
+                flavors.push("image/gif");
+                break;
+            case 2:
+                flavors.push("image/gif");
+                flavors.push("image/jpeg");
+                flavors.push("image/jpg");
+                flavors.push("image/png");
+                break;
+        }
+
+        flavors.push("application/x-moz-file");
+        flavors.push("text/x-moz-url");
+        flavors.push("text/uri-list");
+        flavors.push("text/html");
+        flavors.push("text/unicode");
+
+        return flavors;
+    }
+
+    function getDataFromClipboard() {
+        var trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);
+
+        trans.init(null);
+        var flavors = getOrderedFlavors();
+        for (i = 0; i < flavors.length; i++) {
+            trans.addDataFlavor(flavors[i]);
+        }
+
+        Services.clipboard.getData(trans, Services.clipboard.kGlobalClipboard);
+
+        var flavor = {};
+        var data = {};
+        var len = {};
+
+        trans.getAnyTransferData(flavor, data, len);
+
+        if (flavor.value === "text/uri-list") {
+            // if the flavor is text/uri-list, the data will be garbage
+            // (UTF-8 interpreted as UTF-16?), so we'll get the string again
+            // using text/unicode which works. 
+            // We'll still return text/uri-list as flavor, though, so the resulting
+            // string can be handled as a list of URIs.
+            trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);
+
+            trans.init(null);
+            trans.addDataFlavor("text/unicode");
+
+            Services.clipboard.getData(trans, Services.clipboard.kGlobalClipboard);
+
+            trans.getAnyTransferData(flavor, data, len);
+
+            return { flavor: "text/uri-list", data: data.value, length: len.value };
+        }
+
+        return { flavor: flavor.value, data: data.value, length: len.value };
+    }
+
+    function createFile(name) {
+        var file = FileUtils.getFile("TmpD", [name]);
+        file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE);
+        return file;
+    }
+
+    function writeString(data, name, callback) {
+        var file = createFile(name);
+        var ostream = FileUtils.openSafeFileOutputStream(file);
+        var converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Ci.nsIScriptableUnicodeConverter);
+        converter.charset = "UTF-8";
+        var istream = converter.convertToInputStream(data);
+
+        NetUtil.asyncCopy(istream, ostream, function () {
+            callback(file);
+        });
+    }
+
+    function showException(ex) {
+        var stringBundle = document.getElementById("stringBundle");
+        var promptService = Cc["@mozilla.org/embedcomp/prompt-service;1"].getService(Ci.nsIPromptService);
+        var title = stringBundle.getString("errorTitle");
+        var msg = stringBundle.getString("errorMessage") + ": " + ex;
+        promptService.alert(window, title, msg);
+    }
+
+    function addFileAttachment(file) {
+        try {
+            var attachment = FileToAttachment(file);
+            AddAttachments([attachment]);
+            createdFiles.push(file);
+        } catch (ex) {
+            showException(ex);
+        }
+    }
+
+    var createdFiles = [];
+
+    var my = {
+        canAttach: function () {
+            var clipboard = Services.clipboard;
+            var flavors = getOrderedFlavors();
+            var hasFlavors = clipboard.hasDataMatchingFlavors(flavors, flavors.length, clipboard.kGlobalClipboard);
+            return hasFlavors;
+        },
+        attachFromClipboard: function () {
+            try {
+                if (my.canAttach()) {
+                    var data = getDataFromClipboard();
+
+                    if (data.flavor.indexOf("image/") === 0) {
+                        var extension = data.flavor === "image/png" ? "png" : (data.flavor === "image/gif" ? "gif" : "jpg");
+                        var file = createFile("image." + extension);
+                        var output = FileUtils.openSafeFileOutputStream(file);
+                        NetUtil.asyncCopy(data.data, output, function () {
+                            addFileAttachment(file);
+                        });
+                    } else if (data.flavor === "text/html") {
+                        var html = data.data.QueryInterface(Ci.nsISupportsString).data;
+                        writeString(data.data, "document.html", function (file) {
+                            addFileAttachment(file);
+                        });
+                    } else if (data.flavor === "text/x-moz-url") {
+                        var text = data.data.QueryInterface(Ci.nsISupportsString).data;
+                        var lines = text.split('\n');
+                        var attachment = Cc["@mozilla.org/messengercompose/attachment;1"].createInstance(Ci.nsIMsgAttachment);
+                        attachment.url = lines[0];
+                        if (lines.length > 1) attachment.name = lines[1];
+                        if (lines.length > 2) attachment.size = lines[2];
+                        AddAttachments([attachment]);
+                    } else if (data.flavor === "text/uri-list") {
+                        var text = data.data.QueryInterface(Ci.nsISupportsString).data;
+                        var lines = text.split('\n');
+                        var attachments = [];
+                        for (i = 0; i < lines.length; i++) {
+                            var attachment = Cc["@mozilla.org/messengercompose/attachment;1"].createInstance(Ci.nsIMsgAttachment);
+                            var fileUri = lines[i];
+                            if (!fileUri.startsWith("file:///")) {
+                                fileUri = OS.Path.toFileURI(fileUri);
+                            }
+                            attachment.url = fileUri;
+                            attachments.push(attachment);
+                        }
+                        AddAttachments(attachments);
+                    } else if (data.flavor === "text/unicode" || data.flavor === "text/plain") {
+                        var text = data.data.QueryInterface(Ci.nsISupportsString).data;
+                        writeString(data.data, "document.txt", function (file) {
+                            addFileAttachment(file);
+                        });
+                    } else if (data.flavor === "application/x-moz-file") {
+                        var file = data.data.QueryInterface(Ci.nsIFile);
+                        var attachment = FileToAttachment(file);
+                        AddAttachments([attachment]);
+                    }
+                }
+            } catch (ex) {
+                showException(ex);
+            }
+        },
+        updateCommand: function () {
+            var canAttach = my.canAttach();
+            goSetCommandEnabled("attachFromClipboardCmd", canAttach);
+        }
+    };
+
+    function unload() {
+        for (i = 0; i < createdFiles.length; i++) {
+            createdFiles[i].remove(false);
+        }
+        createdFiles = [];
+    }
+
+    window.addEventListener("load", function () {
+        var menuEditPopup = document.getElementById("menu_EditPopup");
+        menuEditPopup.addEventListener("popupshowing", my.updateCommand);
+
+        var msgComposeWindow = document.getElementById("msgcomposeWindow");
+        msgComposeWindow.addEventListener("compose-window-close", unload);
+        msgComposeWindow.addEventListener("compose-window-unload", unload);
+    });
+    window.addEventListener("unload", unload);
+
+    return my;
+}());

+ 3 - 0
chrome/locale/de-DE/clipboard.dtd

@@ -0,0 +1,3 @@
+<!ENTITY attachFromClipboard.label "Aus Zwischenablage">
+<!ENTITY attachFromClipboard.accesskey "Z">
+<!ENTITY attachFromClipboardContextMenu.label "Aus Zwischenablage anfügen">

+ 2 - 0
chrome/locale/de-DE/clipboard.properties

@@ -0,0 +1,2 @@
+errorTitle=Einfügen aus Zwischenablage
+errorMessage=Fehler beim Einfügen aus der Zwischenablage

+ 3 - 0
chrome/locale/en-US/clipboard.dtd

@@ -0,0 +1,3 @@
+<!ENTITY attachFromClipboard.label "From clipboard">
+<!ENTITY attachFromClipboard.accesskey "c">
+<!ENTITY attachFromClipboardContextMenu.label "Attach from clipboard">

+ 2 - 0
chrome/locale/en-US/clipboard.properties

@@ -0,0 +1,2 @@
+errorTitle=Attach from Clipboard
+errorMessage=Failed to create attachment from the clipboard

+ 13 - 0
chrome/skin/icon.svg

@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<svg
+   xmlns="http://www.w3.org/2000/svg"
+   version="1.1"
+   width="64"
+   height="64"
+   viewBox="0 0 64 64"
+>
+  <path
+     d="m 57.864407,8.1355932 -18,0 c 0,-4.42 -3.58,-7.99999998 -8,-7.99999998 -4.42,0 -8,3.57999998 -8,7.99999998 l -18.0000002,0 c -1.104,0 -2,0.896 -2,1.9999998 l 0,52 c 0,1.104 0.896,2 2,2 l 52.0000002,0 c 1.104,0 2,-0.896 2,-2 l 0,-52 c 0,-1.1039998 -0.896,-1.9999998 -2,-1.9999998 z m -26,-4 c 2.208,0 4,1.792 4,4 0,2.2079998 -1.792,3.9999998 -4,3.9999998 -2.208,0 -4,-1.792 -4,-3.9999998 0,-2.208 1.792,-4 4,-4 z m 24,55.9999998 -48.0000002,0 0,-48 8.0000002,0 0,6 c 0,1.104 0.896,2 2,2 l 28,0 c 1.104,0 2,-0.896 2,-2 l 0,-6 8,0 0,48 z" />
+  <path
+     d="m 37.265382,33.813844 -2.03,-2.028 -10.15,10.148001 c -1.682,1.682 -1.682,4.408 0,6.088 1.682,1.68 4.408,1.682 6.09,0 l 12.18,-12.178001 c 2.804,-2.802 2.804,-7.346 0,-10.148 -2.804,-2.802 -7.348,-2.804 -10.15,0 l -12.788,12.786 c -0.01,0.01 -0.02,0.018 -0.028,0.026 -3.91,3.910001 -3.91,10.246001 0,14.154001 3.91,3.908 10.246,3.908 14.156,0 0.008,-0.008 0.016,-0.018 0.026,-0.028 l 0.002,0.002 8.73,-8.728 -2.03,-2.028 -8.73,8.726 c -0.01,0.008 -0.018,0.018 -0.026,0.026 -2.784,2.784 -7.312,2.784 -10.096,0 -2.784,-2.784 -2.784,-7.31 0,-10.094001 0.01,-0.01 0.018,-0.018 0.028,-0.026 l -0.002,-0.002 12.79,-12.786 c 1.678,-1.68 4.41,-1.68 6.09,0 1.68,1.68 1.678,4.41 0,6.088 l -12.18,12.178001 c -0.56,0.56 -1.47,0.56 -2.03,0 -0.56,-0.56 -0.56,-1.47 0,-2.028 l 10.15,-10.150001 z" />
+</svg>

+ 20 - 0
manifest.json

@@ -0,0 +1,20 @@
+{
+  "manifest_version": 2,
+  "applications": {
+    "gecko": {
+      "id": "clipboard@scaniatm.net",
+      "strict_min_version": "68.0"
+    }
+  },
+  "name": "Attach from Clipboard",
+  "description": "Create message attachments directly from the system clipboard.",
+  "version": "1.3.68",
+  "homepage_url": "https://scaniatm.net",
+  "icons": {
+    "48": "chrome/skin/icon.svg",
+    "96": "chrome/skin/icon.svg"
+  },
+  "legacy": {
+    "type": "xul"
+  }
+}