组件ID可以在Chrome控制台通过$find找到,但不能在外部注入代码中找到

Component ID can be found by $find in Chrome console but not in external injection code

本文关键字:外部 但不能 注入 代码 找到 ID Chrome 控制台 组件 find      更新时间:2023-09-26

它有一个网页使用 microsoftajax 来创建确认对话框,ComponentID可以通过$find(或Sys.Application.findComponent)在Chrome控制台和Sys.Application.getComponents中找到ID组件列表,但是当我将代码注入Chrome扩展的内容脚本时,结果总是返回'NULL'除了DOM操作。我只是想更改组件的属性以实现自动确认,而无需手动单击,但这似乎很难做到。这是我的代码:

 $(document).ready(function(){  
    Sys.Application.getComponents() //return 'NULL'
    Sys.Application.findComponent('sth')._ConfirmOnFormSubmit=true  //return 'Cannot set property '_ConfirmOnFormSubmit' of null '
    })
    function pageLoad(sender,args) {
//Sys.Application.initialize()
console.log($find('sth'))  //return 'NULL'
function confirm() {return true} //seems to approach the fact working well that bypassed the dialog in chrome console  but still not working in Content Script
}

为什么Content和Console的结果不同?我该怎么做才对呢?

如果有人能给我一个解决办法,我会非常感激。

问好。

您看到这种行为的原因是内容脚本存在于一个孤立的上下文中。他们不能访问页面的JavaScript和它的状态。

要进行测试,您可以打开控制台并在顶部将上下文从<top frame>切换到扩展的上下文。您将看到不同的控制台结果。

要解决这个问题,你需要注入另一个时间,这一次从内容脚本到页面的上下文中;然后,您可以使用自定义事件或window.postMessage()在两者之间通信数据。

这是一个典型的问题