-
Notifications
You must be signed in to change notification settings - Fork 1
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 WebExtensions #1
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,62 +11,119 @@ | |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
async function getCurrentWindow() | ||
{ | ||
let windows = await messenger.windows.getAll(); | ||
import { parse5322 } from "./email-address-parser/email-addresses.mjs"; | ||
|
||
for (let window of windows) { | ||
if ((window.type === "messageDisplay" || window.type === "normal") | ||
&& window.focused === true) | ||
return window; | ||
// There is no real need for these functions to be async. But being able to use | ||
// the .then() callback of the return value for further manipulations is quite handy. | ||
async function getFirstHeader(arr) { | ||
if (Array.isArray(arr) && arr.length > 0) { | ||
return arr[0]; | ||
} | ||
return undefined; | ||
} | ||
async function getAllHeader(arr) { | ||
if (Array.isArray(arr) && arr.length > 0) { | ||
return arr; | ||
} | ||
return undefined; | ||
} | ||
function parseDisplayName(addr) { | ||
let rv = parse5322.parseOneAddress(addr); | ||
return { | ||
name: rv.name, | ||
email: rv.address, | ||
} | ||
} | ||
// 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; | ||
} | ||
// Compatibility layer for former experiment. | ||
async function getMsgData(messageId) { | ||
let full = await browser.messages.getFull(messageId); | ||
let date = await getFirstHeader(full.headers["date"]).then( | ||
value => value ? new Date(value) : value | ||
); | ||
let from = await getAllHeader(full.headers["from"]).then( | ||
value => value ? value.map(addr => parseDisplayName(addr)) : value | ||
); | ||
let replyTo = await getAllHeader(full.headers["reply-to"]).then( | ||
value => value ? value.map(addr => parseDisplayName(addr)) : value | ||
); | ||
let subject = await getFirstHeader(full.headers["subject"]); | ||
let body = getBody(full.parts); | ||
|
||
function main() | ||
{ | ||
if (!body) { | ||
return null; | ||
} | ||
|
||
return { | ||
header: { date, from, replyTo, subject }, | ||
body, | ||
isPatch: (body.indexOf("\n---") >= 0 && body.indexOf("\n+++") >= 0) || body.indexOf("\ndiff --git") >= 0 | ||
} | ||
} | ||
|
||
function main() { | ||
// onMessage listeners should not be async, but instead return a Promise for | ||
// active responses and false if they are not responsible for the given request. | ||
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); | ||
|
||
messenger.messageDisplayAction.setBadgeBackgroundColor( | ||
{tabId: sender.tab.id, color: "green"}); | ||
{ tabId: sender.tab.id, color: "green" }); | ||
messenger.messageDisplayAction.setBadgeText( | ||
{tabId: sender.tab.id, text: "✔"}); | ||
{ tabId: sender.tab.id, text: "✔" }); | ||
setTimeout(() => { | ||
messenger.messageDisplayAction.setBadgeText( | ||
{tabId: sender.tab.id, text: null}); | ||
{ tabId: sender.tab.id, text: null }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These three are examples for style changes I was referring to. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will try to undo the automatic pretty format changes caused by Visual Studio Code tomorrow. |
||
}, 500); | ||
return Promise.resolve(); | ||
} | ||
return false; | ||
jan-kiszka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
|
||
messenger.messageDisplayAction.onClicked.addListener(tab => { | ||
messenger.tabs.executeScript(tab.id, {file: "content-script.js"}); | ||
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); | ||
|
||
/* detect patch pattern in the body */ | ||
if ((msg.body.indexOf("\n---") >= 0 && msg.body.indexOf("\n+++") >= 0) || msg.body.indexOf("\ndiff --git") >= 0) { | ||
messenger.messageDisplay.onMessageDisplayed.addListener(async (tab, message) => { | ||
let msg = await getMsgData(message.id); | ||
if (msg && msg.isPatch) { | ||
messenger.messageDisplayAction.enable(tab.id); | ||
} else { | ||
messenger.messageDisplayAction.disable(tab.id); | ||
|
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> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2013 Fog Creek Software | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
https://github.com/jackbearheart/email-addresses |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reads like as if there could be a better way. However, to my understanding, there is no way for the content script to access
browser.messages
directly. Therefore, we need such an interface, no?