You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
NewEduCoderBuild/js/pdf/index.html

156 lines
4.0 KiB

<!DOCTYPE html>
<html>
<head>
<title>Edu PDF</title>
</head>
<body>
<div id="pdf-viewer" style="height: 100vh"></div>
<script>
window.focus();
const pattern = /(\w+)=([^\#&]*)/gi;
const parames = {};
location.href.replace(pattern, function (attr, key, value) {
parames[key] = decodeURI(value);
});
window.disabledEdit = parames.model === "view" ? true : false;
console.log("parames",parames,decodeURIComponent(parames.url || 'http://127.0.0.1:8080/pdf.pdf'))
function IsFull() {
var fullscreenElement =
window.top.document.fullscreenElement
|| window.top.document.mozFullscreenElement
|| window.top.document.webkitFullscreenElement;
var fullscreenEnabled =
document.fullscreenEnabled
|| document.mozFullscreenEnabled
|| document.webkitFullscreenEnabled;
console.log("fullscreenElement", fullscreenElement);
if(window.isFullScreen){
// 客户端
return window.isFullScreen;
}
if (fullscreenElement == null) {
return false;
} else {
return true;
}
}
window.addEventListener("message", (event) => {
if (event.data === "getData") {
const { data } = event.data
_x2x_.saveAsCopyAndGetBufferAndName(true)
}
}, false)
const path = window.parent.location.pathname
const reg = /^\/classrooms\/[^/]+\/exercise\/\d+\/users\/[^/]+$/
window.isExercise = reg.test(path);
window.onblur = function(){
if(reg.test(path)){
if (window.top.document.webkitExitFullscreen) {
window.top.document.webkitExitFullscreen()
} else if (document.exitFullscreen) {
window.top.document.exitFullscreen()
} else if (document.msExitFullscreen) {
window.top.document.msExitFullscreen()
} else if (document.mozCancelFullScreen) {
window.top.document.mozCancelFullScreen()
}
window.top.postMessage(
{
type: 'closePop',
time: Date.now()
},
'*'
);
}
}
try {
if(isExercise && IsFull()){
window.top.document.getElementById('closePop').hidden = true;
}
} catch (error) {
}
// 创建按钮
const btn = document.createElement('button');
btn.innerText = '回到考试';
// 按钮样式
Object.assign(btn.style, {
position: 'fixed',
right: '40px',
bottom: '40px',
padding: '10px 20px',
background: '#1677ff',
color: '#fff',
border: 'none',
borderRadius: '6px',
cursor: 'pointer',
fontSize: '14px',
zIndex: 9999
});
// 点击事件
btn.addEventListener('click', () => {
window.top.postMessage(
{
type: 'closePop',
time: Date.now()
},
'*'
);
});
// 挂载到页面
if(isExercise && IsFull())
document.body.appendChild(btn);
</script>
<script async type="module">
import EmbedPDF from 'https://www-cdn.educoder.net/js/pdf/js/embedpdf.js';
window.aaa = EmbedPDF.init({
type: 'container',
target: document.getElementById('pdf-viewer'),
src: decodeURIComponent((parames.url.startsWith("http") ? "" : location.origin)+parames.url ),
disableEditing: true, // 如果 EmbedPDF 支持此选项
disableAnnotation: true, // 禁用注释功能
disableTextLayer: false // 保留文本层以支持搜索,但不允许修改
});
window.downloadArrayBuffer = function (buffer, filename, mimeType = 'application/octet-stream') {
const blob = new Blob([buffer], { type: mimeType });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename || "edu.pdf";
a.style.display = 'none';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
</script>
</body>
</html>