EN
How to make simple Google Chrome Extension
0 points
In this article, we would like to show you how to make a simple Chrome Extension.
Below we present a simple extension that after pressing Alt + C will copy the title and URL of the currently opened tab and put them in the clipboard.
Note:
Check out this article, to see how to load your extension into Chrome:
How to load extension into Google Chrome
xxxxxxxxxx
1
example_chrome_extension/
2
|
3
+-- manifest.json
4
|
5
+-- background.js
manifest.json
xxxxxxxxxx
1
{
2
"name": "Copy tool",
3
"manifest_version": 3,
4
"version": "0.1",
5
"description": "Copy title and URL of current website",
6
"commands": {
7
"copy": {
8
"suggested_key": {
9
"default": "Alt+C"
10
},
11
"description": "Copy shortcut"
12
}
13
},
14
"permissions": [
15
"activeTab",
16
"scripting",
17
"tabs"
18
],
19
"background": {
20
"service_worker": "background.js"
21
}
22
}
background.js
xxxxxxxxxx
1
const copyTitleAndURL = () => {
2
const dataToClipboard = document.title + '\n' + window.location.toString();
3
4
const temporaryElement = document.createElement('textarea');
5
temporaryElement.value = dataToClipboard;
6
document.body.appendChild(temporaryElement);
7
temporaryElement.select();
8
document.execCommand('Copy');
9
document.body.removeChild(temporaryElement);
10
};
11
12
chrome.commands.onCommand.addListener(async (command) => {
13
const [tab] = await chrome.tabs.query({
14
active: true,
15
currentWindow: true,
16
});
17
18
if (command === 'copy') {
19
chrome.scripting.executeScript({
20
target: { tabId: tab.id },
21
function: copyTitleAndURL,
22
});
23
}
24
});