Created
September 25, 2017 12:22
-
-
Save a4lg/8e7bea9e218e8e29a8bb3414ccd9fc3b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name !NAJ: Download Helper | |
// @namespace http://a4lg.com/ | |
// @description Adds download control buttons | |
// @include https://www.digital.archives.go.jp/DAS/meta/listPhoto* | |
// @version 1 | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
var dtype = document.getElementById("download_type"); | |
if (!dtype) | |
return; | |
dtype = dtype.value; | |
var nvols = (function() { | |
var i = 1; | |
while (true) { | |
if (!document.getElementById("id_" + i)) | |
return i - 1; | |
++i; | |
} | |
return null; | |
})(); | |
var pages = []; | |
var totalPages = 0; | |
for (var i = 1; i <= nvols; i++) { | |
pages.push(parseInt(document.getElementById("max_page_" + i).value)); | |
totalPages += pages[i - 1]; | |
} | |
if (!maxDownloadCnt) { | |
console.log("warning: maxDownloadCnt variable not found. falling back..."); | |
maxDownloadCnt = 500; | |
} | |
var downloadSelectionControl = document.createElement("select"); | |
downloadSelectionControl.addEventListener("change", function(e) { | |
var range = e.target.value; | |
range = range.split(","); | |
var range0 = parseInt(range[0]); | |
var range1 = parseInt(range[1]); | |
var nvols = parseInt(range[2]); | |
for (var i = 1; i <= nvols; i++) { | |
document.getElementById("id_" + i.toString()).checked = | |
(i >= range0 && i <= range1); | |
} | |
}); | |
// 自動ダウンロード用のコントロールを設置 | |
{ | |
var controlBox = document.createElement("table"); | |
controlBox.classList.add("simple-table"); | |
document.body.insertBefore(controlBox, document.body.childNodes[0]); | |
var elem0, elem1; | |
elem0 = document.createElement("tbody"); | |
controlBox.appendChild(elem0); | |
elem1 = document.createElement("tr"); | |
elem0.appendChild(elem1); | |
elem0 = document.createElement("th"); | |
elem0.style.width = "250px"; | |
elem0.textContent = "自動ダウンロードインデックス"; | |
elem1.appendChild(elem0); | |
elem0 = document.createElement("td"); | |
elem1.appendChild(elem0); | |
elem0.appendChild(downloadSelectionControl); | |
} | |
var addOption = function(downloadSelectionControl, range0, range1, nvols) { | |
var labelFromId = function(n) { | |
var l = document.getElementsByTagName("label"); | |
for (var i = 0; i < l.length; i++) { | |
if (l[i].getAttribute("for") === "id_" + n.toString()) | |
return l[i].textContent; | |
} | |
return "(不明)"; | |
}; | |
var elem = document.createElement("option"); | |
elem.value = range0.toString() + "," + range1.toString() + "," + nvols.toString(); | |
downloadSelectionControl.appendChild(elem); | |
var label0 = "表紙"; | |
if (range0 > 1) | |
label0 = labelFromId(range0); | |
var label1 = label0; | |
if (range1 > 1) | |
label1 = labelFromId(range1); | |
if (range0 == 1 && range1 == nvols) | |
elem.textContent = "全部"; | |
else if (label0 === label1) | |
elem.textContent = label0; | |
else | |
elem.textContent = label0 + " - " + label1; | |
elem.textContent = document.getElementById("id_" + range0.toString()).value + " : " + elem.textContent; | |
}; | |
var constructed = false; | |
if (totalPages < maxDownloadCnt) { | |
addOption(downloadSelectionControl, 1, nvols, nvols); | |
constructed = true; | |
} | |
var isInRange = false; | |
var range0 = 1; | |
var tmpPages = 0; | |
for (var i = 0; i < nvols; i++) { | |
if (pages[i] > maxDownloadCnt) { | |
if (isInRange) { | |
addOption(downloadSelectionControl, range0, i, nvols); | |
isInRange = false; | |
} | |
continue; | |
} | |
if (!isInRange) { | |
isInRange = true; | |
range0 = i + 1; | |
} | |
if (tmpPages + pages[i] > maxDownloadCnt) { | |
addOption(downloadSelectionControl, range0, i, nvols); | |
tmpPages = 0; | |
range0 = i + 1; | |
} | |
tmpPages += pages[i]; | |
} | |
if (isInRange) | |
addOption(downloadSelectionControl, range0, nvols, nvols); | |
downloadSelectionControl.selectedIndex = 0; | |
downloadSelectionControl.dispatchEvent(new Event("change")); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment