5ch.net Image Inserter

Insert images found in 5ch threads.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        5ch.net Image Inserter
// @name:ja     5ch.net Image Inserter
// @namespace   Violentmonkey Scripts
// @include     */r/*/*/*
// @include     */test/read.cgi/*/*
// @include     */read.php/*/*
// @include     */log/*/*/*/
// @include     *.5ch.net/*/*
// @grant       unsafeWindow
// @version     1.2
// @author      -
// @description Insert images found in 5ch threads.
// @description:ja 5chのスレッドにあるイメージをインサートする。
// @license     MIT
// @icon        https://5ch.net/favicon.ico
// ==/UserScript==

function fileExtension(url) {
  const parts = url.split('/')
  const fileParts = parts[parts.length - 1].split('.')
  let ext = fileParts[fileParts.length - 1]
  ext = ext.replace(/\?.*$/, '')
  return ext
}

const imageExtensions = [
  'gif',
  'jpg',
  'jpeg',
  'png',
  'webp'
]
const videoExtensions = [
  'webm',
  'mp4'
]
function typeOf(url) {
  const ext = fileExtension(url).toLowerCase()
  if (imageExtensions.includes(ext)) {
    return 'image'
  } else if (videoExtensions.includes(ext)) {
    return 'video'
  } else {
    return 'other'
  }
}

function cleanUrl(url) {
  const newUrl = url.replace(/^http.*jump.5ch.net\/\?/, '')
  return newUrl
}

function insertImage(a) {
  const newUrl = cleanUrl(a.href)
  a.innerHTML = `<img class="inserted" src="${newUrl}" loading="lazy" />`
}

function insertVideo(a) {
  const newUrl = cleanUrl(a.href)
  a.innerHTML = `<video controls loop class="inserted"><source src="${newUrl}" /></video>`
}

// main _______________________________________________________________

// Apply CSS
let css = `
video {
  max-width: 100%;
}
`
let style = document.createElement("style");
style.type = "text/css";
style.appendChild(document.createTextNode(css));
document.head.appendChild(style);

// scan 2023-04+ era DOM
document.querySelectorAll('article .post-content a').forEach((a) => {
  const t = typeOf(a.href)
  switch (t) {
  case 'image':
    insertImage(a)
    break;
  case 'video':
    insertVideo(a)
    break;
  }
})