-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
228c532
commit b24546c
Showing
10 changed files
with
88 additions
and
1,272 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,80 @@ | ||
// Old and less strict DOI regex. | ||
// const doiRegex = "10.\\d{4,9}/[-._;()/:a-z0-9A-Z]+"; | ||
const doiRegex = new RegExp( | ||
/\b(10[.][0-9]{4,}(?:[.][0-9]+)*\/(?:(?!["&\'<>])\S)+)\b/ | ||
); | ||
const doiRegex = /\b(10[.][0-9]{4,}(?:[.][0-9]+)*\/(?:(?!["&'<>])\S)+)\b/; | ||
const nexusUrl = "https://standard--template--construct-org.ipns.dweb.link/#/nexus_science/doi:"; | ||
const trueRed = "#BC243C"; | ||
|
||
function resetBadgeText() { | ||
browser.browserAction.setBadgeText({ text: "" }); | ||
async function openTab(doi) { | ||
await browser.tabs.create({ | ||
url: nexusUrl + doi, | ||
}); | ||
} | ||
|
||
async function onNexusContextClick(info, tab) { | ||
// if right-clicked on link, then parse link address first | ||
let doi = info.linkUrl; | ||
doi = doi ? doi.match(doiRegex)[0].split(";")[0] : doi; | ||
// if link not valid, try the highlighted text | ||
if (!doi) { | ||
doi = info.selectionText; | ||
} | ||
|
||
await openTab(doi); | ||
} | ||
|
||
function getHtml(htmlSource) { | ||
htmlSource = htmlSource[0]; | ||
foundRegex = htmlSource.match(doiRegex); | ||
if (foundRegex) { | ||
var doi = foundRegex[0].split(";")[0]; | ||
doi = doi.replace(/\.pdf/, ""); | ||
var destUrl = nexusUrl + doi; | ||
// console.log("Regex: " + foundRegex); | ||
var creatingTab = browser.tabs.create({ | ||
url: destUrl, | ||
}); | ||
creatingTab.then(); | ||
} else { | ||
browser.browserAction.setBadgeTextColor({ color: "white" }); | ||
browser.browserAction.setBadgeBackgroundColor({ color: trueRed }); | ||
browser.browserAction.setBadgeText({ text: ":'(" }); | ||
async function onMessage(request, sender, sendResponse) { | ||
console.log(request); | ||
if (request.openTab) { | ||
await openTab(request.openTab); | ||
} else if (request.fail) { | ||
await browser.action.setBadgeTextColor({color: "white"}); | ||
await browser.action.setBadgeBackgroundColor({color: trueRed}); | ||
await browser.action.setBadgeText({text: ":'("}); | ||
} | ||
} | ||
|
||
function executeJs() { | ||
const executing = browser.tabs.executeScript({ | ||
code: "document.body.innerHTML", | ||
async function onExtensionClick(tab) { | ||
await browser.scripting.executeScript({ | ||
target: {tabId: tab.id}, | ||
func: async () => { | ||
if (typeof browser === "undefined") { | ||
browser = chrome; | ||
} | ||
|
||
const foundRegex = document.body.innerHTML.match(/\b(10[.][0-9]{4,}(?:[.][0-9]+)*\/(?:(?!["&'<>])\S)+)\b/); | ||
if (foundRegex) { | ||
let doi = foundRegex[0].split(";")[0]; | ||
doi = doi.replace(/\.pdf/, ""); | ||
await browser.runtime.sendMessage({openTab: doi}); | ||
} else { | ||
await browser.runtime.sendMessage({fail: true}); | ||
} | ||
} | ||
}); | ||
executing.then(getHtml); | ||
} | ||
|
||
async function resetBadgeText() { | ||
await browser.action.setBadgeText({text: ""}); | ||
} | ||
|
||
|
||
if (typeof browser === "undefined") { | ||
browser = chrome; | ||
} | ||
|
||
// add nexus option to context menu (right click) | ||
browser.contextMenus.create({ | ||
id: "doi-selection", | ||
title: "Find article by DOI!", | ||
contexts: ["selection"], | ||
id: "nexus-doi-selection", | ||
title: "Find article on Nexus!", | ||
contexts: ["selection", "link"], | ||
}); | ||
|
||
browser.contextMenus.onClicked.addListener((info, tab) => { | ||
var creatingTab = browser.tabs.create({ | ||
url: nexusUrl + info.selectionText, | ||
}); | ||
}); | ||
// when nexus option in context menu is clicked | ||
browser.contextMenus.onClicked.addListener(onNexusContextClick); | ||
|
||
// when extension toolbar icon is clicked | ||
browser.action.onClicked.addListener(onExtensionClick); | ||
|
||
browser.runtime.onMessage.addListener(onMessage); | ||
|
||
browser.browserAction.onClicked.addListener(executeJs); | ||
browser.tabs.onUpdated.addListener(resetBadgeText); |
Oops, something went wrong.