简体   繁体   English

是否可以在 IFRAME 标签嵌入的页面中注入 JavaScript 代码?

[英]Is it possible to inject JavaScript code in a page embedded by IFRAME tag?

There is a certain page (say, named "TargetPage.html" in which I inject the Javascript code below by means of a Firefox extension:有一个特定页面(例如,名为“TargetPage.html”的页面,我通过 Firefox 扩展名在其中注入了下面的 Javascript 代码:

<!-- TargetPage.html -->
unsafeWindow.document.getElementById("target-element").click();

I want to embed this page into another page (say, named "AnotherPage.php") by using the HTML tag "iframe".我想通过使用 HTML 标签“iframe”将此页面嵌入到另一个页面(例如,名为“AnotherPage.php”)。 Like below:如下所示:

<!-- AnotherPage.php -->
<iframe src="TargetPage.html"></iframe>

Now that I embedded it via "iframe", there is no way to inject any JavaScript code with a browser extension.现在我通过“iframe”嵌入了它,没有办法通过浏览器扩展注入任何 JavaScript 代码。

Is there any way of running this code in the embedded page?有没有办法在嵌入页面中运行此代码?

Only if both pages are in the same domain.仅当两个页面在同一个域中时。

Otherwise is considered cross-domain scripts, and it's a big no-no, for browsers to allow it.否则被认为是跨域脚本,这是一个很大的禁忌,浏览器允许它。

However, there are ways to achieve such communication between domains.但是,有一些方法可以实现域之间的这种通信。

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

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