Skip to content

Commit

Permalink
browserpass copy and fill actions work for FF and Chrome MV3, need to…
Browse files Browse the repository at this point in the history
… troubleshoot alarm to clear clipboard. browserpass#320
  • Loading branch information
Patrick Miller committed Dec 31, 2024
1 parent de22957 commit f948cc2
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 30 deletions.
85 changes: 66 additions & 19 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,19 +170,47 @@ async function updateMatchingPasswordsCount(tabId, forceRefresh = false) {
* @return void
*/
async function copyToClipboard(text, clear = true) {
await setupOffscreenDocument("offscreen/offscreen.html");
chrome.runtime.sendMessage({
type: "copy-data-to-clipboard",
target: "offscreen-doc",
data: text,
});
console.debug(`copyToClipboard(${text}, ${clear})`);
if (isChrome()) {
await setupOffscreenDocument("offscreen/offscreen.html");
chrome.runtime.sendMessage({
type: "copy-data-to-clipboard",
target: "offscreen-doc",
data: text,
});
} else {
document.addEventListener(
"copy",
function (e) {
e.clipboardData.setData("text/plain", text);
e.preventDefault();
},
{ once: true }
);
document.execCommand("copy");
}

// @TODO: not sure alarm is firing when background is idle/inactive
if (clear) {
lastCopiedText = text;
chrome.alarms.create("clearClipboard", { delayInMinutes: 1 });
}
}

/**
*
*/
function isChrome() {
const ua = navigator.userAgent;
const matches = ua.match(/(chrom)/i) || [];
if (Object.keys(matches).length > 2 && /chrom/i.test(matches[1])) {
console.debug(`'isChrome == true`);
return true;
}
console.debug(`isChrome == false`);
return false;
}

/**
* Read plain text from clipboard
*
Expand All @@ -191,22 +219,35 @@ async function copyToClipboard(text, clear = true) {
* @return string The current plaintext content of the clipboard
*/
async function readFromClipboard() {
await setupOffscreenDocument("offscreen/offscreen.html");
if (isChrome()) {
await setupOffscreenDocument("offscreen/offscreen.html");

const response = await chrome.runtime.sendMessage({
type: "read-from-clipboard",
target: "offscreen-doc",
});
const response = await chrome.runtime.sendMessage({
type: "read-from-clipboard",
target: "offscreen-doc",
});

if (response.status != "ok") {
console.error(
"failure reading from clipboard in offscreen document",
response.message || undefined
);
return;
}
if (response.status != "ok") {
console.error(
"failure reading from clipboard in offscreen document",
response.message || undefined
);
return;
}

return response.message;
return response.message;
} else {
const ta = document.createElement("textarea");
// these lines are carefully crafted to make paste work in both Chrome and Firefox
ta.contentEditable = true;
ta.textContent = "";
document.body.appendChild(ta);
ta.select();
document.execCommand("paste");
const content = ta.value;
document.body.removeChild(ta);
return content;
}
}

/**
Expand Down Expand Up @@ -565,6 +606,12 @@ async function getFullSettings() {
defaultStore: {},
});
var response = await hostAction(configureSettings, "configure");
console.debug(
`getFullSettings => hostAction(configureSettings..${
Object.keys(configureSettings).length
}, configure)`,
{ configureSettings }
);
if (response.status != "ok") {
settings.hostError = response;
}
Expand Down
2 changes: 1 addition & 1 deletion src/manifest-chromium.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"extension_pages": "default-src 'none'; font-src 'self'; img-src 'self' data:; script-src 'self'"
},
"commands": {
"_execute_browser_action": {
"_execute_action": {
"suggested_key": {
"default": "Ctrl+Shift+L"
}
Expand Down
21 changes: 11 additions & 10 deletions src/manifest-firefox.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
{
"manifest_version": 2,
"manifest_version": 3,
"name": "Browserpass",
"description": "Browser extension for zx2c4's pass (password manager)",
"version": "3.8.0",
"author": "Maxim Baz <[email protected]>, Steve Gilberd <[email protected]>",
"homepage_url": "https://github.com/browserpass/browserpass-extension",
"background": {
"persistent": true,
"scripts": ["js/background.dist.js"]
},
"icons": {
"16": "icon16.png",
"128": "icon.png"
},
"browser_action": {
"action": {
"default_icon": {
"16": "icon16.png",
"128": "icon.svg"
Expand All @@ -32,20 +31,22 @@
"clipboardWrite",
"nativeMessaging",
"notifications",
"webRequest",
"webRequestBlocking",
"http://*/*",
"https://*/*"
"scripting",
"storage",
"webRequest"
],
"content_security_policy": "default-src 'none'; font-src 'self'; img-src 'self' data:; script-src 'self'; style-src 'self'",
"applications": {
"host_permissions": ["http://*/*", "https://*/*"],
"content_security_policy": {
"extension_pages": "default-src 'none'; font-src 'self'; img-src 'self' data:; script-src 'self'"
},
"browser_specific_settings": {
"gecko": {
"id": "[email protected]",
"strict_min_version": "54.0"
}
},
"commands": {
"_execute_browser_action": {
"_execute_action": {
"suggested_key": {
"default": "Ctrl+Shift+L"
}
Expand Down

0 comments on commit f948cc2

Please sign in to comment.