M
M
Mercury132018-06-23 13:05:55
JavaScript
Mercury13, 2018-06-23 13:05:55

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.js
{

  "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" ]
    }
  ]

}
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";

The button doesn't work, anything after addListener doesn't work either. Nothing is clear from the examples. No permissions?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mercury13, 2018-06-23
@Mercury13

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.json
{

  "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" ]
  }

}
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);
content.js
function cheatHere()
{
  alert("Cheat!");
}

browser.runtime.onMessage.addListener(cheatHere);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question