从内容脚本触发点击事件 - Chrome扩展 [英] Triggering a click event from content script - chrome extension

查看:167
本文介绍了从内容脚本触发点击事件 - Chrome扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Chrome扩展程序的内容脚本中,我点击某些网站上的某些链接/按钮。为此,我在内容脚本中使用以下代码(我在内容脚本中嵌入了jQuery):

  $(css_selector) .trigger(click)

这适用于大多数网站。



然而,在delta.com,match.com和paypal.com等特定网站上,这种触发元素点击的方式无效。在delta.com上,当我尝试在内容脚本中触发时,我收到以下异常:

 错误:尝试在不存在的情况下引用节点。 
错误:NotFoundError:DOM异常8

奇怪的是,如果我打开javascript consoleon delta .com,包括一个jQuery并尝试相同的点击触发代码片段,它的工作原理。



在match.com和paypal.com上,触发根本无法在内容中工作脚本并没有错误。我甚至不能像我在delta.com上所做的那样通过JavaScript控制台触发点击事件。

如果我手动使用鼠标点击,所有这三个网站上的一切正常。因此,我也试图模拟使用mousedown(),mouseup(),但这也不起作用。



这似乎是问题,因为来自这些网站的JavaScripts劫持并忽略事件。我试图从这些网站阅读代码,看看发生了什么,但代码太多了。



有没有人知道这里发生了什么,以及如何解决它?

解决方案

由于浏览器扩展沙箱和基本的jQuery功能,您不能触发非$ j $ click事件,触发或点击



您可以调用原始DOM元素点击方法,就像用鼠标点击元素一样。只需使用 [0] 即可访问DOM元素:

  $( css_selector)[0]。点击(); 

尽管您很少需要,但您可以使用<$ $中的相同代码触发所有匹配的按钮C $ C>每个。作为 中,每个都是DOM元素,所以非常简单:

 $(css_selector).each(function(){
this.click();
});


In my chrome extension's content script, I click on certain links/buttons on webpages from certain sites. To do that, I use following code in the content script (I embed jQuery in the content script):

$(css_selector).trigger("click")

This works on most sites.

However, on certain sites like delta.com, match.com, and paypal.com, this way of triggering a click on elements does not work. On delta.com, I get following exception thrown when I attempt triggering in the content script:

Error: An attempt was made to reference a Node in a context where it does not exist.
Error: NotFoundError: DOM Exception 8

Strange thing is, if I open javascript consoleon delta.com, include a jQuery and attempt the same click triggering code snippet, it works.

On match.com and paypal.com, triggering simply does not work in the content script and there is no error. I cannot even trigger "click" event through javascript console the way I did on delta.com.

If I manually use mouse click, everything works fine on all the three sites. Hence I also tried to simulate that using mousedown(), mouseup(), but that did not work either.

This seems to be issue because javascripts from those sites are hijacking and ignoring events. I tried to read code from these sites to see what is happening but there was simply too much code.

Does anyone have any idea about what is happening here and how to fix it?

解决方案

Due to browser extension sand-boxing, and basic jQuery functionality, you cannot trigger a non-jQuery click event with trigger or click.

You can however call the raw DOM element click method, which will act exactly as if the element was clicked with the mouse. Just use [0] to access the DOM element:

$(css_selector)[0].click();

Although you would seldom need to, you can trigger all matching buttons using the same code in an each. As the this in an each is the DOM element it is quite simple:

$(css_selector).each(function(){
    this.click();
});

这篇关于从内容脚本触发点击事件 - Chrome扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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