Last active
June 18, 2022 09:49
-
-
Save btamayo/d7cc58465df99c5a4e8bd47859c9a075 to your computer and use it in GitHub Desktop.
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 Reddit Title Bold For r/acturnips | |
// @version 0.1 | |
// @description Bolds Reddit Post Titles | |
// @author btamayo | |
// @match https://www.reddit.com/r/acturnips/* | |
// @run-at document-idle | |
// @grant GM_addStyle | |
// @grant GM_getResourceText | |
// @grant GM_log | |
// @require http://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js | |
// @resource theme http://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/tomorrow-night.min.css | |
// ==/UserScript== | |
const STYLE_TWEAKS = `dd { border-width: thin; border-style: groove; margin: 0em 1em 1em 1em; } | |
dt > tt { margin: 0em 1em 1em 1em; color: chartreuse; } | |
dt > var { color: khaki; } | |
dd > p { margin: 1em 1em 1em 1em; } | |
.hljs { font-size: 1.1em; }`; | |
let concat = (previous, current) => [...previous, ...current]; | |
let flatMap = (xs, fn) => xs.map(fn).reduce(concat, []); | |
// https://stackoverflow.com/a/14767071/1354493 | |
let numbersWords = [ | |
'one', | |
'two', | |
'three', | |
'four', | |
'five', | |
'six', | |
'seven', | |
'eight', | |
'nine', | |
'ten', | |
'eleven', | |
'twelve', | |
'thirteen', | |
'fourteen', | |
'fifteen', | |
'sixteen', | |
'seventeen', | |
'eighteen', | |
'nineteen', | |
'twenty', | |
'thirty', | |
'forty', | |
'fifty', | |
'sixty', | |
'seventy', | |
'eighty', | |
'ninety', | |
]; | |
const regex = /\d\d\d/gm; | |
let hasPrice = (str) => { | |
let price = -1; | |
const matches = str.matchAll(regex); | |
for (const match of matches) { | |
GM_log( | |
`Found ${match[0]} start=${match.index} end=${ | |
match.index + match[0].length | |
}.` | |
); | |
if (match[0]) { | |
price = match[0]; | |
price = parseInt(price, 10); | |
} | |
} | |
return price; | |
}; | |
function wordNumbers(str) { | |
let strArr = str.split(' '); | |
let modified = strArr.map((word) => { | |
let wordStripped = word.toLowerCase().replace(/[^a-z]/g, ''); | |
let found = numbersWords.indexOf(wordStripped) | |
if (found > 0) { | |
return word = `<b>${word}</b>` | |
} | |
return word; | |
}) | |
let ret = modified.join(' ') | |
return ret; | |
} | |
function getTitles(e) { | |
GM_log(e); | |
if (e.classList.contains('title')) { | |
let titles = e.querySelectorAll('a.title'); | |
[...titles].forEach((title) => { | |
let price = hasPrice(title.innerHTML); | |
if (price > 0) { | |
title.innerHTML = title.innerHTML.replace(price, `<b>${price}</b>`); | |
GM_log(price); | |
if (price > 600) { | |
title.style.backgroundColor = '#03b300'; | |
title.style.color = 'white'; | |
title.style.fontSize = 'x-large'; | |
} else if (price > 550) { | |
title.style.backgroundColor = '#c1ffbf'; | |
title.style.color = '#003821'; | |
// title.style.fontSize = 'large'; | |
} else if (price < 260 && price > 0) { | |
// title.style.backgroundColor = '#86a585'; | |
title.style.color = '#8a7979'; | |
title.style.fontSize = 'small'; | |
let parent = title.closest('[data-context="listing"]'); | |
parent.style.display = 'none'; | |
GM_log(`parent:`); | |
GM_log(parent); | |
} else { | |
// title.style.backgroundColor = '#86a585'; | |
title.style.color = '#8a7979'; | |
title.style.fontSize = 'small'; | |
let parent = title.closest('[data-context="listing"]'); | |
// parent.style.backgroundColor = 'yellow'; | |
GM_log(`parent:`); | |
GM_log(parent); | |
} | |
} else { | |
title.innerHTML = wordNumbers(title.innerHTML) | |
} | |
}); | |
} | |
} | |
let titles = ['[SW] Nooks buying at 604!']; | |
function test() { | |
[...titles].forEach((title) => { | |
let answer = hasPrice(title); | |
console.log(answer); | |
}); | |
} | |
function process(e) { | |
GM_log(`New element added ${e}`); | |
GM_log(e); | |
if (e.classList > 0) { | |
[...e.getElementsByClassName('title')].forEach(getTitles); | |
} | |
} | |
(function () { | |
'use strict'; | |
GM_addStyle(STYLE_TWEAKS); | |
let linklisting = document.querySelector('linklisting'); | |
GM_log('linklisting: '); | |
GM_log(linklisting); | |
[...document.getElementsByClassName('title')].forEach(getTitles); | |
new MutationObserver((ms) => { | |
flatMap(ms, (m) => m.addedNodes).forEach(process); | |
// flatMap(ms, (m) => m.addedNodes).forEach(process); | |
}).observe(document.getElementsByClassName('linklisting')[0], { childList: true, subtree: false }); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment