Last active
January 3, 2021 03:29
-
-
Save danreeves/c3dbee99b2eb504eeb4819f5b21eb760 to your computer and use it in GitHub Desktop.
kinopio.club markdown userscript
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
function kinopioExtension() { | |
let dataAttr = 'data-kinopio-extension-by-dnrvs' | |
function processCard(container, node, text) { | |
// Clean out old nodes | |
container.querySelectorAll(`[${dataAttr}]`).forEach(n => n.remove()) | |
// Clone the real one | |
let newNode = node.cloneNode(true /* deep */) | |
// Make sure the real one is hidden | |
node.style.setProperty('display', 'none') | |
// Set up our node | |
newNode.setAttribute(dataAttr, true) | |
// Ensure it's visble | |
newNode.style.setProperty('display', 'block') | |
// Set it's content | |
newNode.innerHTML = parse(text.wholeText) | |
// Insert it after the real one | |
node.after(newNode) | |
} | |
function handler(events) { | |
for (let e of events) { | |
if (e.type === "characterData") { | |
let container = e.target?.parentElement?.parentElement | |
let node = e.target?.parentElement | |
if (container?.classList.contains("card-content-wrap")) { | |
if (!node.hasAttribute(dataAttr)) { | |
processCard(container, node, e.target) | |
} | |
} | |
} | |
} | |
} | |
new MutationObserver(handler).observe(document.body, { attributes: false, childList: true, subtree: true, characterData: true }) | |
document.querySelectorAll('.card-content-wrap').forEach(container => { | |
let node = container.querySelector('.name') | |
let text = node.firstChild.nodeName === "#text" ? node.firstChild : new Text | |
processCard(container, node, text) | |
}) | |
document.addEventListener("click", event => { | |
if (event.target.nodeName === "A") { | |
if (event.target.parentElement.hasAttribute(dataAttr)) { | |
event.stopPropagation() | |
event.stopImmediatePropagation() | |
event.preventDefault() | |
window.open(event.target.getAttribute("href"), "_blank") | |
} | |
} | |
}) | |
} | |
/* | |
https://github.com/mrvautin/downa | |
Modified by @dnrvs | |
MIT License | |
Copyright (c) 2018 Mark Moffat | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
*/ | |
const linksRe = /\[([^[]+)\]\(([^)]+)\)/gmi; | |
const boldRe = /(\*)(.*?)\1/gmi; | |
const emphasisRe = /(\_)(.*?)\1/gmi; | |
const delRe = /~(.*?)~/gmi; | |
const quoteRe = /:"(.*?)":/gmi; | |
const inlineCodeRe = /`([^`]+)`/gmi; | |
const rules = { | |
links: { | |
regex: linksRe, | |
replacer: function(match, $1, $2) { | |
return `<a href="${$2}">${$1}</a>`; | |
} | |
}, | |
bold: { | |
regex: boldRe, | |
replacer: function(match, $1, $2) { | |
return `<strong>${$2}</strong>`; | |
} | |
}, | |
emphasis: { | |
regex: emphasisRe, | |
replacer: function(match, $1, $2) { | |
return `<em>${$2}</em>`; | |
} | |
}, | |
del: { | |
regex: delRe, | |
replacer: function(match, $1, $2) { | |
return `<del>${$1}</del>`; | |
} | |
}, | |
quote: { | |
regex: quoteRe, | |
replacer: function(match, $1, $2) { | |
return `<q>${$1}</q>`; | |
} | |
}, | |
inlineCode: { | |
regex: inlineCodeRe, | |
replacer: function(match, $1, $2) { | |
return `<code>${$1}</code>`; | |
} | |
}, | |
}; | |
/** | |
* @param {String} markdown A string of Markdown content | |
* @returns {String} A HTML formatted string | |
*/ | |
function parse(markdown) { | |
Object.keys(rules).forEach((key) => { | |
markdown = markdown.replace(rules[key].regex, rules[key].replacer); | |
}); | |
return markdown; | |
}; | |
// Lets goooo~ | |
kinopioExtension() |
the querySelector method at line 43 is probably the culprit. text name nodes inside cards have a slightly different dom structure. try replacing it with
// get all the name segment nodes in each card container
document.querySelectorAll('.card-content-wrap').forEach(container => {
let nodes = container.querySelectorAll('.name span')
// filter out tag nodes
nodes = Array.from(nodes).filter(node => node.classList.length === 0)
nodes.forEach(node => {
text = node.textContent
processCard(container, node, text)
})
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a sweet script. Are you still using it? I'm finding that the text isn't being parsed properly, so all the cards end up being blank. If not, no worries – thanks!