Last active
January 8, 2016 21:58
-
-
Save alexwlchan/5adf997e7006c4a4e6a0 to your computer and use it in GitHub Desktop.
Opt-in to spoilers on SFF.SE questions with particular tags
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 SFF.SE Show spoilers on questions with particular tags | |
// @namespace http://tampermonkey.net/ | |
// @version 1.0 | |
// @description Show all spoilers on SFF.SE questions with specified tags | |
// @author alexwlchan | |
// @match *://scifi.stackexchange.com/questions/* | |
// @match *://meta.scifi.stackexchange.com/questions/* | |
// @grant none | |
// ==/UserScript== | |
/* jshint -W097 */ | |
'use strict'; | |
// If every tag on a question is in this array, then the spoilers on this | |
// question will be shown. | |
var SHOW_ME_SPOILERS_IF_TAGGED = [ | |
"harry-potter", | |
"star-wars", | |
]; | |
// Returns an array of tags on a question | |
var getTagsOnQuestion = function() { | |
var taglist = document.getElementsByClassName("post-taglist")[0]; | |
var hrefs = taglist.getElementsByClassName("post-tag"); | |
var tags = []; | |
for (var ii = 0; ii < hrefs.length; ii++) { | |
tags.push(hrefs[ii].innerText); | |
} | |
return tags; | |
} | |
// Returns true/false depending on whether the spoilers on this question | |
// should be shown -- that is, whether every tag on the question is in | |
// SHOW_ME_SPOILERS_IF_TAGGED. | |
var shouldShowSpoilersOnQuestion = function() { | |
// Get the tags on a question. All questions have at least one tag. | |
var tags = getTagsOnQuestion(); | |
for (var jj = 0; jj < tags.length; jj++) { | |
// If we don't find the tag in the array of spoiler-able tags, | |
// then we have to reject the question. | |
if (SHOW_ME_SPOILERS_IF_TAGGED.indexOf(tags[jj]) == -1) { | |
return false; | |
} | |
} | |
return true; | |
} | |
// Modify all the spoilers on the page to show their spoiler text | |
var showSpoilers = function() { | |
var spoilers = document.getElementsByClassName("spoiler"); | |
for (var kk = 0; kk < spoilers.length; kk++) { | |
spoilers[kk].transition = "none"; | |
spoilers[kk].style.color = "#222426"; | |
} | |
} | |
if (shouldShowSpoilersOnQuestion()) { | |
showSpoilers(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment