Greasy Fork

ScienceDirect下载

避免跳转在线pdf,可直接下载ScienceDirect文献到本地

目前为 2022-10-10 提交的版本。查看 最新版本

// ==UserScript==
// @name                ScienceDirect Download
// @name:zh-CN          ScienceDirect下载
// @namespace      tampermonkey.com
// @version        2.0
// @license MIT
// @description         Avoid jumping to online pdf, and directly download ScienceDirect literature to local
// @description:zh-CN   避免跳转在线pdf,可直接下载ScienceDirect文献到本地
// @match        *://www.sciencedirect.com/*
// @match        *://pdf.sciencedirectassets.com/*
// @grant        none
// @run-at document-start
// ==/UserScript==

/**
 * 获取 blob
 * @param  {String} url 目标文件地址
 * @return {cb} 
 */
function getBlob(url, cb) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);
    xhr.responseType = 'blob';
    xhr.onload = function () {
        if (xhr.status === 200) {
            cb(xhr.response);
        }
    };
    xhr.send();
}

/**
 * 保存
 * @param  {Blob} blob     
 * @param  {String} filename 想要保存的文件名称
 */
function saveAs(blob, filename) {
    if (window.navigator.msSaveOrOpenBlob) {
        navigator.msSaveBlob(blob, filename);
    } else {
        var link = document.createElement('a');
        var body = document.querySelector('body');
        link.href = window.URL.createObjectURL(blob);
        link.download = filename;
        // fix Firefox
        link.style.display = 'none';
        body.appendChild(link);
        link.click();
        body.removeChild(link);
        window.URL.revokeObjectURL(link.href);
    };
}

/**
 * 下载
 * @param  {String} url 目标文件地址
 * @param  {String} filename 想要保存的文件名称
 */
function download(url, filename) {
    getBlob(url, function (blob) {
        saveAs(blob, filename);
    });
};
(function () {
    'use strict';
    var domain = document.domain;
    if (domain == 'pdf.sciencedirectassets.com') {
        var url = document.URL + '&download=true';
        console.log(url);
        var title = document.URL.split("/")[5].split("-")[2];
        // var html_url = "https://www.sciencedirect.com/science/article/pii/" + document.URL.split("/")[5].split("-")[2]
        var ret = prompt('请输入文件名,点击确认下载', title);
        if (ret !== null && ret != '') {
            var filename = ret + '.pdf';
            download(url, filename);
        };
    };
    if (domain == 'www.sciencedirect.com') {
        document.addEventListener("DOMContentLoaded", DOM_ContentReady);
        function DOM_ContentReady() {
            // get rawlink
            var head = document.head;
            // creat newlink
            var linkid = head.getElementsByTagName('meta')[0].content;
            if (linkid) {
                var new_url = "https://www.sciencedirect.com/science/article/pii/" + linkid + "/pdfft?isDTMRedir=true";
                let Container = document.createElement('div');
                Container.id = "sp-ac-container";
                Container.style.position = "fixed";
                Container.style.left = "300px";
                Container.style.top = "28px";
                Container.style['z-index'] = "999999";
                Container.innerHTML = `<button title="Tips:Copy the title of the article before downloading" class="button1" id="download" onclick="window.location.href='${new_url}'")>download</button>
                <style>
                .button1 {
                -webkit-transition-duration: 0.4s;
                transition-duration: 0.4s;
                padding: 2px 16px;
                text-align: center;
                background-color: green;
                color: white;
                border: 1px solid #4CAF50;
                border-radius:5px;
                }
                .button1:hover {
                background-color: #4CAF50;
                color: red;
                }
                </style>`;
                document.body.appendChild(Container);
                //绑定按键点击功能
                //     Container.onclick = function () {
                //         // alert("你好");
                //         downPlan(url);
                //         return;
                //     };
                //     console.log(url);
            }
        }
    };
})()