时间:2023-09-01 03:48:01 | 来源:网站运营
时间:2023-09-01 03:48:01 来源:网站运营
转载:如何在浏览器中编辑网页:转载:function to_file_path_name(filename) { filename = filename.trim(); // filename = filename.replace(" ", "_"); filename = filename.replaceAll(" ", "_"); // # https://www.cnblogs.com/jjliu/p/11514226.html filename = filename.replaceAll(":", ""); return filename;}//我同学dearning,从网上搜来的函数String.prototype.replaceAll = function (s1, s2) { return this.replace(new RegExp(s1, "gm"), s2);};function downloadTxt(filename, text) { var pom = document.createElement('a'); pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text)); pom.setAttribute('download', filename); if (document.createEvent) { var event = document.createEvent('MouseEvents'); event.initEvent('click', true, true); pom.dispatchEvent(event); } else { pom.click(); }}function stripScripts(s) { var div = document.createElement('div'); div.innerHTML = s.outerHTML; // div.innerHTML = s; // div.firstChild=s; // console.log(div); var scripts = div.getElementsByTagName('script'); console.log(scripts.length); var i = scripts.length; while (i--) { scripts[i].parentNode.removeChild(scripts[i]); } // return div.innerHTML; return div; // http://codingdict.com/questions/10189}function getTitle() { let title=null; let titleClassMaybe = ["problemName_3A5bH", "mb-2 card-header","Card-title"]; let titleIdMaybe = [ "pageTitleText"]; let titleDom; for (let i = 0; i < titleClassMaybe.length; i++) { titleDom = document.getElementsByClassName(titleClassMaybe[i])[0]; if (titleDom) { title = titleDom.textContent; break; } } for (let i = 0; i < titleIdMaybe.length; i++) { titleDom = document.getElementById(titleIdMaybe[i]); if (titleDom) { title = titleDom.textContent; break; } } if(title==null){ title="没有标题"; } title = to_file_path_name(title); return title;}function downloadPage() { // https://blog.csdn.net/weixin_42649856/article/details/104325029 // console.log(document.); // console.log(document.getElementsByTagName('html')[0].outerHTML); // downloadTxt("pta.html",document.getElementsByTagName('html')[0].outerHTML); let htmlDom = document.getElementsByTagName("html")[0]; // console.log(htmlDom); // try{ // title = document.getElementById("pageTitleText").textContent; // title = title.trim(); // }catch (e) { // title = htmlDom.getElementsByClassName("mb-2 card-header")[0].textContent; // } let title = getTitle(); // title = to_file_path_name(title); // htmlDom = removeAllScript(htmlDom); let htmlTxt = stripScripts(htmlDom).outerHTML; // console.log(htmlDom); // console.log(htmlDom.outerHTML); // 获得文本的html // let body = htmlDom.outerHTML; // let body = document.getElementsByTagName('html')[0].outerHTML; // console.log(body); // let htmlTxt = removeDontWant(htmlDom.outerHTML); // let htmlTxt = removeDontWant(htmlDom); // console.log(htmlDom.outerHTML); // console.log(htmlDom); downloadTxt(title+".html", htmlTxt);}downloadPage();
关键词:编辑,浏览,转载