Created
February 5, 2016 01:38
-
-
Save mosaicer/3ce234b47e2be88e7f7d to your computer and use it in GitHub Desktop.
Serve cakes and let's party
This file contains hidden or 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 Serve Cakes | |
| // @namespace https://github.com/mosaicer | |
| // @author mosaicer | |
| // @description Serve cakes and let's party | |
| // @match http://*.reddit.com/* | |
| // @match https://*.reddit.com/* | |
| // @version 1.0 | |
| // @run-at document-idle | |
| // @grant GM_addStyle | |
| // ==/UserScript== | |
| (function () { | |
| 'use strict'; | |
| var hrefArray = location.href.split('/'), | |
| commentsFlag = hrefArray[5] === 'comments', | |
| siteTable = commentsFlag ? | |
| document.getElementById('siteTable_t3_' + hrefArray[6]) : | |
| document.getElementById('siteTable'), | |
| serveCakes = function (targetNode) { | |
| [].forEach.call(targetNode.querySelectorAll('.tagline'), | |
| function (tagLine) { | |
| var spanTag = tagLine.querySelector('.userattrs'); | |
| if (!!spanTag) { | |
| if (!!spanTag.childElementCount) { | |
| if (!!!spanTag.querySelector('.cakeday')) { | |
| spanTag.innerHTML = spanTag.innerHTML.slice(0, -1) + | |
| ',' + generateCakesHtml(tagLine); | |
| } | |
| } else { | |
| spanTag.innerHTML = '[' + generateCakesHtml(tagLine); | |
| } | |
| } | |
| } | |
| ); | |
| }, | |
| generateCakesHtml = function (tagLine) { | |
| var userName = commentsFlag ? | |
| tagLine.children[1].textContent : tagLine.children[0].textContent; | |
| return '<a class="cakeday" title="' + userName + | |
| ' は reddit の誕生日を祝いました!" href="/user/' + userName + | |
| '">🍰</a>]'; | |
| }; | |
| [].forEach.call(siteTable.children, serveCakes); | |
| new MutationObserver(function (mutations) { | |
| mutations.forEach(function (mutation) { | |
| [].forEach.call(mutation.addedNodes, function (theNode) { | |
| if (theNode.nodeName === 'DIV') { | |
| if (commentsFlag) { | |
| if (theNode.hasAttribute('data-fullname')) { | |
| serveCakes(theNode); | |
| } | |
| } else if (!theNode.hasAttribute('url')) { | |
| [].forEach.call(theNode.children, serveCakes); | |
| } | |
| } | |
| }); | |
| }); | |
| }).observe(siteTable, {childList: true, subtree: true}); | |
| }()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment