Neymar_code 2017-10-21 06:23 采纳率: 0%
浏览 759

解释一段JavaScript代码

window.problemText = decodeURIComponent('%3Cscript%3E') + '\n' + window.problemText + '\n' + decodeURIComponent('%3C%2Fscript%3E');
var tab = document.getElementById('tab');
var textarea = document.getElementById('textarea');
var problem = document.getElementById('problem');
var output = document.getElementById('output');
var iframe = document.getElementById('iframe');
var script = '%3Cscript%3E' + encodeURIComponent('window.onerror=function(a){parent.postMessage({error:a.toString()},"*")};window.console=window.console||{};window.console.log=function(a){parent.postMessage({console:a},"*")};window.alert=function(a){if(a===1)parent.postMessage({success:1},"*");else if(a==1)parent.postMessage({warning:"You should alert NUMBER 1."},"*");else{parent.postMessage({warning:"You need to alert 1."},"*")}};window.onmessage=function(a){try{check(a.data)}catch(e){parent.postMessage({error:e.toString().split("\n")[0]},"*")}}') + '%3C%2Fscript%3E';
function localCheck() {
problem.innerText = window.problemText;
iframe.src = 'data:text/html,' + encodeURIComponent(window.problemText.replace(/\n\s*/g, '')) + script;
iframe.onload = function () {
this.contentWindow.postMessage(textarea.value, '*');
};
}
localCheck();
window.onmessage = function (e) {
var d = e.data;
console.log(d);
if (d.success !== undefined) {
tab.className = 'rs-tab rs-tab-success';
tab.innerText = 'Local check passed, running server check...';
// server check
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
tab.innerText = 'Server response: \'' + xhr.responseText + '\'.';
}
}
};
xhr.open('POST', '/check', true);
xhr.send(JSON.stringify({
id: location.pathname.match(/^\/(\d+)$/)[1],
ans: textarea.value,
}));
} else if (d.warning !== undefined) {
tab.className = 'rs-tab rs-tab-warning';
tab.innerText = d.warning;
} else if (d.error !== undefined) {
tab.className = 'rs-tab rs-tab-danger';
tab.innerText = d.error;
output.innerText = '';
} else if (d.console !== undefined) {
tab.className = 'rs-tab rs-tab-warning';
tab.innerText = 'Not the answer yet...';
output.innerText = d.console;
}
};

  • 写回答

1条回答 默认 最新

  • 问问那只猫 2017-10-21 07:27
    关注

    感觉是用tab切换来加载不同的网页。网页加载用的是iframe

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘