// ==UserScript==
// @name AUTOMOMEify for OTT
// @namespace http://mrob.com/time/scripts-beta
// @description Inserts automeme output in the xkcd fora.
// @version 48660.32
// @downloadURL http://mrob.com/time/scripts-beta/automomeify.user.js.txt
// @include http://forums.xkcd.com/*
// @include http://www.forums.xkcd.com/*
// @include http://fora.xkcd.com/*
// @include http://www.fora.xkcd.com/*
// @include http://forums3.xkcd.com/*
// @include http://echochamber.me/*
// @include http://www.echochamber.me/*
// @grant GM_xmlhttpRequest
// @grant GM.xmlHttpRequest
// ==/UserScript==
/* AUTOMOMEify version 29519.72
* Copyright (C) 2014 Penguin Development,
* Copyright (c) 2014-2016 Robert Munafo (mrob27)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
NOTES
Here are some posts that can be used to test what this script does:
fora.xkcd.com/viewtopic.php?p=3592407#p3592407
REVISION HISTORY
np10194.38 First version by @Link, OTT:1951:12#p3590925
np10252.58 Many changes by @mrob27 to make it play better with other
userscripts. More needs to be done as this version does not make nice HTML
memes, it just substitutes plain text.
np12258.49 Maximum title langth is actually 60 characters.
np26307.28 Use mrob.com URL because @Link's version is randomly
doubling letters without my consent.
np26493.87 Start subject lines with "1190: Time: "
np26494.20 Use Link's much more thorough tests to find out if we're
on any OTT-related page; this makes the meme-in-subject thing work
when posting a reply via the "quote" button, in addition to when
making an original comment with the Post Reply button.
np29516.37 Clean up (syntax errors, etc.). Increase length limit to 100
(because the fora software's limit has increased).
np40912.21 Apparently GM_xmlhttpRequest has become GM.xmlHttpRequest
np48660.32 Fix errors/warnings reported by TamperMonkey
*/
var memes = [];
var mix = 0; // meme array index
console.info("typeof(GM) == " + typeof(GM));
if (typeof(GM) != 'undefined') {
console.info("typeof(GM.xmlHttpRequest) == " + typeof(GM.xmlHttpRequest));
}
var xhp = -1;
if (typeof(GM_xmlhttpRequest) == 'function') {
xhp = GM_xmlhttpRequest;
} else if (typeof(GM) == 'object') {
if (typeof(GM.xmlHttpRequest) == 'function') {
xhp = GM.xmlHttpRequest;
}
}
console.info("typeof(xhp) == " + typeof(xhp));
// xhp = -1; console.info("2. xhp = " + xhp);
function htmlify(m)
{
var spl = m.split ('_');
var s = spl[0];
var j = 0;
for (j = 1; j < spl.length - 1; j++) {
s = s + ((j % 2) ? "" : "<\/span>");
s = s + spl[j];
}
if (spl.length > 1) {
s = s + ((spl.length % 2) ? "<\/span>" : "_") + spl[spl.length - 1];
}
return ("");
}
/* Submit a request to Link's AUTOMOME server and get some meme
* phrases. %%% I should also initiate a request to
* http://mrob.com/time/automome/butan.php?n=27 and whichever request
* completes first can set a flag telling the other to just do the
* memes.concat() and not call automomeify. This will make the script
* more resilient to the failage caused by a non-responsive server.*/
function get_memes ()
{
if (memes.length === 0) {
// var req = new GM.xmlHttpRequest ({
if (xhp == -1) {
// shim setup failed to detect a function
memes = ["FILLER MEME"];
automomeify(false);
} else {
var req = new xhp ({
method: "GET",
// formerly automome.penguindevelopment.org/automome-web.py?n=32
url: "http://mrob.com/time/automome/butan.php?n=32",
onload: function (resp)
{
memes = memes.concat(resp.responseText.trim().split ("\n"));
// console.info('got ' + memes.length + ' memes');
automomeify(false);
}
});
}
} else {
automomeify(false);
}
}
/* %%% Once I figure out how O_o, this will:
* Change a text node to a more complex node containing some of the
* original text plus HTML for an embedded AUTOMOME
*
* But for now we just change the text to the same text with memes in place
* of any found magichars. */
function replaceTextContent(tNode)
{
/* Get the node's text */
var txt = tNode.textContent;
/* Replace any embedded &Ue4a6 chars with the meme we just got. */
var idx;
while ((idx = txt.indexOf ("\uE4A6")) != -1) {
// console.info('rtc found magic char in "' + txt + '"');
mix = (mix + 1) % (memes.length);
// was: txt.slice(0, idx) + htmlify(memes[mix]) + memes[mix] + txt.slice(idx + 1)
txt = txt.slice(0, idx) + memes[mix] + txt.slice(idx + 1);
}
// tNode.innerHTML = txt; // This does not work `\O_o/'
tNode.textContent = txt; // plain-text cop-out
}
/* This recursively (depth-first) traverses a given DOM node and its
* subnodes for text, and applies the filter to each. */
function changeTextNodes(node)
{
var length, childNodes;
// If this is a text node, AUTOMOMEify it
if (node.nodeType == Node.TEXT_NODE) {
replaceTextContent(node);
} else {
// This is something other than a text node, recurse any children
childNodes = node.childNodes;
length = childNodes.length;
for (var i=0; i max_title_length && memes.length > 0) {
m = memes.pop();
}
// console.info("got '" + m + "'");
if (m.length < max_title_length) {
subj.value = "1190: Time: " + m;
} else {
if (detect) {
return true;
} else {
get_memes(); // Note this calls automomeify again after getting stuff
return false;
}
}
}
} else if (document.location.href.indexOf ("viewtopic.php") != -1) {
if (detect) {
/* We're just detecting. Use innerHTML as a quick way to spot
* the presence of the magic character */
if (document.body.innerHTML.indexOf("\uE4A6") != -1) {
return true;
}
return false;
}
changeTextNodes(document.body);
}
return false;
}
/* Ask automomeify to detect if there is any work to do on this page */
if (automomeify(true)) {
/* Yes! We need memes. get_memes will call automomeify(false) when
* it gets some. */
get_memes();
}