RGdoi

Convert DOI links in Researchgate to hyperlinks

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        RGdoi
// @namespace   https://github.com/ashwinvis/greasyfork
// @description Convert DOI links in Researchgate to hyperlinks
// @author      ashwinvis
// @license     GPL-3.0+; http://www.gnu.org/licenses/gpl-3.0.txt
// @include     http://www.researchgate.net/publication/*
// @include     https://www.researchgate.net/publication/*
// @version     0.0.5
// @supportURL  https://github.com/ashwinvis/greasyfork
// @grant       GM.xmlHttpRequest
// ==/UserScript==

var matchRegexpGlobal = /DOI.*/mg;
var extractRegexp = /(?:DOI:\s)([\w\.\/\-])*/g;
var extractRegexpAfterLogin = /<li(.*?)>(.*?)<\/li>/;

function extractDoiAndHyperlink(text)
{
    var root = "https://doi.org/";

    // var text = window.document.body.textContent;
    while((match = matchRegexpGlobal.exec(text)) != null) {
        var doi = extractRegexp.exec(match[0]);
        var doiLogin = extractRegexpAfterLogin.exec(match[0]);
        if (doi != null) {
            console.log("DOI detected: " + doi[0]);
            var doi_link = doi[0].replace(/DOI\:\s/i, "");
            replaceElement(doi_link);
        }
        else if (doiLogin != null) {
            console.log("DOI detected: " + doiLogin[2]);
            replaceElementAfterLogin(doiLogin[2]);
        }
        else {
            console.log("DOI not detected by extract regex!")
        }
    }
    console.log("End of RGdoi.js")
}


function replaceElement(doi) {
    var divs = document.querySelectorAll(".publication-meta-secondary");
    var element = divs[0];
    console.log("Wrapping hyperlink on " + element.innerHTML)
    var replace = '<a href="https://doi.org/' + doi +'">' + element.innerHTML +  '</a>';
    element.innerHTML = replace;
}


function replaceElementAfterLogin(doi) {
    var lis = document.querySelectorAll(".nova-e-list__item");
    var element = lis[3];
    console.log("Wrapping hyperlink on " + element.innerHTML)
    var replace = '<a href="https://doi.org/' + doi +'">' + element.innerHTML +  '</a>';
    element.innerHTML = replace;
}


// Old method: which gets blocked 
// extractDoiAndHyperlink(window.top)

GM.xmlHttpRequest({
  method: "GET",
  url: location.href,
  headers: {
    "Content-Type": "application/x-www-form-urlencoded"
  },
  onload: function(response) {
    extractDoiAndHyperlink(response.responseText);
  }
});