Skip to content

Instantly share code, notes, and snippets.

@stormchasing
Last active June 22, 2018 21:27
Show Gist options
  • Save stormchasing/1d78d964bc9f3e6a203258ee1ad1516f to your computer and use it in GitHub Desktop.
Save stormchasing/1d78d964bc9f3e6a203258ee1ad1516f to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Highlight tornado in SPC outlooks
// @namespace http://okstorms.com/
// @version 0.2
// @description Highlights the word tornado in SPC outlooks
// @author JR Hehnly (@stormchasing)
// @match https://www.spc.noaa.gov/products/outlook*
// @grant none
// ==/UserScript==
(function() {
'use strict';
var keywordCsvList = 'tornado';
var highlightStyle = 'color: #ffffff; background-color: #ff0000;';
var regexQuantifiers = /[-\/\\^$*+?.()|[\]{}]/g;
var keywords = keywordCsvList.replace(regexQuantifiers, '\\$&').split(',').join('|');
var pat = new RegExp('(' + keywords + ')', 'gi');
var span = document.createElement('span');
var snapElements = document.evaluate(
'.//text()[normalize-space() != "" ' +
'and not(ancestor::style) ' +
'and not(ancestor::script) ' +
'and not(ancestor::textarea) ' +
'and not(ancestor::code) ]',
document.body, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
if (!snapElements.snapshotItem(0)) { return; }
for (var i = 0, len = snapElements.snapshotLength; i < len; i++) {
var node = snapElements.snapshotItem(i);
if (pat.test(node.nodeValue)) {
var sp = span.cloneNode(true);
sp.innerHTML = node.nodeValue.replace(pat, '<span style="' + highlightStyle + '">$1</span>');
node.parentNode.replaceChild(sp, node);
}
}
keywordCsvList = 'wind';
highlightStyle = 'color: #ffffff; background-color: #0000ff;';
regexQuantifiers = /[-\/\\^$*+?.()|[\]{}]/g;
keywords = keywordCsvList.replace(regexQuantifiers, '\\$&').split(',').join('|');
pat = new RegExp('(' + keywords + ')', 'gi');
span = document.createElement('span');
snapElements = document.evaluate(
'.//text()[normalize-space() != "" ' +
'and not(ancestor::style) ' +
'and not(ancestor::script) ' +
'and not(ancestor::textarea) ' +
'and not(ancestor::code) ]',
document.body, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
if (!snapElements.snapshotItem(0)) { return; }
for (i = 0, len = snapElements.snapshotLength; i < len; i++) {
node = snapElements.snapshotItem(i);
if (pat.test(node.nodeValue)) {
sp = span.cloneNode(true);
sp.innerHTML = node.nodeValue.replace(pat, '<span style="' + highlightStyle + '">$1</span>');
node.parentNode.replaceChild(sp, node);
}
}
keywordCsvList = 'hail';
highlightStyle = 'color: #ffffff; background-color: #00ff00;';
regexQuantifiers = /[-\/\\^$*+?.()|[\]{}]/g;
keywords = keywordCsvList.replace(regexQuantifiers, '\\$&').split(',').join('|');
pat = new RegExp('(' + keywords + ')', 'gi');
span = document.createElement('span');
snapElements = document.evaluate(
'.//text()[normalize-space() != "" ' +
'and not(ancestor::style) ' +
'and not(ancestor::script) ' +
'and not(ancestor::textarea) ' +
'and not(ancestor::code) ]',
document.body, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
if (!snapElements.snapshotItem(0)) { return; }
for (i = 0, len = snapElements.snapshotLength; i < len; i++) {
node = snapElements.snapshotItem(i);
if (pat.test(node.nodeValue)) {
sp = span.cloneNode(true);
sp.innerHTML = node.nodeValue.replace(pat, '<span style="' + highlightStyle + '">$1</span>');
node.parentNode.replaceChild(sp, node);
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment