QUOTE(雾雨玲子 @ Jun 22 2025, 06:56)

Recently I found a script that can download works from dlsite that are only available for web browsing. It's a bit rough; does anyone want to optimize it?
(function() {
'use strict';
let revokeObjectURL = URL.revokeObjectURL
URL.revokeObjectURL = function(url) {
var element = document.getElementsByTagName('img');
if(element && element.length) {
var page = element[element.length - 1].getAttribute('alt');
if(page) {
var fileName = '';
var link = document.createElement('a');
link.href = url;
link.download = fileName + page + '.jpeg';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}
return revokeObjectURL.apply(this, arguments)
}
})();
// ==UserScript==
// @name DLsite Web Viewer Downloader (Optimized)
// @namespace [
tampermonkey.net]
http://tampermonkey.net/// @version 2.0
// @description Optimizes the script to download images from DLsite's web viewer by hooking URL.revokeObjectURL.
// @author YourName
// @match [
www.dlsite.com]
https://www.dlsite.com/books/viewer/=/product_id/*// @grant none
// @run-at document-start
// ==/UserScript==
(function() {
'use strict';
// --- 設定區 (請根據實際頁面情況調整) ---
// 1. 目標圖片的 CSS 選擇器,用來精準定位圖片
const IMAGE_SELECTOR = '.pinch-zoom-target > img';
// 2. 作品標題的 CSS 選擇器,用來命名檔案
const TITLE_SELECTOR = '.work_name a';
// -------------------------------------
// 保存原始的 revokeObjectURL 函式
const originalRevokeObjectURL = URL.revokeObjectURL;
// 將常見的圖片 MIME 類型對應到副檔名
const mimeTypeToExtension = {
'image/jpeg': 'jpg',
'image/png': 'png',
'image/gif': 'gif',
'image/webp': 'webp',
'image/bmp': 'bmp',
};
// 清理檔名中的無效字元
function sanitizeFilename(name) {
// 將 Windows/Mac/Linux 不允許的字元替換成底線
return name.replace(/[\\/:*?"<>|]/g, '_');
}
// 覆寫原始函式
URL.revokeObjectURL = async function(url) {
// 我們只處理 blob URL,因為圖片資料通常在這裡
if (!url.startsWith('blob:')) {
// 如果不是 blob URL,直接呼叫原始函式並返回
return originalRevokeObjectURL.apply(this, arguments);
}
try {
// 1. 【更精準】使用 CSS 選擇器定位圖片元素
const imgElement = document.querySelector(IMAGE_SELECTOR);
// 確保我們找到了圖片元素
if (imgElement) {
// 2. 【更聰明】從 blob URL 動態獲取檔案類型
const response = await fetch(url);
const blob = await response.blob();
const extension = mimeTypeToExtension[blob.type] || 'jpg'; // 如果找不到對應類型,預設為 jpg
// 3. 【更完善的檔名】組合作品標題和頁碼
const titleElement = document.querySelector(TITLE_SELECTOR);
const title = titleElement ? titleElement.textContent.trim() : 'Untitled';
const page = imgElement.getAttribute('alt') || '00';
// 組合並清理檔名,例如:"作品標題_p01.jpg"
const fileName = sanitizeFilename(`${title}_p${page}.${extension}`);
// 建立一個隱藏的 <a> 標籤來觸發下載
const link = document.createElement('a');
link.href = url;
link.download = fileName;
// 附加到 DOM、點擊、然後移除
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
} catch (error) {
// 4. 【更穩定】加入錯誤處理
console.error('DLsite Downloader Script Error:', error);
} finally {
// 5. 【最重要】無論成功或失敗,都必須呼叫原始函式,以釋放記憶體並確保頁面正常運作
originalRevokeObjectURL.apply(this, arguments);
}
};
console.log('DLsite Downloader script loaded and ready.');
})();