Make Copy URL use native clipboard host

This commit is contained in:
David Heinemeier Hansson
2026-07-22 17:25:28 -07:00
parent 6a047462e8
commit 475211a5d4
12 changed files with 162 additions and 103 deletions
@@ -1,65 +0,0 @@
// NOTE: this file is intentionally version-numbered (background-2.js). Chromium
// caches the service worker for a command-line --load-extension and does NOT
// re-register it when the script changes in place, so an updated worker never
// takes effect on existing installs. Changing the filename is a new script URL,
// which forces a fresh registration. If you change this worker's code, rename it
// (background-3.js, ...) and update manifest.json's background.service_worker.
let creatingOffscreenDocument;
async function ensureOffscreenDocument() {
if (await chrome.offscreen.hasDocument()) return;
if (!creatingOffscreenDocument) {
creatingOffscreenDocument = chrome.offscreen.createDocument({
url: 'offscreen.html',
reasons: ['CLIPBOARD'],
justification: 'Copy the active tab URL to the clipboard'
}).finally(() => {
creatingOffscreenDocument = undefined;
});
}
await creatingOffscreenDocument;
}
async function copyUrl(url) {
if (!url) return;
try {
await ensureOffscreenDocument();
const copied = await chrome.runtime.sendMessage({
target: 'offscreen',
type: 'copy-url',
url
});
if (!copied) return;
// The omarchy notification shell renders a chromium toast slim (no icon
// slot) when the summary begins with a glyph followed by 2+ spaces and the
// body is empty. iconUrl is a required field, so keep a 1x1 transparent png.
chrome.notifications.create({
type: 'basic',
iconUrl: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAC0lEQVR4nGNgAAIAAAUAAXpeqz8AAAAASUVORK5CYII=',
title: '󰅍 URL copied to clipboard',
message: ''
});
} catch (error) {
console.error('[copy-url] failed:', error);
}
}
// Keyboard shortcut (Alt+Shift+L).
chrome.commands.onCommand.addListener((command) => {
if (command !== 'copy-url') return;
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
copyUrl(tabs[0] && tabs[0].url);
});
});
// Clicking the extension's toolbar icon.
chrome.action.onClicked.addListener((tab) => {
copyUrl(tab && tab.url);
});
@@ -0,0 +1,23 @@
// Keep this filename versioned. Chromium caches service workers for extensions
// loaded via --load-extension, so a new URL forces registration of new code.
function copyUrl(url) {
if (!url) return;
// The native host owns both the Wayland clipboard and confirmation toast.
chrome.runtime.sendNativeMessage('com.omarchy.copy_url', { url }, () => {
void chrome.runtime.lastError;
});
}
chrome.commands.onCommand.addListener((command) => {
if (command !== 'copy-url') return;
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
copyUrl(tabs[0] && tabs[0].url);
});
});
chrome.action.onClicked.addListener((tab) => {
copyUrl(tab && tab.url);
});
@@ -1,11 +1,11 @@
{
"manifest_version": 3,
"name": "Copy URL",
"version": "1.3",
"version": "1.5",
"description": "Copy current URL to clipboard, this extension is installed by Omarchy",
"action": { "default_title": "Copy URL" },
"key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxbOMKgGcG20uelP6lx5OnuafGP9gNR1ajzURuQ45tv/Is5sc1tn9ii9PsKxpzDzPL9Z5lAqrTFgEcKHyTJnZXAGOMIruB0odJ0BW6eoQ3rxaQ/fBK2yGN7FZAfygPOWHj+Fdrh2eYQ7Ap92Dmlz8x0ceGyais415KmeevVcPwS7jjHFd1bGBp17cEMOBQcOx/3zCnko1hGtCzSdyZQl3cNMebW/FGvmxlPUMLVfWhysfFUuzxMRRf58+/j0mo/9iKQ0uTuxbQ8+W64OmYOVR87IXYNgbo+hEtORjiQhVg1KzaBx1bZ6clQMv96W/xYy1cIzUA5yI9y7lNr7n9zKijwIDAQAB",
"permissions": ["activeTab", "clipboardWrite", "notifications", "offscreen"],
"permissions": ["activeTab", "nativeMessaging"],
"icons": {
"16": "icon.png",
"48": "icon.png",
@@ -17,5 +17,5 @@
"description": "Copy URL"
}
},
"background": {"service_worker": "background-2.js"}
"background": {"service_worker": "background-4.js"}
}
@@ -1,9 +0,0 @@
<!doctype html>
<html>
<head>
<script src="offscreen.js"></script>
</head>
<body>
<textarea id="target"></textarea>
</body>
</html>
@@ -1,17 +0,0 @@
chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
if (message.target !== 'offscreen' || message.type !== 'copy-url') return;
// Offscreen documents are never focused, so navigator.clipboard.writeText
// rejects with "Document is not focused". execCommand('copy') has no focus
// requirement and is authorized by the clipboardWrite permission.
try {
const target = document.getElementById('target');
target.value = message.url;
target.focus();
target.select();
sendResponse(document.execCommand('copy'));
} catch (error) {
console.error('[copy-url offscreen] error:', error);
sendResponse(false);
}
});
@@ -0,0 +1,9 @@
{
"name": "com.omarchy.copy_url",
"description": "Omarchy Copy URL clipboard host",
"path": "__HOST_PATH__",
"type": "stdio",
"allowed_origins": [
"chrome-extension://bgpiichlckmfanooecilcjemknkcpngb/"
]
}