Skip to content

Commit 5b08ee9

Browse files
authored
Merge pull request #4 from arielszmerla/feat/add-shortcut
Feat/add shortcut
2 parents 3f48e01 + 08333ec commit 5b08ee9

6 files changed

Lines changed: 104 additions & 10 deletions

File tree

code/background.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,37 @@
1+
import { cleanAwsUrl } from './utils.js';
2+
13
chrome.runtime.onInstalled.addListener(() => {
24
console.log("AWS Console URL Cleaner extension installed.");
35
});
46

57
chrome.runtime.onStartup.addListener(() => {
68
console.log("AWS Console URL Cleaner extension started.");
7-
});
9+
});
10+
11+
chrome.commands.onCommand.addListener(async (command) => {
12+
if (command === "clean-url") {
13+
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
14+
if (!tab || !tab.url) return;
15+
16+
const cleaned = cleanAwsUrl(tab.url);
17+
if (!cleaned) return;
18+
19+
await copyToClipboard(tab.id, cleaned);
20+
}
21+
});
22+
23+
async function copyToClipboard(tabId, text) {
24+
// Try to send the message first
25+
try {
26+
await chrome.tabs.sendMessage(tabId, { action: "copyToClipboard", text });
27+
} catch (e) {
28+
// If the content script is not present, inject it and try again
29+
await chrome.scripting.executeScript({
30+
target: { tabId },
31+
files: ["code/content.js"]
32+
});
33+
// Wait a moment for the script to load, then resend the message
34+
await new Promise(resolve => setTimeout(resolve, 100));
35+
await chrome.tabs.sendMessage(tabId, { action: "copyToClipboard", text });
36+
}
37+
}

code/content.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
2+
if (msg.action === "copyToClipboard" && msg.text) {
3+
// Fallback for clipboard write
4+
const textarea = document.createElement('textarea');
5+
textarea.value = msg.text;
6+
document.body.appendChild(textarea);
7+
textarea.select();
8+
document.execCommand('copy');
9+
document.body.removeChild(textarea);
10+
sendResponse({ success: true });
11+
}
12+
return true;
13+
});

code/popup.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
<link rel="stylesheet" href="popup.css">
66
</head>
77
<body>
8-
<h3>Clean AWS Console URL</h3>
8+
<h3>Clean/Copy AWS Console URL</h3>
99
<button id="cleanBtn">Clean & Copy URL</button>
1010
<div id="status"></div>
11-
<script src="popup.js"></script>
11+
<script type="module" src="popup.js"></script>
1212
</body>
1313
</html>

code/popup.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1+
// code/popup.js
2+
import { cleanAwsUrl } from './utils.js';
3+
14
document.getElementById("cleanBtn").onclick = () => {
25
chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
3-
const url = new URL(tabs[0].url);
4-
const cleanedHost = url.host.replace(/^\d{12}-[a-z0-9]+\./, '');
5-
const cleanedUrl = `${url.protocol}//${cleanedHost}${url.pathname}${url.search}${url.hash}`;
6+
const originalUrl = tabs[0].url;
7+
const cleanedUrl = cleanAwsUrl(originalUrl);
8+
9+
if (!cleanedUrl) {
10+
document.getElementById("status").textContent = "⚠️ Invalid or unsupported URL.";
11+
return;
12+
}
613

7-
// Copy to clipboard
814
navigator.clipboard.writeText(cleanedUrl).then(() => {
915
document.getElementById("status").textContent = "✅ Cleaned URL copied!";
1016
}).catch(() => {
1117
document.getElementById("status").textContent = "❌ Failed to copy.";
1218
});
1319
});
14-
};
20+
};

code/utils.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// code/utils.js
2+
export function cleanAwsUrl(rawUrl) {
3+
try {
4+
const url = new URL(rawUrl);
5+
const cleanedHost = url.host.replace(/^\d{12}-[a-z0-9]+\./, '');
6+
return `${url.protocol}//${cleanedHost}${url.pathname}${url.search}${url.hash}`;
7+
} catch (e) {
8+
return null;
9+
}
10+
}

manifest.json

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
"name": "AWS Console URL Cleaner",
44
"version": "0.1",
55
"description": "Cleans AWS console URLs by removing account-specific prefixes.",
6-
"permissions": ["tabs", "clipboardWrite", "activeTab", "scripting"],
6+
"permissions": [
7+
"tabs",
8+
"clipboardWrite",
9+
"activeTab",
10+
"scripting"
11+
],
712
"action": {
813
"default_popup": "code/popup.html",
914
"default_icon": {
@@ -16,6 +21,36 @@
1621
"128": "assets/imgs/icon.png"
1722
},
1823
"background": {
19-
"service_worker": "code/background.js"
24+
"service_worker": "code/background.js",
25+
"type": "module"
26+
},
27+
"web_accessible_resources": [
28+
{
29+
"resources": [
30+
"code/utils.js"
31+
],
32+
"matches": [
33+
"<all_urls>"
34+
]
35+
}
36+
],
37+
"content_scripts": [
38+
{
39+
"matches": [
40+
"<all_urls>"
41+
],
42+
"js": [
43+
"code/content.js"
44+
]
45+
}
46+
],
47+
"commands": {
48+
"clean-url": {
49+
"suggested_key": {
50+
"default": "Ctrl+Shift+U",
51+
"mac": "Command+Shift+U"
52+
},
53+
"description": "Clean and copy AWS URL"
54+
}
2055
}
2156
}

0 commit comments

Comments
 (0)