How do I access the popup page DOM from bg page in Chrome extension?

前端 未结 3 2110
鱼传尺愫
鱼传尺愫 2021-02-08 14:14

In Google Chrome\'s extension developer section, it says

The HTML pages inside an extension have complete access to each other\'s DOMs, and they can invoke f

3条回答
  •  说谎
    说谎 (楼主)
    2021-02-08 14:54

    I understand why you want to do this as I have run into the problem myself.

    The easiest thing I could think of was using Google's method of a callback - the sendRequest and onRequest methods work as well, but I find them to be clunky and less straightforward.

    Popup.js

    chrome.extension.getBackgroundPage().doMethod(function(params)
    {
        // Work with modified params
        // Use local variables
    });
    

    Background.html

    function doMethod(callback)
    {
        if(callback)
        {
            // Create/modify params if needed
            var params;
    
            // Invoke the callback
            callback(params);
        }
    }
    

提交回复
热议问题