Answer the question
In order to leave comments, you need to log in
Why isn't the browser action fired in a Firefox extension?
On a well-known site on traffic rules, there is an achievement "pass the test in 30 seconds." The achievement is clearly inhuman, and they invite you to write a cheat, so I started small: how to make this thing work. (Previous achievement, test in 1 minute, passed by myself and honestly).
Source.
{
"manifest_version": 2,
"name": "Green Way Cheat",
"version": "1.0",
"description": "A simple addon for cheating in green-way.com.ua",
"icons": {
"48": "icons/green-way-48.png"
},
"applications": {
"gecko": {
"id": "[email protected]",
"strict_min_version": "45.0"
}
},
"permissions": [ "activeTab", "tabs" ],
"browser_action": {
"default_icon": "icons/green-way-48.png",
"default_title": "Green Way Cheat"
},
"content_scripts": [
{
"matches": [ "https://green-way.com.ua/*" ],
"js": [ "green-way.js" ]
}
]
}
// Выполняется
document.body.style.border = "5px solid yellow";
// Что-то глючит
browser.browserAction.onClicked.addListener(() => {
alert("Cheat!");
document.body.style.border = "5px solid red";
});
// Не выполняется
document.body.style.border = "5px solid blue";
Answer the question
In order to leave comments, you need to log in
Again, you have to answer yourself. Understood, although he suffered for a long time.
There are two scripts in the browser extension: background and content. The background one can work with the browser and its windows, the content one can work with the page. The only exchange between them is messages.
Since there is quite a lot of AJAX on this particular site, it is difficult for a JS beginner to get by with a single content script, so let the background script from the browser panel initiate the scam, and the content script interfere with the page. It is done like this.
{
"manifest_version": 2,
"name": "Green Way Cheat",
"version": "1.0",
"description": "A simple addon for cheating in green-way.com.ua",
"icons": {
"48": "icons/green-way-48.png"
},
"applications": {
"gecko": {
"id": "[email protected]",
"strict_min_version": "45.0"
}
},
"permissions": [ "activeTab", "tabs" ],
"browser_action": {
"default_icon": "icons/green-way-48.png",
"default_title": "Green Way Cheat"
},
"content_scripts": [
{
"matches": [ "https://green-way.com.ua/*" ],
"js": [ "content.js" ]
}
],
"background": {
"scripts": [ "bg.js" ]
}
}
function getActiveTab()
{
return browser.tabs.query({active: true, currentWindow: true});
}
function cheatMain()
{
getActiveTab().then((tabs) => {
browser.tabs.sendMessage(tabs[0].id, { command: "cheat" } );
});
}
browser.browserAction.onClicked.addListener(cheatMain);
function cheatHere()
{
alert("Cheat!");
}
browser.runtime.onMessage.addListener(cheatHere);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question