XKCD -- Easy Random

Quicker and more reliable "random comic" button

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name     XKCD -- Easy Random
// @description Quicker and more reliable "random comic" button
// @license MIT
// @version  1
// @namespace https://keybase.io/jpaugh
// @support  Chat with me on Keybase
// @author   Jonathan Paugh
// @grant    none
// @match 	 https://xkcd.com/*
// @compatible firefox
// @compatible chrome
// @compatible IE11 --- not that it helps
// ==/UserScript==

var MAGICAL_BG_COLOR = "orangered"
var XKCDS_WHEN_CHECKED = 2134
var XKCD_CHECK_DATE = new Date(2019, 4, 9)
var COMICS_PER_WEEK = 3

function guestimateCurrentCount() {
  var now = new Date()
  var MILLI_PER_WEEK = 7 * 24 * 60 * 60 * 1000
  var weeksSinceChecked = (now.getTime() - XKCD_CHECK_DATE.getTime()) / MILLI_PER_WEEK
  return XKCDS_WHEN_CHECKED + Math.floor(weeksSinceChecked * COMICS_PER_WEEK) // Works for past and future dates! :-D
}

function getRandomComicInclusive(min, max) {
  min = Math.ceil(min) // Why did you give me a fractional min? You sneaky caller!
  max = Math.floor(max)
  return Math.floor(Math.random() * (max - min + 1)) + min
}

var min = 1
var max = guestimateCurrentCount()
// TODO: Add a fudge factor (+20) to the max, and then pre-load the new comic to ensure it exists.
// Maybe I should load it via AJAX and overwrite the current page? IDK

var randLinks = document.querySelectorAll("a[href^='//c.xkcd.com/random/comic']")
Array.prototype.forEach.call(randLinks, function (el) {
  el.style.backgroundColor = MAGICAL_BG_COLOR
  el.addEventListener("click", function (event) {
    (event.preventDefault)
      ? event.preventDefault()
      : (event.returnValue = false)
    window.location.href = "https://xkcd.com/" + getRandomComicInclusive(min, max)
  })
});