web 程序生成桌面快捷方式(window)

2,064 阅读1分钟

生成桌面快捷方式

在我们日常开发桌面网页程序时,经常会遇到产品说,能不能在把网站生成一个快捷方式的图标,后续方便打开。

得嘞,既然要满足需求,那咱就研究研究呗。

PHP 生成桌面快捷方式:
<?php
$shortcut = "[InternetShortcut]
URL=https://juejin.cn/
IDList=
IconIndex=43
IconFile=C:\\Windows\system32\SHELL32.dll
HotKey=1626
[{000214A0-0000-0000-C000-000000000046}]
Prop3=19,2
";
Header("Content-type: application/octet-stream");
header("Content-Disposition: p_w_upload; filename=掘金.url");
echo $shortcut;
?>

javascript 生成桌面快捷方式:

原理基本跟PHP生成方式一样,下载.url文件

function urlDownload(content, fileName){
   const eleLink = document.createElement("a");
   eleLink.download = fileName;
   eleLink.style.display = "none";
   const blob = new Blob([content]);
   eleLink.href = URL.createObjectURL(blob);
   document.body.appendChild(eleLink);
   eleLink.click();
   document.body.removeChild(eleLink);
}

cosnt {protocol, href, hostname} = location;
cosnt content = `[{000214A0-0000-0000-C000-000000000046}]
    Prop3=19,11
    [InternetShortcut]
    IDList=
    URL=${href}
    IconFile=${protocol}//${hostname}/favicon.ico
    IconIndex=1`;

urlDownload(content, '掘金.url');

到此,我们就可以在window下生成桌面快捷方式了。