Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert to pure WebExtension and further refactorings/fixes #2

Merged
merged 7 commits into from
Aug 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
copypatch*.xpi
node_modules
package-lock.json
15 changes: 12 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,25 @@ ARCHIVE_NAME=$(PACKAGE_NAME)-$(RELEASE_TAG).xpi
PACKAGE_FILES= \
manifest.json \
copypatch*.png \
background.html \
background-script.js \
content-script.js \
api/CopyPatch/ \
node_modules/email-addresses/LICENSE \
node_modules/email-addresses/lib/email-addresses.js \
COPYING

UPDATE_VERSION='s|"version":.*|"version": "$(VERSION)",|'

all package: clean $(PACKAGE_FILES)
all package: clean node_modules $(PACKAGE_FILES)
zip -r $(ARCHIVE_NAME) $(PACKAGE_FILES)

node_modules: package.json
npm install
sed -i 's/(this)/(globalThis)/' node_modules/email-addresses/lib/email-addresses.js

distclean: clean
rm -rf node_modules

clean:
rm -f $(ARCHIVE_NAME)

Expand All @@ -51,4 +60,4 @@ release:
git commit -s manifest.json -m "Bump version number"
git tag -as $(VERSION) -m "Release $(VERSION)"

.PHONY: clean release
.PHONY: clean distclean release
93 changes: 0 additions & 93 deletions api/CopyPatch/implementation.js

This file was deleted.

18 changes: 0 additions & 18 deletions api/CopyPatch/schema.json

This file was deleted.

100 changes: 81 additions & 19 deletions background-script.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,99 @@
/*
* Copy Patch Thunderbird Add-On
*
* Copyright (c) Jan Kiszka, 2019-2020
* Copyright (c) Jan Kiszka, 2019-2023
* Copyright (c) John Bieling, 2023
*
* Authors:
* Jan Kiszka <[email protected]>
* John Bieling <[email protected]>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

async function getCurrentWindow()
import "./node_modules/email-addresses/lib/email-addresses.js";

function getFirstHeader(arr)
{
if (Array.isArray(arr) && arr.length > 0) {
return arr[0];
}
return undefined;
}

function getAllHeader(arr)
{
if (Array.isArray(arr) && arr.length > 0) {
return arr;
}
return undefined;
}

function parseDisplayName(addr)
{
let windows = await messenger.windows.getAll();
let rv = emailAddresses.parseOneAddress(addr);
return {
name: rv.name,
email: rv.address,
}
}

for (let window of windows) {
if ((window.type === "messageDisplay" || window.type === "normal")
&& window.focused === true)
return window;
/* Find first text/plain body */
function getBody(parts)
{
/* First check all parts in this level */
for (let part of parts) {
if (part.body && part.contentType == "text/plain") {
return part.body;
}
}
/* Now check all subparts */
for (let part of parts) {
if (part.parts) {
let body = getBody(part.parts);
if (body) {
return body;
}
}
}
return null;
}

async function getMsgData(messageId)
{
let full = await browser.messages.getFull(messageId);
let date = await getFirstHeader(full.headers["date"]);
let from = await getAllHeader(full.headers["from"]);
let replyTo = await getAllHeader(full.headers["reply-to"]);
let subject = await getFirstHeader(full.headers["subject"]);
let body = getBody(full.parts);

if (!body) {
return null;
}

return {
header: {
date: date ? new Date(date) : date,
from: from ? from.map(addr => parseDisplayName(addr)) : from,
replyTo: replyTo ? replyTo.map(addr => parseDisplayName(addr)) : replyTo,
subject: subject
},
body: body,
isPatch: (body.indexOf("\n---") >= 0 && body.indexOf("\n+++") >= 0) ||
body.indexOf("\ndiff --git") >= 0
}
}

function main()
{
messenger.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "getMsg") {
return messenger.CopyPatch.getSelectedMessage(sender.tab.windowId);
return browser.messageDisplay.getDisplayedMessage(sender.tab.id).then(
msg => getMsgData(msg.id)
);
}
if (request.action === "clipboardWrite") {
navigator.clipboard.writeText(request.text);
Expand All @@ -40,33 +106,29 @@ function main()
messenger.messageDisplayAction.setBadgeText(
{tabId: sender.tab.id, text: null});
}, 500);
return Promise.resolve();
}
return false;
});

messenger.messageDisplayAction.onClicked.addListener(tab => {
messenger.tabs.executeScript(tab.id, {file: "content-script.js"});
});

messenger.commands.onCommand.addListener(async (name) => {
messenger.commands.onCommand.addListener(async (name, tab) => {
if (name !== "copyPatch") {
return;
}

let window = await getCurrentWindow();
if (window) {
let tabs = await messenger.tabs.query({windowId: window.id});
if (await messenger.messageDisplayAction.isEnabled({tabId: tabs[0].id})) {
messenger.tabs.executeScript(tabs[0].id,
{file: "content-script.js"});
}
if (await messenger.messageDisplayAction.isEnabled({tabId: tab.id})) {
messenger.tabs.executeScript(tab.id, {file: "content-script.js"});
}
});

messenger.messageDisplay.onMessageDisplayed.addListener(async (tab, message) => {
msg = await messenger.CopyPatch.getSelectedMessage(tab.windowId);
let msg = await getMsgData(message.id);

/* detect patch pattern in the body */
if ((msg.body.indexOf("\n---") >= 0 && msg.body.indexOf("\n+++") >= 0) || msg.body.indexOf("\ndiff --git") >= 0) {
if (msg && msg.isPatch) {
messenger.messageDisplayAction.enable(tab.id);
} else {
messenger.messageDisplayAction.disable(tab.id);
Expand Down
7 changes: 7 additions & 0 deletions background.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script src="background-script.js" type="module"></script>
</head>
</html>
25 changes: 3 additions & 22 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,20 @@
"applications": {
"gecko": {
"id": "[email protected]",
"strict_min_version": "78.4.0",
"strict_max_version": "102.*"
"strict_min_version": "102.4.0"
}
},
"name": "Copy Patch",
"description": "Copy email content to clipboard for application as patch",
"author": "Jan Kiszka",
"version": "2.1.3",
"homepage_url": "http://git.kiszka.org/copypatch.git",
"homepage_url": "https://github.com/jan-kiszka/copypatch",
"icons": {
"32": "copypatch.png",
"64": "copypatch64.png"
},
"background": {
"scripts": [
"background-script.js"
]
"page": "background.html"
},
"permissions": [
"messagesRead",
Expand All @@ -37,21 +34,5 @@
},
"description": "Copy as patch"
}
},
"experiment_apis": {
"CopyPatch": {
"schema": "api/CopyPatch/schema.json",
"parent": {
"scopes": [
"addon_parent"
],
"paths": [
[
"CopyPatch"
]
],
"script": "api/CopyPatch/implementation.js"
}
}
}
}
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "copypatch",
"description": "Copy email content to clipboard for application as patch",
"repository": {
"type": "git",
"url": "https://github.com/jan-kiszka/copypatch.git"
},
"license": "MPL-2.0",
"dependencies": {
"email-addresses": "^5.0.0"
}
}