Search code examples
google-chrome-extensionmicrosoft-edgebrowser-extension

Edge Extension does not respond to runtime.onMessage.addListener()


I have been trying to get my hands dirty with developing some extensions on the Edge platform. Going good so far, until I hit a blocker. I have a method in my background script which produces context menus, and when clicked, sends a message to the context script. Everything works fine except that the context script shows no signs of invoking the event listener.

Here is my code: manifest.json:

{
"name":"Edge",
"author":"Auth",
"version":"1.0",
"description":"none",
"permissions":["*://*/","tabs","bookmarks","unlimitedStorage","contextMenus"],
"browser_action":{
    "default_icon":{
        "20":"images/Edge.png",
        "40":"images/Edge40.png"
    },
    "default_title":"Edge"
},
"background":{
    "persistent":true,
    "scripts":["back.js"]

},
"content_scripts":[{
    "matches":["<all_urls>"],
    "js":["cs.js"],
    "run_at":"document_end"
}]

}

background script:

browser.contextMenus.create({
id: "Show Notes",
title: "Show Notes",
contexts: ['link']
});

browser.contextMenus.create({
    id:"Make Notes",
    title:"Make Notes",
    contexts:['all']
});


browser.contextMenus.onClicked.addListener(function(info, tab) {
    alert("s")
    alert(info.menuItemId)
    alert(info.linkUrl)
    browser.tabs.query({active: true}, function(tabs){
        browser.tabs.getCurrent(function(tab){
            browser.tabs.sendMessage(tab.id,{
                "id":info.menuItemId,
                "link":info.linkUrl
            })
        })
    }) 
    alert("sent")
});

Content script:

browser.runtime.onMessage.addListener(lis)

function lis(request, sender, sendResponse) {
    alert("msg")
    if(request.id=="Show Notes"){
        alert("received")
        note.style.display="block"
    }
    if(request.id=="Make Notes"){
        note.style.display="block"
        alert("received")
    }
    return true
}

All the alert messages in the background script work fine, but the content script just doesn't make even a single pop up.

I have read and tried to implement a lot of answers, not of them worked. What could be going wrong? Thanks!


Solution

  • As Edge doesn't support Promises, you would have to rely on callback mechanism. Please synchronize your tabs.query or tabs.getCurrent with tabs.sendMessage. For the given code you will be seeing an error in the background page's console stating "id as undefined". Synchronizing the above two methods would help fix this. Also the bookmark permission in the manifest.json is useless as Edge currently doesn't support bookmarks. (Just wanted you to know this incase you missed it).