Skip to content

Instantly share code, notes, and snippets.

@lbeckman314
Forked from cannin/highlight_pmc.user.js
Last active October 30, 2019 16:28
Show Gist options
  • Select an option

  • Save lbeckman314/f8f8b02d14c381bf96aee6253bdbe604 to your computer and use it in GitHub Desktop.

Select an option

Save lbeckman314/f8f8b02d14c381bf96aee6253bdbe604 to your computer and use it in GitHub Desktop.
Highlight PMC Text
// ==UserScript==
// @name Highlight PMC
// @namespace http://tampermonkey.net/
// @version 0.1
// @description TBA
// @author cannin
// @require https://cdn.jsdelivr.net/mark.js/8.6.0/mark.min.js
// @grant GM.getValue
// @grant GM.setValue
// @include http*://www.ncbi.nlm.nih.gov/pmc/articles/*
// @home https://gist.github.com/cannin/4a30c1e3551b83f059c2206e491a082a
// ==/UserScript==
// TODO Fix character encoding/decoding
// (e.g. https://www.ncbi.nlm.nih.gov/pmc/articles/PMC2695807/?text=NMA)+inhibits+DmXR).
// TODO Readjust scroll after image loading
// (e.g. https://www.ncbi.nlm.nih.gov/pmc/articles/PMC3441039/?text=L-NMA,+ADMA+is+a+product).
(function() {
'use strict';
const text = new URLSearchParams(window.location.search).get("text");
let map = new Map();
map.set("[.*+?^${}()|[\]\\]", "\\$&");
// Correct science specific elements.
map.set("alpha|beta|gamma|delta|epsilon", ".*");
// Correct line breaks in NCBI text.
map.set(" ", "[\\\n ]");
let regExpStr = text;
map.forEach((value, key, map) => {
regExpStr = regExpStr.replace(new RegExp(key, "g"), value);
});
const regExp = new RegExp(regExpStr, "gi");
const marker = new Mark(document.querySelector(".content"));
try {
// From: https://jsfiddle.net/julmot/vpav6tL1/
marker.markRegExp(regExp);
document.getElementsByTagName("mark")[0].scrollIntoView();
} catch (e) {
console.error("e: " + e);
try {
// Nonstandard fallback approach.
window.find(text);
} catch (e1) {
console.error("e1: " + e1);
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment