Greasy Fork

Greasy Fork is available in English.

文档在线预览与下载更名

2021/3/29 上午12:53:16

当前为 2022-04-12 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        文档在线预览与下载更名
// @namespace   Violentmonkey Scripts
// @match       *://*/*
// @grant       none
// @version     1.1
// @author      -
// @description 2021/3/29 上午12:53:16
// @license MIT
// ==/UserScript==

/**
 * @file
 * Checks for document links in web pages and inserts
 * an icon beside the links to enable opening with
 * online services like Google Docs viewer.
 *
 * @author Deekshith Allamaneni
 * @copyright 2015 Docs Online Viewer
 */


(function(){
var docLinks = document.links;
const supportedFileExtList = ["pdf","doc","docx","xls","xlsx","ppt","pps","pptx","xps","odt","odp","rtf","ods","wpd"];
var doCheck = true;
const dov_host_exclude =/(docs\.google\.com|sourceforge\.net|adf\.ly|mediafire\.com|springerlink\.com|ziddu\.com|ieee\.org|issuu\.com|asaha\.com|office\.live\.com)$/
// Include paths to exclude showing icon
const dov_href_exclude = /(https:\/\/github.com\/.*\/.*\/blob\/.*|file:\/\/\/.*)/ 
const dovIconImgPath = "https://dov.parishod.com/assets/images/beside-link-icon.svg";


var DocLink = function (docLink) {
    this._docLink = docLink;
};
DocLink.prototype = {
    get hasSupportedExtension () {
        return supportedFileExtList.some( thisFileType => {
            var url = this._docLink.pathname.toLowerCase();
            if (url.endsWith('.' + thisFileType))
                return true;
        });
    },
    get isSupported () {
        return (!((this._docLink.host).match(dov_host_exclude)) 
            && !((this._docLink.href).match(dov_href_exclude)) 
            && this.hasSupportedExtension
            && this._docLink.textContent.trim().length > 0); // Issue #6: No blank innerText
    },
    get isProcessed () {
        return this._docLink.docView;
    },
    get iconLink () { 
        var viewLink = document.createElement('a');
        //这个预览地址偶尔会好用viewLink.href = `http://view.xdocin.com/xdoc?_xdoc=${encodeURIComponent(this._docLink.href)}`;
       //谷歌预览是备用的 viewLink.href = `https://docs.google.com/viewer?url=${encodeURIComponent(this._docLink.href)}&embedded=true`;
      //再加一个备用的:url后面的部分需要base64编码:https://file.keking.cn/onlinePreview?url=aHR0cDovL3d3dzEudXN0bC5lZHUuY24vancvZG9jcy8yMDE5MDcyNzE0MTk1NDc5NDI1OC5kb2M=
       viewLink.href = `https://view.officeapps.live.com/op/view.aspx?src=${encodeURIComponent(this._docLink.href)}`;
      /*
            Parameter description:
                embedded= <true>: to open google docs in embedded mode
                dov=1: If opened by Docs Online Viewer. Set by this script.
        */
        //viewLink.docView=true; -> This line is removed in this version but still doubt if it can really be removed.
        viewLink.title=`View this in Office`;
        var ico = document.createElement("img");
        ico.src =  "https://raw.githubusercontent.com/adeekshith/Docs-Online-Viewer/165389071f4c792241c4d7d4079d103369e413f7/src/images/beside-link-icon.svg";
        // Adjusts the margin of the icon to the given number of pixels (3 to 5px is advisable)
        ico.style.marginLeft = "3px";
        ico.style.width = "16px";
        ico.style.height = "16px";
        viewLink.appendChild(ico);
        // Disabled opening link in new tab by default.
        viewLink.setAttribute("target", "_blank");
        return viewLink;
    },
  get iconLink2 () { 
        var viewLink = document.createElement('a');
       viewLink.href = `http://view.xdocin.com/xdoc?_xdoc=${encodeURIComponent(this._docLink.href)}`;
    //上面这个是xdoc文档预览的地址
        viewLink.title=`View this in Xdoc`;
        var ico = document.createElement("img");
        ico.src =  "https://raw.githubusercontent.com/adeekshith/Docs-Online-Viewer/165389071f4c792241c4d7d4079d103369e413f7/src/images/beside-link-icon.svg";
        ico.style.marginLeft = "3px";
        ico.style.width = "16px";
        ico.style.height = "16px";
        viewLink.appendChild(ico);
        viewLink.setAttribute("target", "_blank");
        return viewLink;
    },
    get fileExtension () {
        var fUrl = this._docLink.pathname;
        //fUrl=fUrl.toUpperCase();
        // Returns file extension. Returns "" if no valid extension
        // Ref: http://stackoverflow.com/a/1203361/3439460
        return fUrl.substr((~-fUrl.lastIndexOf(".") >>> 0) + 2);
    },
    get queryStripped() {
        // remove any ?query in the URL     
        return `${this._docLink.origin}${this._docLink.pathname}`;
    }

};


function checkLinks()
{
    for (var i = 0; i < docLinks.length; ++i) 
    {
        var thisDocLink = new DocLink(docLinks[i]);
        if ( thisDocLink.isSupported && !thisDocLink.isProcessed) 
        {
          //console.log("find a link to change!");
          //docLinks[i].download=docLinks[i].text+docLinks[i].href.substring(docLinks[i].href.lastIndexOf("."));
          docLinks[i].download=docLinks[i].text+"."+thisDocLink.fileExtension;
          console.log("修改下载文件名为:"+docLinks[i].download);
          //上两行是我加的,用于把下载文件名改为链接文字加扩展名,因为原先的下载文件名总是全数字
          if ( thisDocLink.fileExtension !== "pdf") {
            docLinks[i].parentNode.insertBefore(thisDocLink.iconLink2 , docLinks[i].nextSibling);
          docLinks[i].parentNode.insertBefore(thisDocLink.iconLink , docLinks[i].nextSibling); 
          // Append the icon beside the link,pdf除外
          }
        }
    // The link which is checked is flagged so that it is not repeatedly checked again.
    docLinks[i].docView=true;
   }
}

// Execute these functions
// to append icon beside document links and
// add listener for new nodes
checkLinks();

    // create an observer instance
    var observer = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            if (doCheck)
            {
                doCheck = false;
                setTimeout(function(){checkLinks();doCheck = true;}, 1000);
            }
        });
    });

    // pass in the target node, as well as the observer options
    observer.observe(document.body, {
        attributes: true,
        childList: true,
        characterData: true,
        subtree:true
    });
})();