Last active
October 29, 2019 21:25
-
-
Save cannin/4a30c1e3551b83f059c2206e491a082a to your computer and use it in GitHub Desktop.
Highlight PMC Text
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
// ==UserScript== | |
// @name Highlight PMC | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description TBA | |
// @author cannin | |
// @require https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js | |
// @require https://cdnjs.cloudflare.com/ajax/libs/mark.js/8.11.1/jquery.mark.min.js | |
// @grant GM.getValue | |
// @grant GM.setValue | |
// @include http*://www.ncbi.nlm.nih.gov/pmc/articles/* | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
function decodeStr(str) { | |
var txt = document.createElement("textarea"); | |
txt.innerHTML = str; | |
return txt.value; | |
} | |
var vars = {}; | |
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m, key, value) { | |
vars[key] = value; | |
}); | |
var url = document.URL; | |
console.log("D: " + url + " V: " + JSON.stringify(vars)); | |
var text = vars.text; | |
// Fix HTML entities | |
text = text.replace(/[+]/g, ' '); | |
text = decodeURIComponent(text); | |
// Escape regexp | |
var regexpStr = text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); | |
// Correct science specific elements | |
regexpStr = regexpStr.replace(/alpha|beta|gamma|delta|epsilon/g, '.*'); | |
//regexpStr = regexpStr.replace(/-/g, '.*'); | |
regexpStr = regexpStr.replace('.*.*', '.*'); | |
//regexpStr = regexpStr.replace(/ /g, '.*'); | |
console.log("NCBI: 3 " + regexpStr); | |
var regexp = new RegExp("\\b" + regexpStr + "\\b", "gi"); | |
try { | |
// From: https://jsfiddle.net/julmot/vpav6tL1/ | |
$("#maincontent").markRegExp(regexp); | |
$(window).scrollTop($('mark').offset().top-100); | |
} catch (e1) { | |
console.log(e1); | |
try { | |
window.find(text); | |
} catch (e2) { | |
console.log(e2); | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment