Greasy Fork is available in English.
auto fill douban book subject creation form
当前为
// ==UserScript==
// @name Douban Book Autofill
// @namespace https://gimo.me/
// @version 0.4.1
// @description auto fill douban book subject creation form
// @author Yuanji
// @match https://book.douban.com/new_subject
// @connect www.amazon.co.jp
// @grant GM_xmlhttpRequest
// ==/UserScript==
const submitBtn = document.querySelector('input[name="detail_subject_submit"]')
function createFillBtn() {
const isbn = document.getElementById('p_9').value
const amazonURL = 'https://www.amazon.co.jp/dp/' + isbn + '?showDetailProductDesc=1';
const fillBtn = document.createElement('input');
fillBtn.setAttribute('type', 'submit')
fillBtn.setAttribute('class', 'submit')
fillBtn.setAttribute('value', '自动填写')
fillBtn.onclick = (e) => { e.preventDefault(); getDoc(amazonURL, updateText)}
return fillBtn
}
function createCoverBtn(url) {
const coverBtn = document.createElement('input');
coverBtn.setAttribute('type', 'submit')
coverBtn.setAttribute('class', 'submit')
coverBtn.setAttribute('value', '下载封面')
coverBtn.onclick = (e) => { e.preventDefault(); window.open(url)}
return coverBtn
}
function getDoc(url, callback) {
GM_xmlhttpRequest({
method: 'GET',
url: url,
headers: {
'User-agent': window.navigator.userAgent,
},
onload: (responseDetail) => {
let doc = '';
if (responseDetail.status == 200) {
doc = (new DOMParser).parseFromString(responseDetail.responseText, 'text/html');
callback(doc)
}
}
});
}
function updateText(amazonDoc) {
const title = document.getElementById('p_2');
//const subTitle = document.getElementById('p_42');
const origTitle = document.getElementById('p_98');
const author = document.getElementById('p_5_0');
const price = document.getElementById('p_8');
const publisher = document.getElementById('p_6');
const publishYear = document.getElementById('p_7_selectYear');
const publishMonth = document.getElementById('p_7_selectMonth');
const publishDay = document.getElementById('p_7_selectDay');
const pageNum = document.getElementById('p_10');
const intro = document.querySelector('textarea[name="p_3_other"]');
const authorIntro = document.querySelector('textarea[name="p_40_other"]');
// 默认平装
document.getElementById('p_58_0').checked = true;
const amazonTitleText = amazonDoc.getElementById('productTitle').innerText;
const amazonInfo = amazonDoc.getElementById('detail_bullets_id');
const amazonIntro = amazonDoc.getElementById('productDescription');
const amazonCover = amazonDoc.querySelector("#imgThumbs > div:nth-child(1) > img").src.replace(/_.*_\./, '');
const getTextNodeContent = item => {
for (let n of item.childNodes) {
if (n.nodeType === Node.TEXT_NODE) {
console.log(n);
return n.textContent
}
}
}
for (let item of amazonInfo.getElementsByTagName('li')) {
const itemText = item.innerText
if (itemText.includes('ページ')) {
pageNum.value = parseInt(getTextNodeContent(item));
}
if (itemText.includes('出版社')) {
const [, publisherText, yearText, monthText, dayText] = getTextNodeContent(item).trim().match(/(.*)\((\d+)\/(\d+)\/(\d+)\)/);
publisher.value = publisherText;
publishYear.value = yearText;
publishMonth.value = monthText;
publishMonth.dispatchEvent(new Event('change'));
publishDay.value = dayText;
console.log(publisherText, yearText, monthText, dayText);
}
}
for (let el of amazonIntro.children) {
if (el.innerText.startsWith('内容')) {
intro.value += el.nextElementSibling.innerText.trim();
}
if (el.innerText.startsWith('著者')) {
authorIntro.value = el.nextElementSibling.innerText.trim();
}
}
title.value = amazonTitleText;
origTitle.value = amazonTitleText;
author.value = amazonDoc.getElementsByTagName('title')[0].innerText.split('|')[1].trim();
const coverBtn = createCoverBtn(amazonCover);
submitBtn.parentNode.insertBefore(coverBtn, submitBtn.parentNode.firstChild)
}
(function() {
'use strict';
const fillBtn = createFillBtn();
submitBtn.parentNode.insertBefore(fillBtn, submitBtn)
})();