1

I have a piece of code which works fine on chrome. I did the following step mentioned here. I'm able to load it properly, but the code doesn't run properly. In the background console only the url gets printed, no other consoles or alerts work. Please any help will be appreciated ! Please

window.onload = function() {
 console.log("blah1");
 var port = browser.runtime.connect({name: "knockknock"});
 port.postMessage({request: "sendData"});
 port.onMessage.addListener(function(msg) {
  console.log("blah2");
  if(msg != null){
   
  }
 });
}
var sendData = null;
var url = null;
browser.webRequest.onBeforeRequest.addListener(function(details) {
 if(details != null && sendData == null && details.url.includes("www")){
  console.log(details.url);
  sendData = details.url;
 }
},
{urls: ["<all_urls>"]},
["blocking", "requestBody"]
); 

browser.runtime.onConnect.addListener(function(port) {
 console.log("inside addListener")
 port.onMessage.addListener(function(msg) {
  if (msg.request == "sendData"){
   console.log("sendData : ", sendData);
    port.postMessage(sendData);
  }else if (msg.answer == null){
   sendData = null;
  }
 });
});

My manifest file

{
    "manifest_version": 2,
    "name": "test",
    "version": "1.0.0",
    "author": "medha",

    "icons": {
        "48": "esso.png"
    },

    "content_scripts": [
        {
            "matches": ["<all_urls>"],
            "js": ["content.js"]
        }
    ],
    "background": {
        "scripts": ["background.js"]
    },

    "permissions": [ "webRequest", "webRequestBlocking", "webNavigation", "tabs", "<all_urls>", "storage"],

    "-ms-preload": {
      "backgroundScript": "backgroundScriptsAPIBridge.js",
      "contentScript": "contentScriptsAPIBridge.js"
    }
}
Yu Zhou
  • 11,532
  • 1
  • 8
  • 22
Medha
  • 13
  • 2
  • Do you want to deploy the extension to Edge Legacy? Is there any error in console when you run the extension? I have no idea what are the codes in the snippet. Are they all belong to `background.js`? If I put the codes in the `background.js`, it will log `blah1` and url in console. Please specify where the codes belong to and the expected behavior and [steps to reproduce](https://stackoverflow.com/help/minimal-reproducible-example). – Yu Zhou May 08 '20 at 06:17
  • Try removing `window.onload`, it's not needed anyway in the background script. – wOxxOm May 08 '20 at 06:57
  • the one with windows.onload is my content.js file.....the one below that which has addListener is my background.js file...and Im using Microsoft Edge 44.18362.449.0 version @YuZhou – Medha May 14 '20 at 09:21

1 Answers1

0

I tested and found that if we remove window.onload then the extension will work well in Edge Legacy. It will print all the consoles.

You could set content.script run at document_idle. It's equal to window.onload and you do not need to listen for the window.onload event. For more detailed information, you could refer to this article.

My manifest.json is like below:

{
    "manifest_version": 2,
    "name": "test",
    "version": "1.0.0",
    "author": "medha",

    "icons": {
        "48": "esso.png"
    },

    "content_scripts": [
        {
            "matches": ["<all_urls>"],
            "run_at": "document_idle",
            "js": ["content.js"]
        }
    ],
    "background": {
        "scripts": ["background.js"],
        "persistent": true
    },

    "permissions": [ "webRequest", "webRequestBlocking", "webNavigation", "tabs", "<all_urls>", "storage"],

    "-ms-preload": {
      "backgroundScript": "backgroundScriptsAPIBridge.js",
      "contentScript": "contentScriptsAPIBridge.js"
    }
}
Yu Zhou
  • 11,532
  • 1
  • 8
  • 22