Call to getBackgroundPage from popup's callback function leads to infinite popup page loading

Issue #12239519 • Assigned to Akshay P.

Details

Author
Ilia L.
Created
Jun 5, 2017
Privacy
This issue is public.
Found in
  • Microsoft Edge
Found in build #
38.14393
Reports
Reported by 1 person

Sign in to watch or report this issue.

Steps to reproduce

While making an existing chrome extension port to Edge I found a strange behavior:
Popup code calls getBackgroundPage and then call a function from background with a callback. Popup page goes to infinite load, If getBackgroundPage will be called from this callback code one more time.
Some code to repro:

manifest.json

{
    "name": "TEST",
    "short_name": "TEST",
    "author": "TEST",
    "version": "0.1",
    "manifest_version": 2,
    "background": {
        "scripts": [
            "background.js"
        ],
        "persistent": true
    },
    "permissions": [
        "unlimitedStorage",
        "contextMenus",
        "cookies",
        "tabs",
        "notifications",
        "http://*/*",
        "https://*/*",
        "activeTab",
        "<all_urls>"
    ],
    "browser_action": {
        "default_title": "RUN TEST",
        "default_popup": "popup.html"
    }
}

popup.html

<!DOCTYPE HTML>
<html>
<head>
    <script type="text/javascript" src="popup.js"></script>
</head>
<body style="min-width: 300px; min-height: 415px;">
</body>
</html>

background.js

var message = "hello";
var message2 = "hello2";

function getBGMessage(callback) {
    callback(message);
}

function getBGMessage2() {
    return message2;
}

popup.js

window.addEventListener("load", OnLoad, false);

function getBG() {
    //for chrome test
    if(chrome.extension) {
        browser = chrome;
    }
    return browser.extension &amp;&amp; browser.extension.getBackgroundPage();
}

//Not working
function OnLoad() {
    console.log("POPUP: OnLoad");
    getBG().getBGMessage(function(message) {
        console.log(message);
        console.log(getBG().getBGMessage2());
    })
}

This issue is easy to workaround by making a local variable and using closure inside a callback, but this becomes a bit annoying if you have an existing and large codebase to port:

function OnLoad() {
    console.log("POPUP: OnLoad");
    var bg = getBG();
    bg.getBGMessage(function(message) {
        console.log(message);
        console.log(bg.getBGMessage2());
    });
}

Suddenly I’ve found that call from the setTimeout with any delay (even zero) will also fix the problem:

function OnLoad() {
    console.log("POPUP: OnLoad");
    var bg = getBG();
    bg.getBGMessage(function(message) {
        setTimeout(function() {
            console.log(message);
            console.log(getBG().getBGMessage2());
        }, 0)
    });
}

I’ve made an assumption that this is a scope problem and give a try to self exec function, but this didn’t do the trick:

//Not working
function OnLoad() {
    console.log("POPUP: OnLoad");
    var bg = getBG();
    bg.getBGMessage(function(message) {
        (function() {
            console.log(message);
            console.log(getBG().getBGMessage2());
        })();
    });
}

All mentioned examples are working fine in chrome.
Looks like a bug in extension platform.

Attachments

Comments and activity

  • I end up with global cached var for bg and this works fine, but anyway looks like a strange behavior.

  • Microsoft Edge Team

    Changed Assigned To to “James M.”

  • Hello,

    Thank you for providing this information about the issue. After thorough testing, we are unable to reproduce this problem in Edge with the information at hand. Specifically, the extension is not working as expected even in Chrome.

    To help us repro this problem, please update this case when you can attach the 4 files and provide us some repro steps to see what you are seeing.

    Best Wishes,
    The MS Edge Team

  • Please see attached archive with test, I also added images with results edge_1.png - I can only see one message in console instead of two, edge_2.png - after page reload by f5, nothing going on, i can only see spinner on the tab header, page can’t load. chrome.png - same code on chrome, console shows 2 messages.

  • Microsoft Edge Team

    Changed Assigned To to “Akshay P.”

You need to sign in to your Microsoft account to add a comment.

Sign in