Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firefox WebExtension: Make XMLHttpRequest

I'm making a Firefox WebExtension add-on. Here's what should happen:

  1. User clicks browser icon on ANY page.
  2. JavaScript is executed, collecting information from the page.
  3. Information is sent to my server using XMLHttpRequest

This is what my Chrome extension does. However, I cannot get this to work with the Firefox add-on. The JavaScript is injected and executed because I do see the alert(), which I have put at the end of the script. However, no call is made to my server. The Firefox debugger shows no attempted network activity, nor does it show any error.

Manifest:

{
  "manifest_version": 2,
  "name": "my_name",
  "version": "1.0",
  "description": "My description",
  "icons": {
    "48": "icons/my_icon.png"
  },
  "permissions": [
    "activeTab"
  ],
  "browser_action": {
    "default_icon": "icons/some_icon.png",
    "default_title": "My Name"
  },
  "background": {
    "scripts": ["background.js"]
  }
}

background.js:

browser.browserAction.onClicked.addListener(function(tab) {
    browser.tabs.executeScript(null, {file:"content_script.js"}); 
});

content_script.js:

var xmlHttp=new XMLHttpRequest();
xmlHttp.open("POST", "https://www.my_site.org",true);
var formData = new FormData();  
formData.append("my_var", "my_var");
xmlHttp.send(formData); 
alert("I do get here!");
like image 512
user984003 Avatar asked Dec 07 '16 18:12

user984003


1 Answers

You need to add the URL to permissions in manifest.json

"permissions": [
    "activeTab",
    "*://developer.mozilla.org/*" <= URL
  ],
like image 64
Andy Avatar answered Oct 13 '22 18:10

Andy