是否可以从inspectedWindow.eval内向消息工具面板发送消息? [英] Is it possible to message the dev-tools panel from within inspectedWindow.eval?

本文介绍了是否可以从inspectedWindow.eval内向消息工具面板发送消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读Chrome扩展消息传递文档,但似乎无法建立在检查窗口内进行通信。



例如,

  chrome.devtools.inspectedWindow.eval( 'function(){



/ *发送消息到开发工具面板* /
chrome.runtime.sendMessage({foo:foo为什么在chrome.devtools.inspectedWindow.eval中需要extensionId?这让我退后一步并提出问题。
'')

我试图利用背景和内容脚本来侦听这些消息,但似乎没有任何触发。我的实际实施是监听WebSocket流量,为了简洁起见,我将其留在了外面。我能够听取每个请求/响应,但是我似乎无法在inspectedWindow.eval中建立通信。最后,我的目标是简单地与我的开发工具面板沟通,以便更新用户界面。



更新



我发现了一个有趣的回购,面临类似的问题。他们似乎没有找到有效的解决方案。难道这种消息传递是不允许的设计?

https://github.com/thomasboyt/injectedWindow.eval-communication-sadness

解决方案

您说过您考虑过使用内容脚本。



在这种情况下,您可以引发自定义DOM事件,内容脚本将能够处理它。

  / /内容脚本
window.addEventListener(RebroadcastExtensionMessage,function(evt){
chrome.runtime.sendMessage(evt.detail);
},false);

//评估代码
var message = {/ * whatever * /};
var event = new CustomEvent(RebroadcastExtensionMessage,{detail:message});
window.dispatchEvent(event);

当然,缺点是页面可以监听这些事件,如果它选择为以及欺骗他们。如果这是一个严重的问题,您可以将随机数包含在事件名称和来自开发工具的消息中。但是,再次,一个真正的恶意页面可以覆盖 CustomEvent ..从某种意义上说,这不是一个可以解决的问题,因为任何你 inspectedWindow.eval()与页面完全共享上下文(和API访问)。


I have been reading over the chrome extension message passing documentation, but I cannot seem to establish communication from within the inspected window.

For example,

chrome.devtools.inspectedWindow.eval('function() {  
      .
      . 
      .
     /* send message to dev-tools panel */ 
    chrome.runtime.sendMessage({foo:"foo"});<-- Uncaught Error: Invalid arguments to connect. Why is the extensionId required within chrome.devtools.inspectedWindow.eval? This made me step back and ask the question.
}')

I have tried leveraging both background and content scripts to listen for these messages, but nothing seems to trigger. My actual implementation is listening for WebSocket traffic, I left that out for brevity. I am able to listen to each request/response, however I cannot seem to establish communication within inspectedWindow.eval. In the end, my goal is to simply communicate with my dev-tools panel so I can update the UI.

Update

I found an interesting repo from someone that faced a similar issue. They however did not seem to find a valid solution. Could it be that this sort of messaging is not allowed by design?

https://github.com/thomasboyt/injectedWindow.eval-communication-sadness

解决方案

You said you considered using a content script.

In that case, you can raise a custom DOM event, and the content script will be able to process it.

// Content script
window.addEventListener("RebroadcastExtensionMessage", function(evt) {
  chrome.runtime.sendMessage(evt.detail);
}, false);

// Eval'd code
var message = {/* whatever */};
var event = new CustomEvent("RebroadcastExtensionMessage", {detail: message});
window.dispatchEvent(event);

The downside, of course, is that the page can listen in on those events if it so chooses as well as spoof them. If that's a serious concern, you can include nonces into event names and messages originating from dev tools. But then again, a really hostile page can override CustomEvent.. In a way, this is not a solvable problem, since anything you inspectedWindow.eval() fully shares context (and API access) with the page.

这篇关于是否可以从inspectedWindow.eval内向消息工具面板发送消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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