简体   繁体   English

如何将iframe面板注入Google Chrome扩展程序并在iframe中添加click事件?

[英]how to inject an iframe panel to a Google Chrome extension and add click event inside the iframe?

I did an extension that inject iframe to the current webpage, using Content-scripts: 我做了一个扩展,使用Content-scripts将iframe注入当前网页:

$('body').append('<iframe src="' + chrome.extension.getURL('panel.html') + '" frameborder="0" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; z-index: 2147483647; position: fixed; width: 100%; left: 0px; bottom: 0px; height: 100px; display: block; visibility: visible; outline-style: none; outline-width: 0px; outline-color: rgb(0, 0, 0); " id="GE_Panel"></iframe>');

I can view the iframe in the webpage, but I don't know how to add click event for an element, that inside the iframe... 我可以在网页中查看iframe,但是我不知道如何为iframe中的元素添加点击事件。

You have to wait until the iframe has loaded and then access the elements inside of it: 您必须等到iframe加载完毕,然后再访问其中的元素:

Eg 例如

$('<iframe ... />').load(function() {
    var doc = $(this).contents();
    doc.find('#yourLink').click(...);
}).appendTo('body');

Reference: .contents 参考: .contents

Not sure how Chrome handles this for extensions, but it could be that you will have a same-origin policy problem. 不确定Chrome如何处理扩展程序,但可能是您有一个同源政策问题。

如果您可以控制panel.html,只需将处理click事件的js放在panel.html中。

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

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