iframe 获取值无法将console.log的对象转换为 string

本文探讨了在JavaScript中如何处理同域与跨域的IFrame内容访问问题,并尝试通过各种方法来读取IFrame中的HTML内容。
<iframe src="" name="bb" id="iframe1" ></iframe>
    <div onclick="test()">tesxr</div>
    <div onclick="bobo()">bbb</div>
<script type="text/javascript">
 window.open('http://wenku.baidu.com/view/30f1188fd15abe23482f4dcc.html','bb');
 //将http://wenku.baidu.com的内容放入iframe中,为跨域
   window.open('./base.html','bb');
  //把同域中的内容放入iframe中,为同域
        function test(){
         console.log(window.document.getElementsByTagName('iframe')[0].contentWindow.document);
    //可获取同域中的对象,但是不能获取跨域的console.log(window.document.getElementsByTagName('iframe')[0].contentWindow.document);
            var html=window.document.getElementsByTagName('iframe')[0];
            console.log(html);
            //通过console.log可以打印出跨域iframe中的内容,为一个object  但是无法将其转换为字符串!!!!?是个问题~`
            htmlS=html.toString;
            htmlT=JSON.stringify(html);
            htmlW=html.toString()

            console.log(JSON.stringify(html))
            var htmlStr=JSON.stringify(html);
            alert(html.toString())
            var substr = htmlStr.match(/body(\S*)body/);
            alert(substr)
            console.log(window.document.getElementsByTagName('iframe')[0]);

    }
    function bobo(){
        alert(htmlS);
        alert(htmlT);
        alert(htmlW)
    }
</script>
import { makeObservable, observable, action, runInAction } from "mobx"; import { getMandatoryRemindApi } from "Renderer/api/modules/forceRemind"; import { mainWindow } from "Renderer/main/stores"; import { ETokenBus, EI_Exchange_Company, IMEventBus, IMEventBusTag_JumpToIM, IMEventBusTag_JumpToCalendar, Global_Layout_Home_Init } from "Renderer/ct/EventEmitter"; interface remindList { fid: string; emp_no: string; business_resource: string; business_code: string; url: string; } const { IMEventBus_on, IMEventBus_removeListener } = IMEventBus(); export default class ForceRemindModalStore { mandatoryRemindList: remindList[] = []; currentIndex: number = 0; modal: any = null; constructor() { makeObservable(this, { mandatoryRemindList: observable, currentIndex: observable, modal: observable, init: action, getRemindListAction: action, destroyAction: action, startAfter: action, showNextModal: action, initMessageListener: action, }); } init() { this.initMessageListener(); console.log("=========init"); this.startAfter(); } destroyAction = () => { runInAction(() => { this.mandatoryRemindList = []; console.log("2222destroyAction"); }); //off mainWindow().removeLifeFrequencyEvent(this.lifeStart); }; startAfter () { console.log("注册Global_Layout_Home_Init监听器"); IMEventBus_on(Global_Layout_Home_Init, this.homeInitFinish); mainWindow().addLifeFrequencyEvent(this.lifeStart); } homeInitFinish = () => { console.log("111111111"); this.getRemindListAction(); }; lifeStart = () => { console.log("33333lifeStart"); this.getRemindListAction(); }; async getRemindListAction() { // if (this.mandatoryRemindList.length) return; let res = await getMandatoryRemindApi(); if (res.isSuccess) { let list = res.data.list; runInAction(() => { this.mandatoryRemindList = list; if (this.mandatoryRemindList.length > 0) { this.showNextModal(); } }); } else { runInAction(() => { this.mandatoryRemindList = []; }); } } showNextModal() { if (this.currentIndex >= this.mandatoryRemindList.length) return; const url = this.mandatoryRemindList[this.currentIndex].url; this.modal = document.createElement("div"); this.modal.style.position = "fixed"; this.modal.style.top = "0"; this.modal.style.left = "0"; this.modal.style.width = "100%"; this.modal.style.height = "100%"; this.modal.style.backgroundColor = "rgba(0,0,0,0.5)"; this.modal.style.zIndex = "1000"; this.modal.style.display = "flex"; this.modal.style.justifyContent = "center"; this.modal.style.alignItems = "center"; const iframe = document.createElement("iframe"); iframe.src = url; iframe.style.width = "80%"; iframe.style.height = "80%"; iframe.style.border = "none"; this.modal.appendChild(iframe); document.body.appendChild(this.modal); } closeCurrentModal() { if (this.modal) { document.body.removeChild(this.modal); this.modal = null; this.currentIndex++; this.showNextModal(); } } initMessageListener() { console.log("监听器已注册") window.addEventListener("message", (event) => { console.log('收到消息原始数据:', event); if (event.data == "cornex-im-closeForceRemindModal") { console.log('消息验证通过'); this.closeCurrentModal(); } }); } } 外部浏览器postMessage 发送的消息,store 能监听的到,去关闭弹窗
07-10
import { makeObservable, observable, action, runInAction } from "mobx"; import { getMandatoryRemindApi } from "Renderer/api/modules/forceRemind"; import { mainWindow } from "Renderer/main/stores"; import { ETokenBus, EI_Exchange_Company, IMEventBus, IMEventBusTag_JumpToIM, IMEventBusTag_JumpToCalendar, Global_Layout_Home_Init } from "Renderer/ct/EventEmitter"; interface remindList { fid: string; emp_no: string; business_resource: string; business_code: string; url: string; } const { IMEventBus_on, IMEventBus_removeListener } = IMEventBus(); export default class ForceRemindModalStore { mandatoryRemindList: remindList[] = []; currentIndex: number = 0; modal: any = null; constructor() { makeObservable(this, { mandatoryRemindList: observable, currentIndex: observable, modal: observable, init: action, getRemindListAction: action, destroyAction: action, startAfter: action, showNextModal: action, initMessageListener: action, }); } init() { this.initMessageListener(); console.log("=========init"); this.startAfter(); } destroyAction = () => { runInAction(() => { this.mandatoryRemindList = []; console.log("2222destroyAction"); }); //off mainWindow().removeLifeFrequencyEvent(this.lifeStart); }; startAfter () { console.log("注册Global_Layout_Home_Init监听器"); IMEventBus_on(Global_Layout_Home_Init, this.homeInitFinish); mainWindow().addLifeFrequencyEvent(this.lifeStart); } homeInitFinish = () => { console.log("111111111"); this.getRemindListAction(); }; lifeStart = () => { console.log("33333lifeStart"); this.getRemindListAction(); }; async getRemindListAction() { // if (this.mandatoryRemindList.length) return; let res = await getMandatoryRemindApi(); if (res.isSuccess) { let list = res.data.list; runInAction(() => { this.mandatoryRemindList = list; if (this.mandatoryRemindList.length > 0) { // this.showNextModal(); } }); } else { runInAction(() => { this.mandatoryRemindList = []; }); } } showNextModal() { if (this.currentIndex >= this.mandatoryRemindList.length) return; const url = this.mandatoryRemindList[this.currentIndex].url; this.modal = document.createElement("div"); this.modal.style.position = "fixed"; this.modal.style.top = "0"; this.modal.style.left = "0"; this.modal.style.width = "100%"; this.modal.style.height = "100%"; this.modal.style.backgroundColor = "rgba(0,0,0,0.5)"; this.modal.style.zIndex = "1000"; this.modal.style.display = "flex"; this.modal.style.justifyContent = "center"; this.modal.style.alignItems = "center"; const iframe = document.createElement("iframe"); iframe.src = url; iframe.style.width = "80%"; iframe.style.height = "80%"; iframe.style.border = "none"; this.modal.appendChild(iframe); document.body.appendChild(this.modal); } closeCurrentModal() { if (this.modal) { document.body.removeChild(this.modal); this.modal = null; this.currentIndex++; this.showNextModal(); } } initMessageListener() { console.log("监听器已注册") window.addEventListener("message", (event) => { console.log('收到消息原始数据:', event); if (event.data == "cornex-im-closeForceRemindModal") { console.log('消息验证通过'); this.closeCurrentModal(); } }); } } 将showNextModal方法里 面封成一个tsx页面组件,接受两个参数打开或关闭,另一个url,用于渲染iframe ,closeCurrentModal 直接调用关闭,此组件还要监听外部浏览postMessage传过来的数据用于关闭iframe .
最新发布
07-10
const largeObjectArray = this.createLargeObjectArray(); const newWatch = (callback) =>{ // table 打印时间 const tablePrintTime = this.calculateTime(() => { console.table(largeObjectArray); }); // 普通输出时间 const printLogTime = this.calculateTime(() => { console.log(largeObjectArray); }); // 有效时间检测(排除0) if (tablePrintTime > 0 && printLogTime > 0) { // 动态阈判断(当前对比) if (tablePrintTime > printLogTime * 10) { console.log('时间对比新', tablePrintTime, printLogTime); callback(true); return; } } callback(false); } DisDev({ interval: 300, // 延长检测间隔至300ms,降低误报率 disableIframeParents: false, // 关闭iframe检测 ondevtoolopen(type, next) { if ( document.body.innerHTML.indexOf('检测到非法调试,即将关闭此页面') < 0 ) { newWatch((isDetected) => { console.log('是什么', isDetected) if (isDetected) { document.body.innerHTML = '检测到非法调试,即将关闭此页面!!'; alert('检测到非法调试,即将关闭此页面!!'); window.location.href = 'about:blank'; if (window.history.replaceState) { window.history.replaceState( null, '', window.location.pathname + window.location.search, ); } // next(); } }); } }, }); // 添加监听器 addListener(function(isOpen) { if (isOpen) { if ( document.body.innerHTML.indexOf('检测到非法调试,即将关闭此页面') < 0 ) { newWatch((isDetected) => { console.log('是什么', isDetected) if (isDetected) { document.body.innerHTML = '检测到非法调试,即将关闭此页面!'; alert('检测到非法调试,即将关闭此页面!'); window.location.href = 'about:blank'; if (window.history.replaceState) { window.history.replaceState( null, '', window.location.pathname + window.location.search, ); } } }); } } }); // 开始检测 launch(); // 禁用开发者工具 createLargeObject() { if (!this._largeObject) { this._largeObject = Array.from({length: 500}, (_, i) => ({ key: i, value: `${i}` }) ); } return this._largeObject; } createLargeObjectArray() { return Array(50).fill(this.createLargeObject()); } calculateTime(func) { const start = performance.now(); func(); return performance.now() - start; }将以上代码兼容ie浏览器,并将优化后的代码完整展示
03-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值