Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract DOI from the current web page URL #1799

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions DOI.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"inRepository": true,
"translatorType": 4,
"browserSupport": "gcsv",
"lastUpdated": "2016-11-05 10:57:01"
"lastUpdated": "2018-12-14 20:57:01"
}

// The variables items and selectArray will be filled during the first
Expand All @@ -20,7 +20,18 @@ var selectArray = {};


// builds a list of DOIs
function getDOIs(doc) {
function getDOIs(doc, url) {
var dois = [], m, DOI;

// Extract DOIs from the current URL
var re = /10.[0-9]{4,}?\/[^\s&?#,]*[^\s&?#\/.,]/g;
while (m = re.exec(url)) {
DOI = decodeURIComponent(m[0]);
if (!dois.includes(DOI)) {
dois.push(DOI);
}
}

// TODO Detect DOIs more correctly.
// The actual rules for DOIs are very lax-- but we're more strict.
// Specifically, we should allow space characters, and all Unicode
Expand All @@ -36,10 +47,7 @@ function getDOIs(doc) {
const DOIre = /\b10\.[0-9]{4,}\/[^\s&"']*[^\s&"'.,]/g;
const DOIXPath = "//text()[contains(., '10.')]\
[not(parent::script or parent::style)]";

var dois = [];

var node, m, DOI;
var node;
var results = doc.evaluate(DOIXPath, doc, null, XPathResult.ANY_TYPE, null);
while (node = results.iterateNext()) {
//Z.debug(node.nodeValue)
Expand Down Expand Up @@ -69,7 +77,7 @@ function detectWeb(doc, url) {
const blacklistRe = /^https?:\/\/[^/]*(?:google\.com|sciencedirect\.com\/science\/advertisement\/)/i;

if (!blacklistRe.test(url)) {
var DOIs = getDOIs(doc);
var DOIs = getDOIs(doc, url);
if (DOIs.length) {
return "multiple";
}
Expand Down Expand Up @@ -151,7 +159,7 @@ function retrieveDOIs(dois, doc, providers) {
}

function doWeb(doc, url) {
var dois = getDOIs(doc);
var dois = getDOIs(doc, url);
Z.debug(dois);
var providers = [
{
Expand Down Expand Up @@ -191,6 +199,11 @@ var testCases = [
"type": "web",
"url": "https://en.wikipedia.org/wiki/Template_talk:Doi",
"items": "multiple"
},
{
"type": "web",
"url": "https://zotero.org/?d=10.7208/chicago/9780226924632.001.0001",
"items": "multiple"
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

]
/** END TEST CASES **/