Hi all, I am new on Add-ons and have what it is probably a silly question, however, I haven’t been able to load any content_script. I have tried to include the script in two ways, via the “manifest.json” with the “content_scripts” properties and using the “browser.tabs.executeScript()” method from the script.js file under the browserAction folder.
the current structure, manifest and script from where is injected of my project are as follows:
browserAction
|__index.html
|__script.js
|__style.css
background.js
content_script.js
manifest.json
manifest.json
{
"manifest_version": 2,
"name": "Highlight",
"description": "Highlight text",
"version": "0.0.1",
"icons": {
"64": "icons/icon.png"
},
"permissions": [
"activeTab",
"contextMenus"
],
"content_scripts": [
{
"matches": ["*://*.mozilla.org/*"],
"js": ["content_script.js,"],
"all_frames": true
}
],
"background": {
"scripts": [
"background.js"
]
},
"browser_action": {
"default_icon": {
"64": "icons/icon.png"
},
"default_popup": "browserAction/index.html",
"default_title": "Highlight"
},
"page_action": {
"default_icon": {
"64": "icons/icon.png"
},
"default_popup": "pageAction/index.html",
"default_title": "Highlight"
},
"options_ui": {
"page": "options/index.html"
}
}
script.js
function start() {
function selectText() {
let selectedText = '';
if (window.getSelection) {
selectedText = window.getSelection();
} else if (document.getSelection) {
selectedText = document.getSelection();
} else if (document.selection) {
selectedText = document.selection.createRange().text
}
console.log(selectedText.toString());
};
function onError(error) {
console.log(error);
}
browser.tabs.executeScript({file: "../content_script/index.js"})
.then(selectText)
.catch(onError);
};
start();