简体   繁体   English

您如何通过Google Chrome扩展程序访问网页的事件监听器

[英]How can you gain access to a web page's event listeners from a Google Chrome extension

I'm writing a Chrome extension to list out the source code for each event handler attached to an <a/> tag when you roll over it. 我正在编写一个Chrome扩展程序,以列出悬停在<a/>标记上的每个事件处理程序的源代码。

Currently, I've printed out the href attribute - $(this).attr("href") - and this works fine. 目前,我已经打印出href属性- $(this).attr("href") -并可以正常工作。 The source code is on github at http://github.com/grantyb/Google-Chrome-Link-URL-Extension . 源代码位于github上, 网址http://github.com/grantyb/Google-Chrome-Link-URL-Extension

When I extend it to access event handlers, $(this).data("events") returns null. 当我将其扩展为访问事件处理程序时,$(this).data(“ events”)返回null。 I'm certain that there is a click() handler for my <a/> tag, because when I output $("a").data("events") inside my web page, it correctly lists the handler. 我确定<a/>标签有一个click()处理函数,因为当我在网页内输出$("a").data("events") ,它会正确列出该处理函数。

It seems that jQuery's data() method is checking data that is sandboxed, so I can't access it from inside my Extension. 看来jQuery的data()方法正在检查已沙盒化的数据,所以我无法从扩展程序内部访问它。 That makes sense, since I guess it's stored within the jQuery global variable, and that's clearly a different variable from the jQuery global that lives inside my Extension. 这是有道理的,因为我想这是存储在内部jQuery全局变量,这是明显地从不同的变量jQuery全局,我的分机内居住。

Is there another way to access the list of event handlers for an object in the DOM. 还有另一种方法来访问DOM中对象的事件处理程序列表。 Are event handlers even stored in the DOM? 事件处理程序是否甚至存储在DOM中?

Solved with a terrific hack! 解决了一个了不起的黑客!

Check out the code on GitHub for the details: http://github.com/grantyb/Google-Chrome-Link-URL-Extension . 在GitHub上查看代码以获取详细信息: http : //github.com/grantyb/Google-Chrome-Link-URL-Extension

The Content Script uses jQuery to append a <script> tag directly into the host page ( insert maniacal laugh here ). 内容脚本使用jQuery将<script>标记直接添加到宿主页面中( 在此处插入疯狂的笑声 )。 The "parasite" script has complete visibility over the host page, including DOM and javascript sandbox, since it is now a part of the page. 由于“寄生虫”脚本现在已成为该页面的一部分,因此它对整个宿主页面具有完全可见性,包括DOM和javascript沙箱。 It fetches the data I need, encodes it into a text format and writes it into an attribute of an element that I prepared earlier. 它获取所需的数据,将其编码为文本格式,然后将其写入我之前准备的元素的属性中。

Then, I sit back and wait (setTimeout(...,10)) for the script to do its business. 然后,我坐下来等待(setTimeout(...,10))脚本完成其业务。 When I get called back, I clean up after myself and grab the text-encoded data straight out of the DOM element. 当我被打回电话时,我会自己清理并从DOM元素中直接获取文本编码的数据。

Is there an easier way? 有更容易的方法吗? This feels naughty. 这感觉很调皮。

Your entire extension is sandboxed. 您的整个扩展程序都已沙箱化。 You can access the DOM from your extension, but not the javascript. 您可以从扩展名访问DOM,但不能访问javascript。

The only way to interact is by modifying the DOM in such a way that your javascript loads inside the webpage. 进行交互的唯一方法是修改DOM,以使您的JavaScript可以在网页内加载。 But you'll still be sandboxed so you can't communicate with your extension via javascript directly. 但是您仍然会处于沙箱状态,因此无法直接通过javascript与扩展程序进行通信。

As WoLpH said, your extension is sandboxed and can't access nor modify any webpage DOM. 正如WoLpH所说,您的扩展程序是沙盒化的,无法访问或修改任何网页DOM。

Still, you can achieve your goal "gracefully" by using a content script and message passing : 不过,您仍然可以使用内容脚本消息传递来“优雅地”实现目标:

  • A content script for accessing/modifying the webpages DOM 用于访问/修改网页DOM的内容脚本
  • Message passing for communication between your extension and the webpages 在您的扩展程序和网页之间进行通信时传递消息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM