以下为本人残余save相关代码
function save(){
const ahref=decodeURIComponent(href),fileName=`${prompt('文件名:',(title0?title0:host))}&t=${uw.myms2bjs(Date.now())}`;
if(fileName){//生成文件名,优先使用标题,若没有则使用主机名,并添加时间戳和文件扩展名
uw.dqa("z,script").removeEach();
removeRelativePaths();//移除绝对
body.removeAttribute("contentEditable");//千古神坑:必须关闭编辑状态!!!
antiCORS().then(()=>{
body.myappend('a',title0||ahref,'href',ahref,'style','z-index:0;position:fixed;bottom:0;left:0;opacity:0.7;width:100%');
body.myafter('a','','href','data:text/htm;charset=utf-8,\ufeff'+encodeURIComponent(html.innerHTML.replace(/>\s+</g,'><').replace(/[\r\n]\s*|^\s+|\s+$/g, '')),'download',`${fileName}.htm`,'style','visibilibility:hidden').click();//下载
});
body.myappend('a',title0||ahref,'href',ahref,'style','z-index:0;position:fixed;bottom:0;left:0;opacity:0.7;width:100%');
body.myafter('a','','href','data:text/htm;charset=utf-8,\ufeff'+encodeURIComponent(html.innerHTML.replace(/>\s+</g,'><').replace(/[\r\n]\s*|^\s+|\s+$/g, '')),'download',`${fileName}(less).htm`,'style','visibilibility:hidden').click();//下载
}
}
function removeRelativePaths(){//除了移除改变以下的相对路径为绝对路径外,还要替换含锚的本站点的绝对路径>相对路径;不含css内的路径!
['href','src','url'].forEach(prop=>{
uw.dqa(`[${prop}]`).forEach(ele=>{//选择所有包含指定属性的元素
let eprop=ele[prop]||ele.getAttribute(prop);
if(eprop==='#')return;//若属性值为 #,则跳过该元素
ele[prop]=eprop;//得到完整路径
});
});
//css相对转绝对;css中类似url("../images/ecp_header_unit_login.png?v=20241016;vpn_img");的属性都把../替换为实际网址
}
function removePrefixAttr(domArr,prefix){//移除所有元素的所有on开头属性(DOM0事件):removePrefixAttr(document.querySelectorAll('*'),'on')
domArr.forEach(e=>{
if(e.attributes.length>0){
const attrs=Array.from(e.attributes);//Array.from将e.attributes类数组对象转换为真正数组,避免在遍历时因为移除属性导致索引变化而出现问题
attrs.forEach(attr=>{
const n=attr.name;
if(n.startsWith(prefix))e.removeAttribute(n);
});
}
});
}
async function downloadResource(url){
try{
const response=await fetch(url);
if(!response.ok)throw new Error(`下载资源失败: ${url}`);
return await response.text();//成功
}catch(err){
uw.ce('下载资源时出错:',err);
return null;
}
}
async function antiCORS(){//换链接为文本,规避同源策略
const links=doc.qsa('link[rel="stylesheet"]');
for(const link of links){//处理 link 标签
const css=await downloadResource(link.href);
if(css){
link.mybefore('style',css);
link.parentNode.removeChild(link);
}
}
// const imgs=doc.qsa('img[src]')
// for(const img of imgs){//处理 img 标签
// try{
// const response=await fetch(img.src),blob=response.blob(),reader=new FileReader();
// reader.onloadend=()=>{img.src=reader.result};//img转base64
// reader.readAsDataURL(blob);
// }catch(err){
// ce('图片转base64出错:',err);
// }
// }
// const scripts=doc.qsa('script[src]');
// for(const script of scripts){//处理 script 标签
// const scriptContent=await downloadResource(script.src);
// if(scriptContent){
// script.mybefore('script',scriptContent);
// script.parentNode.removeChild(script);
// }
// }
}
浙公网安备 33010602011771号