我正在尝试将数据发送到sidebar。我看过以下文档,但无法弄清楚如何在打开数据条后正确地将数据发送到数据条。

  • Communicating with sidebar scripts
  • Communicating with other scripts
  • Using ports

  • 我当前的方法是保存边栏附加到全局变量中时获得的worker对象,但是我敢肯定有更好的方法。展示我当前方法的最小示例:
    var BARWORKER = undefined;
    require("sdk/ui/sidebar").Sidebar({
        id: "mybar",
        url: "./sidebar.html",
        onReady: function (worker) {
            BARWORKER = worker;
        }
    });
    
    // lots of code
    
    if (BARWORKER) {
        BARWORKER.port.emit("message", payload);
    }
    
    // lots of code
    

    最佳答案

    要将数据发送到已打开的侧边栏,您还可以使用可见性API。

    https://developer.mozilla.org/en-US/docs/DOM/Using_the_Page_Visibility_API

    function onVisibilityChange() {
      if(!document.hidden){}
    }
    
    document.addEventListener("visibilitychange", onVisibilityChange);
    

    关于javascript - 如何正确地将数据发送到打开的Firefox侧边栏?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29424375/

    10-11 12:34
    查看更多