簡體   English   中英

使用 javascript 檢查 iframe 中是否存在特定文本

[英]Check if specific text exists in iframe using javascript

我得到了這個 iframe:

<iframe id="frami" src="http://192.168.178.22/file.html"></iframe>

其中包含文本:

var m1_enable="1"; var m1_x="0"; var m1_y="570"; var m1_w="1920"; var m1_h="510"; var m1_sensitivity="50"; var m1_threshold="0";

如何檢查 iframe 中是否存在m1_enable="1"

我現在試過了:

iframe_html = document.getElementById('frami').contentWindow.document.body.innerHTML;
if(iframe_html.includes("var")){
    alert();
}
else{
}

如果您的 iframe 在同一個域上,則它是 document.getElementById('frami').contentWindow['m1_enable'] (如果這不是 null,則與 ==1 的 1 進行比較)。 否則,請查看 window.postMessage

https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage

iframe tag has a property called contentDocument that returns the document object generated by an iframe and another called contentWindow that returns the window object generated by an iframe . 您可以通過以下方式檢查“m1_enable="1"

var x = document.getElementById("frami");
  var y = (x.contentWindow || x.contentDocument);
  if (y.document) y = y.document;
  if (y.body.textContent.indexOf('m1_enable="1"')>-1){
    alert('found the phrase'
  } 

您可以從 iframe 獲得 HTML:

iframe_html = document.getElementById('iframe').contentWindow.document.body.innerHTML

並檢查它是否包含m1_enable="1"

iframe_html.includes("m1_enable='1'")

這僅在幀源位於同一域時才有效。 如果它來自不同的域,跨站點腳本 (XSS) 保護將啟動。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM