I am trying to get selected text and open this word in dictionary web site. I am aiming this by selecting the word and when I right click, it'll be open as new tab. But I am missing something.
Here is my background.js
const BASE_URL = "http://lugatim.com/s/";
chrome.runtime.onMessage.addListener((data) => {
if (data.type === "notification") {
notify(data.message);
}
});
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: "word",
title: "Kelimeyi ara: %s",
contexts: ["selection"],
onClicked: (info, tab) => {
console.log(info);
chrome.tabs.create({
url: BASE_URL + encodeURIComponent(info.selectionText),
});
},
});
});
chrome.contextMenus.onClicked.addListener((info, tab) => {
if ("word" === info.menuItemId) {
notify(info.selectionText);
openSite(info.selectionText);
}
});
const notify = (message) => {
return chrome.notifications.create("", {
type: "basic",
title: "Notify!",
message: message || "Notify!",
iconUrl: "./assets/icons/128.png",
});
};
const openSite = (word) => {
return chrome.tabs.create({ url: BASE_URL + word });
};
And here is my manifest.json
{
"manifest_version": 3,
"name": "TEST",
"description": "TEST",
"version": "1.0.0",
"icons": { "128": "lugatim_128.png" },
"action": {
"default_icon": "lugatim_19.png",
"default_popup": "popup.html"
},
"content_security_policy": {
"extension_pages": "script-src 'self'; object-src 'self'"
},
"background": {
"service_worker": "background.js"
},
"permissions": [
"tabs",
"activeTab",
"contextMenus",
"storage",
"activeTab",
"notifications"
]
}
By this way, I could have selected the word and show it in the context menu
But how can I go to the website I want?