How to realize the desktop shortcut of a website in the front end

Inscription: We often need to implement a desktop shortcut on the homepage of our website in our work, so how do we do it?

image display:

 Code:

        The first step: get the path and title name;

        

sName: document.title,
sUrl: window.location.href

        The second step: determine whether it is an IE browser;

    isIE() {
      if (!!window.ActiveXObject || "ActiveXObject" in window) {
        // this.$message({
        //     message: '是IE浏览器',
        //     type: 'warning'
        //   })
        return true;
      } else {
        // alert("不是IE浏览器");
        this.$message({
            message: '如需生成桌面快捷方式,请在IE浏览器中打开!',
            type: 'warning'
          })
        return false;
      }
    },

Note: Use ActivexObject to distinguish between IE browsers and non-IE browsers

           IE supports ActiveObject controls, but the chrome series does not

The third step: adjust the interface

toDesktop() {
      var result = this.isIE();
      if (!result) {
        //不是IE浏览器
        //获得按钮元素
        var toDesktopButtonNode = document.getElementById("toDesktopButton");
        //隐藏按钮
        // toDesktopButtonNode.style.display = "none";
        return false
      }
      let sUrl = this.sUrl
      let sName = this.sName
      console.log('result', sUrl, sName)
        try {
          var WshShell = new ActiveXObject("WScript.Shell");
          // CreateShortcut 方法创建 WshShortcut 对象并将其返回。如果快捷方式标题以 .url 结尾,就会创建 WshURLShortcut 对象。
          // SpecialFolders 使用 WshSpecialFolders 对象提供对 Windows shell 文件夹的访问,如桌面文件夹,开始菜单文件夹和个人文档文件夹。
          // 返回完整的Windows桌面文件夹路径,
          var oUrlLink = WshShell.CreateShortcut(WshShell
              .SpecialFolders("Desktop")
              + "\\" + sName + ".url");
          oUrlLink.TargetPath = sUrl;
          oUrlLink.Save();//保存一个快捷方式,该快捷方式指向 FullName 属性指定的位置。
          this.$notify({
            type: 'success',
            message: '成功创建桌面快捷方式!'
          })
        } catch (e) {
          this.$message({
              message: '当前IE安全级别不允许操作或您的浏览器不支持此功能!',
              type: 'warning'
            })
        }
    },

Remarks: The use of createShortcut() method is a key.

Just like it if you like it! ! !

Guess you like

Origin blog.csdn.net/2201_75705263/article/details/132016591