Last active
October 5, 2020 19:24
-
-
Save Rudokhvist/7583460470cd819a3e4784349d46a12d to your computer and use it in GitHub Desktop.
A little (and dirty) script to help with $-apocalypse in Unitedgamers steam group.
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 UnitedGamers helper | |
// @namespace https://greasyfork.org/users/2205 | |
// @version 0.4 | |
// @description Replaces $ with link to steamgifts | |
// @author Ryzhehvost | |
// @license Apache-2.0 | |
// @match https://steamcommunity.com/groups/TwarArenaGiveaways/discussions/* | |
// @match https://steamcommunity.com/groups/UG-GA/discussions/* | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
function FixComments (comments){ | |
for (let i=comments.length-1; i>=0; i--) { | |
let text = comments[i].innerHTML; | |
console.log(text); | |
let re = /[^\s^>]*[^m](\/giveaway[^\s^<]*)/g; | |
text = text.replace(re, '<a class="bb_link" href="https://steamgifts.com$1" target="_blank" rel="noreferrer">https://steamgifts.com$1</a>'); | |
if (comments[i].innerHTML != text) { | |
comments[i].innerHTML = text; | |
//add a notice | |
let noticeDiv = document.createElement("span"); | |
noticeDiv.className = "commentthread_workshop_authorbadge"; | |
noticeDiv.appendChild(document.createTextNode("(fixed link)")); | |
comments[i].parentElement.querySelector("div.forum_author_menu").after(noticeDiv); | |
} | |
} | |
} | |
function InjectPasteProcessor (editboxes) { | |
for (let i=editboxes.length-1; i>=0; i--) { | |
editboxes[i].addEventListener('paste', (event) => { | |
let paste = (event.clipboardData || window.clipboardData).getData('text'); | |
let reSG = /https?:\/\/(?:www\.)?steamgifts\.com(\/giveaway[^\s<"]*)/gi | |
if (paste.match(reSG)!= null) { | |
paste = paste.replaceAll(reSG, "$$$1"); | |
event.target.setRangeText(paste,event.target.selectionStart, event.target.selectionEnd,"end"); | |
event.preventDefault(); | |
} | |
}); | |
} | |
} | |
let comments = document.getElementsByClassName('commentthread_comment_text'); | |
FixComments(comments); | |
let editboxes = document.getElementsByClassName('commentthread_textarea'); | |
InjectPasteProcessor(editboxes); | |
let replyboxes = document.getElementsByClassName('forumtopic_reply_textarea'); | |
InjectPasteProcessor(replyboxes); | |
let mutationObserver = new MutationObserver(function(mutations) { | |
mutations.forEach(function(mutation) { | |
mutation.addedNodes.forEach( function(currentValue, currentIndex, listObj) { | |
if (currentValue.nodeType == Node.ELEMENT_NODE) { | |
let comments = currentValue.querySelectorAll("div[class^='commentthread_comment_text']"); | |
FixComments(comments); | |
let editboxes = currentValue.querySelectorAll("textarea[class^='commentthread_textarea']"); | |
InjectPasteProcessor(editboxes); | |
let replyboxes = currentValue.querySelectorAll("textarea[class^='forumtopic_reply_textarea']"); | |
InjectPasteProcessor(replyboxes); | |
} | |
}); | |
}); | |
}); | |
mutationObserver.observe(document.documentElement, { | |
childList: true, | |
subtree: true | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment