ページの背景画像を変更するUserScript
このスクリプトは、/customize/背景画像を設定するUserScriptのリメイクです
挙動は少しだけ違います
table:list
ラベル URL コメント
1 サンタ衣装のふーこさん https://i.gyazo.com/a9362ce5ef7fa9ec880efb98089c7fc0.png ふーこさん
2 サンタ衣装のましろさん https://i.gyazo.com/15a000710dfc69c3b8b75cbcd1df6599.png ましろさん
3 サンタ衣装のひよりさん https://i.gyazo.com/4647aaf2ee0cdb72a2556a5b6cef9b7c.png ひよりさん
4 コルセットスカートのふーこさん https://i.gyazo.com/e75528ee087d6bab9cf5f6e168f361a7.png
5 マジシャンましろさん https://i.gyazo.com/ef9ead5cecd3848b1a83c4ff200483e3.png
6 コルセットスカートのひよりさん https://i.gyazo.com/773076671263b4110fd88dd3acb1e1a9.png
7 コルセットスカートのましろさん https://i.gyazo.com/89e6a4f1dec3eea1c3d7366b5e2551b0.png
8 セーラー服のふーこさん https://i.gyazo.com/2839655d6f5772b5ee57620c028b794f.png
9 セーラー服のましろさん https://i.gyazo.com/ab64a525d8eb6a660a19422fd476e503.png
10 セーラー服のひよりさん https://i.gyazo.com/2a47ab88ff90697be1d54688483e6abd.png
使い方
1. テーブル記法で、table:listを作成する
2. APIから list.csvにアクセスできることを確認
3. 自分のページのscript.jsに
code:sample-script.js
import { setupWallpaperMenu } from "/api/code/villagepump/ページの背景画像を変更するUserScript/script.js";
setupWallpaperMenu("/api/table/villagepump/ページの背景画像を変更するUserScript/list.csv");
を追加
setupWallpaperMenu() 引数にはテーブル記法のAPI(CSVファイル)を指定する
リロード
実行例
https://gyazo.com/d1fcd8aaab7e1aadd8e111db4baf40b6
注意事項
リロード時にCSVファイルを読み込んで、メニューを作り直すので、すこし重い
code:script.js
// NOTE: This file is an ES Module.
/**
* Creates a Scrapbox page menu to set a wallpaper from a list of images in a CSV file.
* @param {string} csvUrl - The URL of the CSV file containing image names and URLs.
*/
export function setupWallpaperMenu(csvUrl) {
// Run only in a Scrapbox environment.
if (typeof scrapbox === "undefined") {
console.log("This script is intended to run in a Scrapbox environment.");
return;
}
scrapbox.PageMenu.addMenu({
title: "壁紙",
image: "https://gyazo.com/8a40fddd4f7225e0226401afff3e5795/raw",
onClick: async () => {
const menu = scrapbox.PageMenu("壁紙");
menu.addItem({ title: "Now loading...", onClick: () => null });
try {
const text = await fetchLists(csvUrl);
const urls = [];
const menuItems = [];
// 1. Prepare menu items and a list of valid URLs from the fetched text.
text.split("\n").forEach(line => {
if (!line) return;
const res = line.split(",");
if (res.length < 2 || !res1) return;
const name = res0.length ? res0 : "unknown";
const imageUrl = res1.replace(/\\\/g, "");
if (imageUrlRegex.test(imageUrl)) {
urls.push(imageUrl);
menuItems.push({
title: name,
onClick: () => { changeCss(imageUrl); }
});
}
});
// 2. Clear the "Now loading..." message.
menu.removeAllItems();
// 3. Add the new menu items.
menu.addItem({
title: "消す",
onClick: () => { changeCss(null); }
});
menuItems.forEach(item => menu.addItem(item));
if (urls.length > 0) {
menu.addItem({
title: "ランダム",
onClick: () => { changeCss(choice(urls)); }
});
}
} catch (error) {
console.error("Failed to load wallpaper list:", error);
menu.removeAllItems();
menu.addItem({ title: "Error: Failed to load", onClick: () => null });
}
}
});
}
// --- Helper Functions (exported for testing) ---
export const fetchLists = async (url) => {
const response = await fetch(url);
if (!response.ok) {
throw new Error("Network response was not ok");
}
const text = await response.text();
// Assuming the CSV has a header row to be skipped (slice(1))
// and lines might start with a comma to be removed.
return text.split("\n").slice(1)
.map(line => line.replace(/^,/, ""))
.join("\n");
};
export const changeCss = (imageUrl) => {
const styleId = "__bgi__";
let style = document.getElementById(styleId);
if (style) {
style.remove();
}
// If imageUrl is not provided, simply remove the style and exit.
if (!imageUrl) {
return;
}
const css = `
.page {
background-image: url("${imageUrl}");
background-repeat: repeat-y;
background-attachment: scroll;
background-position: right top;
background-size: 60% auto;
}
`;
style = document.createElement("style");
style.setAttribute("id", styleId);
style.appendChild(document.createTextNode(css.trim()));
document.head.appendChild(style);
};
export const choice = (arr) => {
if (!arr || arr.length === 0) {
return null;
}
return arrMath.floor(Math.random() * arr.length);
};
// A more robust regex for image URLs.
export const imageUrlRegex = /(https?:\/\/[\w\-\.\/?\#\:\%\u3000-\u30FE\u4E00-\u9FA0\uFF01-\uFFE3\\]+)\.(jpg|jpeg|gif|png|webp|svg)/i;
#井戸端アドベントカレンダー2025
UserScript.icon