如何在浏览器标签之间传递一个值? [英] How to pass a value between browser tabs?

查看:224
本文介绍了如何在浏览器标签之间传递一个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从一个标签传递一个值到另一个。



我尝试了 sessionStorage 和剪贴板,但到目前为止还没有运气。这里是演示和代码:

https://builder.addons.mozilla.org/package/154290/latest/



它试图做的是获得然后将它传递给打开的标签:
$ b

  //创建一个新的上下文菜单项目。 
var menuItem = contextMenu.Item({
label:Check with Proofread Bot,
context:contextMenu.SelectionContext(),
contentScript:'self.on(click ),function(){'+
'var text = window.getSelection()。toString();'+
'sessionStorage.setItem(proofread_bot_chrome,text);'+
'self.postMessage(text);'+
'});',
onMessage:function(text){
clipboard.set(text);
// sessionStorage。 setItem(proofread_bot_chrome,text);
tabs.open({
url:http://proofreadbot.com,
onOpen:function onOpen(tab){
();
alert(selection.text);
sessionStorage.setItem(proofread_bot_chrome()); //做一些东西,比如听内容
//加载
alert(clipboard.get ,clipboard.get());
}
});
}
});


解决方案

sessionStorage 和 localStorage 总是绑定到一个域。内容脚本以网页的权限运行,这意味着他们访问该网页的会话存储 - 对于传递数据不是很有用。而他们根本就没有剪贴板的存取。



这意味着你无法绕过传统的信息传递。当你打开选项卡时,你应该附加一个内容脚本,等待它准备好(内容脚本可以发送一条消息来表明),并发送文本。就像这样:

onOpen:function(tab){
var worker = tab .attach(
contentScript:self.on('message',function(text){alert(text);});+
self.postMessage(null);,
onMessage:function(){
worker.postMessage(text);
}
});

供参考:使用 postMessage()


I am trying to pass a value from one tab to another.

I tried sessionStorage and clipboard, but no luck so far. Here is the demo, and the code as well:

https://builder.addons.mozilla.org/package/154290/latest/

What it tries to do is get the selected text, and pass it to the opened tab:

// Create a new context menu item.
var menuItem = contextMenu.Item({
    label: "Check with Proofread Bot",
    context: contextMenu.SelectionContext(),
    contentScript: 'self.on("click", function () {' +
                '  var text = window.getSelection().toString();' +
                '  sessionStorage.setItem("proofread_bot_chrome", text);' +
                '  self.postMessage(text);' +
                        '});',
    onMessage: function(text) {
        clipboard.set(text);
        //sessionStorage.setItem("proofread_bot_chrome", text);
        tabs.open({
          url: "http://proofreadbot.com",
          onOpen: function onOpen(tab) {
            // do stuff like listen for content
            // loading.
            alert(clipboard.get());
            alert(selection.text);
            sessionStorage.setItem("proofread_bot_chrome", clipboard.get());
          }
        });
    }
});

解决方案

sessionStorage and localStorage are always bound to a domain. Content scripts run with the privileges of the web page meaning that they access the session storage for that web page - not very useful to pass data around. And they simply don't have clipboard access.

This means that you can't get around "traditional" messaging. When you open the tab you should attach a content script, wait for it to be ready (the content script can send a message to indicate that) and send it the text. Something like this:

onOpen: function(tab) {
  var worker = tab.attach({
    contentScript: "self.on('message', function(text) {alert(text);});" +
                   "self.postMessage(null);",
    onMessage: function() {
      worker.postMessage(text);
    }
  });
}

For reference: Communicating using postMessage()

这篇关于如何在浏览器标签之间传递一个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆