Skip to content

Instantly share code, notes, and snippets.

@evrenesat
Created February 29, 2020 22:35
Show Gist options
  • Select an option

  • Save evrenesat/2d718c1ac1fa9e3616538dd00b83ee70 to your computer and use it in GitHub Desktop.

Select an option

Save evrenesat/2d718c1ac1fa9e3616538dd00b83ee70 to your computer and use it in GitHub Desktop.
/*
==UserScript==
@name Solarize
@match https://3.basecamp.com/3788301/*
@match https://github.com/solarmonkey/*
@version 1.0
==/UserScript==
*/
const isGithub = document.location.host.includes('github')
// don't replace text within these tags
var skipTags = { 'a': 1, 'style': 1, 'script': 1, 'iframe': 1 }
// find text nodes to apply replFn to
var findKW = function(el, term, replFn) {
var child, tag
for (var i = el.childNodes.length - 1; i >= 0; i--) {
child = el.childNodes[i]
if (child.nodeType == 1) { // ELEMENT_NODE
tag = child.nodeName.toLowerCase()
if (!(tag in skipTags)) {
findKW(child, term, replFn)
}
} else if (child.nodeType == 3) { // TEXT_NODE
console.log(child.data)
replaceKW(child, term, replFn)
}
}
}
// replace terms in text according to replFn
var replaceKW = function(text, term, replFn) {
var match,
matches = []
while (match = term.exec(text.data)) {
matches.push(match)
}
for (var i = matches.length - 1; i >= 0; i--) {
match = matches[i]
// cut out the text node to replace
text.splitText(match.index)
text.nextSibling.splitText(match[0].length)
text.parentNode.replaceChild(replFn(match[1], match[0]), text.nextSibling)
}
}
link_style = 'background-color: rgba(255, 205, 112, 0.56); color: black !important; border-radius: 4px; padding: 2px;'
const doReplacement = function() {
console.log('Do replacement')
findKW(
document.body,
new RegExp(/KB(\d+)/, 'gi'),
function(match, text) {
var link = document.createElement('a')
link.href = 'https://solarmonkey.kanbanize.com/ctrl_board/5/cards/' + match + '/details/'
link.target = '_blank'
link.style = link_style
link.innerHTML = text
return link
}
)
!isGithub && findKW(
document.body,
new RegExp(/\#(\d\d\d\d)/, 'g'),
function(match, text) {
var link = document.createElement('a')
link.href = 'https://github.com/solarmonkey/app/pull/' + match
link.target = '_blank'
link.style = link_style
link.innerHTML = text
return link
}
)
}
const isSafari = () => Boolean(navigator.vendor.match(/[Aa]+pple/g))
// Select the node that will be observed for mutations
const targetNode = document.body
window.__delayed = 0
// Options for the observer (which mutations to observe)
const config = { childList: true, subtree: true }
// // Callback function to execute when mutations are observed
const observerWay = function() {
const callback = function(mutationsList, observer) {
// Use traditional 'for loops' for IE 11
for (let mutation of mutationsList) {
if (mutation.type === 'childList') {
if (window.__delayed) clearTimeout(window.__delayed)
window.__delayed = setTimeout(doReplacement, 1000)
}
}
}
// Create an observer instance linked to the callback function
const observer = new MutationObserver(callback)
// Start observing the target node for configured mutations
observer.observe(targetNode, config)
}
const oldWay = function() {
document.addEventListener('DOMNodeInserted', function() {
if (window.__delayed) clearTimeout(window.__delayed)
window.__delayed = setTimeout(doReplacement, 1000)
}, false)
}
oldWay()
doReplacement()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment