4chan Post Randomizer

Adds a button that redirects to a random post on a random board

2018-05-28 기준 버전입니다. 최신 버전을 확인하세요.

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

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

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

// ==UserScript==
// @name         4chan Post Randomizer
// @namespace    http://www.4chan.org/
// @version      0.4
// @description  Adds a button that redirects to a random post on a random board
// @include      http://boards.4chan.org/*
// @include      https://boards.4chan.org/*
// @grant        none
// @run-at       document-end
// ==/UserScript==

// use fancy mobile-like buttons on 4chan X, simple HTML form button otherwise
if (document.documentElement.classList.contains('fourchan-x')) {
  var randomizer_button = document.createElement('a');
  randomizer_button.classList.add('qr-link');
  // dummy href like original button
  randomizer_button.href = 'javascript:;';
  var randomizer_container = document.createElement('div');
  randomizer_container.classList.add('qr-link-container');
  // always force visible (Onee-Chan hides the quicklink by default)
  randomizer_container.style['display'] = 'block';
  // no extra margins please
  randomizer_container.appendChild(randomizer_button);
} else {
  var randomizer_button = document.createElement('button');
  randomizer_button.style['display'] = 'block';
  randomizer_button.style['margin-left'] = 'auto';
  randomizer_button.style['margin-right'] = 'auto';
  randomizer_button.style['margin-bottom'] = '8px';
  randomizer_button.style['font-family'] = 'inherit';
  var randomizer_container = randomizer_button;
}
randomizer_button.setAttribute('id', 'throwthedice');
randomizer_button.appendChild(document.createTextNode('Go to Random Post'));
// insert below lower blotter line, or the full-width <hr> on /f/
if (location.pathname.startsWith('/f/')) {
  var insert_before = document.querySelector('body > script');
} else {
  var insert_before = document.querySelector('.middlead');
}
insert_before.parentNode.insertBefore(randomizer_container, insert_before);

randomizer_button.addEventListener('click', function() {
  var xhr = new XMLHttpRequest();
  // cascaded re-use of the XMLHttpRequest for the board list, catalog and thread
  xhr.open('GET', '//a.4cdn.org/boards.json');
  xhr.onreadystatechange = function() {
    if (xhr.readyState == 4 && xhr.status == 200 && xhr.getResponseHeader("Content-Type") == "application/json") {
      var boards = JSON.parse(xhr.response)['boards'].map(b => b.board);
      var random_board = boards[Math.floor(boards.length * Math.random())];
      xhr.open('GET', '//a.4cdn.org/' + random_board + '/catalog.json');
      xhr.onreadystatechange = function() {
        if (xhr.readyState == 4 && xhr.status == 200 && xhr.getResponseHeader("Content-Type") == "application/json") {
          var threads = JSON.parse(xhr.response).map(page => page['threads'].map(thread => thread['no'])).reduce((p1, p2) => p1.concat(p2), []).slice(-150);
          var random_thread = threads[Math.floor(threads.length * Math.random())];
          xhr.open('GET', '//a.4cdn.org/' + random_board + '/thread/' + random_thread + '.json');
          xhr.onreadystatechange = function() {
            if (xhr.readyState == 4 && xhr.status == 200 && xhr.getResponseHeader("Content-Type") == "application/json") {
              var posts = JSON.parse(xhr.response)['posts'].map(p => p.no);
              var redirect_link = '//boards.4chan.org/' + random_board + '/thread/' + random_thread;
              var post_index = Math.floor(posts.length * Math.random());
              if (post_index > 0) {
                redirect_link += '#p' + posts[post_index];
              }
              location.href = redirect_link;
            }
          };
          xhr.send();
        }
      };
      xhr.send();
    }
  };
  xhr.send();
});