我正在尝试创建一个小书签,它将允许教师突出显示网页上的文本并将该选择下载为纯文本(.txt)文件。我已设置a demo page以显示我正在工作以及卡住的位置。从动态创建的窗口下载文本文件
在演示页面上,我可以突出显示文本并使用“下载选择”按钮直接下载。但是,如果您错误地突出显示,则必须返回并重新执行。 “制作页面”按钮可抓取突出显示的文本,并创建一个包含文本的弹出框以进行打样。
的script.js
function makePage(text) {
var text = "";
if(typeof window.getSelection != "undefined") {
text = window.getSelection().toString();
var newPage = window.open("", null, "height=200,width=300,status=yes,menubar=no");
newPage.document.write("<body><a id='download' download='text.txt'></a><p id='copy'>" + text + "</p><button id='download' onclick='getPlainText()'>Download</button><scipt type='text/javascript' src='script.js' defer></script></body></html>");
console.log(text)
}
}
function getPlainText(copy) {
var text = "";
if(typeof window.getSelection != "undefined") {
text = window.getElementById('copy').innerContent;
var download = document.getElementById('download');
download.href = 'data:text/plain;charset:utf-8,' + encodeURIComponent(text);
var event = new MouseEvent('click');
download.dispatchEvent(event);
}
return text;
}
当我点击弹出的下载按钮,我得到的控制台类型错误:
Uncaught TypeError: window.getElementById is not a function
我搬到脚本该文件,但无论它在哪里,错误返回,我不明白为什么。任何想法如何让这个工作?