网上搜索了一圈,都是用 ActiveXObject("WScript.Shell"); 只能是IE使用,
老款的IE本能就可以右键创建桌面快捷方式,我要的是可以Chrome和其他浏览器能用.
之前做了个下载网页的js书签脚本改了一下.
代码如下,最好添加为书签工具使用
原理:
url桌面快捷方式就是一个 .url 后缀的文件而已
[{000214A0-0000-0000-C000-000000000046}]
Prop3=19,11
[InternetShortcut]
IDList=
URL=https://www.baidu.com/
IconFile=https://www.baidu.com/favicon.ico
IconIndex=1
那么用js创建这个文件下载就OK了
缺点:打开时系统有安全提示,下载也有安全提示
下载文件的js代码是以前参考的别人的博客,具体可以百度出来,在此致谢
我主要是二次发挥,做成了生成 url 桌面快捷方式
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>js创建 网页-桌面快捷方式</title>
<style>
#downdiv{
position:fixed;
top:30px;
right:30px;
z-index:9999999;
}
#downdiv input{
display: block;
width: 100%;
}
#down_btn {
font-size:50px;
color:lawngreen;
}
</style>
</head>
<body>
js创建 网页-桌面快捷方式
<script type="text/javascript">
// javascript:
(function() {
var content=`[{000214A0-0000-0000-C000-000000000046}] \n
Prop3=19,11 \n
[InternetShortcut] \n
IDList= \n
URL=${location.href} \n
IconFile=${location.protocol}//${location.hostname}/favicon.ico \n
IconIndex=1 \n
`;
var downdiv = document.createElement("div");
downdiv.id="downdiv";
downdiv.innerHTML=`
<style>
#downdiv{
position:fixed;
top:30px;
right:30px;
z-index:9999999;
}
#downdiv input{
display: block;
width: 100%;
}
#down_btn {
font-size:50px;
color:lawngreen;
}
</style>
名称:<input type="text" id="sitetitle" value="__${document.title}__url" />
网址:<input type="text" id="siteurl" value="${location.href}" />
`;
var down_btn = document.createElement("button");
down_btn.innerText = "下载 URL桌面快捷方式";
down_btn.title = "注意查看,下载文件";
down_btn.id = "down_btn";
downdiv.appendChild(down_btn);
document.body.appendChild(downdiv);
var eledowndiv = document.querySelector("#downdiv");
var eleButton = document.querySelector("#down_btn");
var funDownload = function(content, filename) {
var eleLink = document.createElement("a");
eleLink.download = filename;
eleLink.style.display = "none";
var blob = new Blob([content]);
eleLink.href = URL.createObjectURL(blob);
document.body.appendChild(eleLink);
eleLink.click();
document.body.removeChild(eleLink)
};
if ("download" in document.createElement("a")) {
eleButton.addEventListener("click", function() {
var sitename =document.querySelector('#sitetitle').value||document.title;
document.body.removeChild(eledowndiv);
funDownload(content, sitename + ".url")
})
} else {
eleButton.onclick = function() {
alert("浏览器不支持")
}
}
})();
</script>
<script type="text/javascript-------------书签工具 添加书签,地址栏 copy 如下代码 --------------">
javascript:(function(){var content=`[{000214A0-0000-0000-C000-000000000046}]\nProp3=19,11\n[InternetShortcut]\nIDList=\nURL=${location.href}\nIconFile=${location.protocol}//${location.hostname}/favicon.ico\nIconIndex=1\n`;var downdiv=document.createElement("div");downdiv.id="downdiv";downdiv.innerHTML=`<style>#downdiv{position:fixed;top:30px;right:30px;z-index:9999999}#downdiv input{display:block;width:100%}#down_btn{font-size:50px;color:lawngreen}</style>名称:<input type="text"id="sitetitle"value="__${document.title}__url"/>网址:<input type="text"id="siteurl"value="${location.href}"/>`;var down_btn=document.createElement("button");down_btn.innerText="下载 URL桌面快捷方式";down_btn.title="注意查看,下载文件";down_btn.id="down_btn";downdiv.appendChild(down_btn);document.body.appendChild(downdiv);var eledowndiv=document.querySelector("#downdiv");var eleButton=document.querySelector("#down_btn");var funDownload=function(content,filename){var eleLink=document.createElement("a");eleLink.download=filename;eleLink.style.display="none";var blob=new Blob([content]);eleLink.href=URL.createObjectURL(blob);document.body.appendChild(eleLink);eleLink.click();document.body.removeChild(eleLink)};if("download"in document.createElement("a")){eleButton.addEventListener("click",function(){var sitename=document.querySelector('#sitetitle').value||document.title;document.body.removeChild(eledowndiv);funDownload(content,sitename+".url")})}else{eleButton.onclick=function(){alert("浏览器不支持")}}})();
</script>
</body>
</html>