// ==UserScript== // @name 从Discogs添加豆瓣条目 // @license MIT // @namespace http://tampermonkey.net/ // @version 1.0000000006 // @license MIT // @run-at document-end // @description // @author 越洋飞机 // @match *://www.discogs.com/release* // @match *://www.discogs.com/master* // @match *://music.douban.com/new_subject* // @icon https://s.discogs.com/badaa268132c5df6360d067acd267fbebd755915/images/discogs-white.png?5 // @grant GM_xmlhttpRequest // @grant GM_download // @grant GM_openInTab // @grant GM_addElement // @grant GM_registerMenuCommand // @description 添加豆瓣条目,自动填写信息,使用https://api.discogs.com // @downloadURL none // ==/UserScript== (function () { 'use strict'; async function GetRequest(url = '') { const response = await fetch(url); return response.json(); // parses JSON response into native JavaScript objects } var doubanURL = "https://music.douban.com/new_subject"; var discogsURL = window.location.href; console.log(discogsURL); function NewSubject() { let masterURL = "https://www.discogs.com/master/" let releaseURL = "https://www.discogs.com/release/" let apiURL = "https://api.discogs.com/" if (discogsURL.startsWith(masterURL)) { let pos = discogsURL.indexOf("-"); let masterID = discogsURL.substring(31, pos); let httpRequest = new XMLHttpRequest(); GetRequest(apiURL.concat("masters/", masterID)) .then(data => { console.log(data); let releaseID = data.main_release; addItem(releaseID); }); } else if (discogsURL.startsWith(releaseURL)) { let pos = discogsURL.indexOf("-") let releaseID = discogsURL.substring(32, pos) addItem(releaseID) } } /** function GetRequest(url,storageName) { var httpRequest = new XMLHttpRequest(); var json; httpRequest.open('GET', url, true); httpRequest.send(); httpRequest.onreadystatechange = function () { if (httpRequest.readyState == 4 && httpRequest.status == 200) { json = JSON.parse(httpRequest.responseText); localStorage.setItem(storageName,httpRequest.responseText); } }; } **/ function addItem(releaseID) { console.log(releaseID) GetRequest('https://api.discogs.com/releases/' + releaseID + '?token=tgRatMaOmFfXjBwHNBlZDQtXrOAELZwpywEOCEbb') .then(data => { console.log(data); var info = new Object(); info.artists = [...data.artists]; info.genres = [...data.genres]; info.numArtists = info.artists.length; info.label = data.labels[0].name; info.title = data.title; info.format = data.formats[0].name; if (data.styles) { info.styles = [...data.styles]; } else { info.styles = "Unknown"; } if (data.released) { info.release = data.released; } else { info.release = "Unknown"; } if (data.notes) { info.note = data.notes; } else { info.note = "None"; } if (data.country) { info.country = data.country; } else { info.country = "Unknown"; } info.link = discogsURL; info.tracklist = '' for (let index = 0; index < data.tracklist.length; index++) { if (data.tracklist[index].type_ == 'heading') { info.tracklist += data.tracklist[index].title + '\n'; } if (data.tracklist[index].type_ == 'track') { info.tracklist += data.tracklist[index].position + '. ' + data.tracklist[index].title + ' ' + data.tracklist[index].duration + '\n'; } } if (data.formats[0].descriptions) { if (data.formats[0].descriptions.indexOf("Album") > -1) { info.type = "专辑" } else if (data.formats[0].descriptions.indexOf("Single") > -1) { info.type = "单曲"; } else if (data.formats[0].descriptions.indexOf("Compilation") > -1) { info.type = "选集"; } else if (data.formats[0].descriptions.indexOf("EP") > -1) { info.type = "EP"; } else if (data.formats[0].descriptions.indexOf("LP") > -1 || data.formats[0].descriptions.indexOf("Stereo") > -1 || data.formats[0].descriptions.indexOf("12\"") > -1 || data.formats[0].descriptions.indexOf("33 ⅓ RPM") > -1) { info.type = "专辑"; } else if (data.formats[0].descriptions.indexOf("Mini-Album") > -1) {// whatever info.type = "EP"; } else if (data.formats[0].descriptions.indexOf("7\"") > -1 || data.formats[0].descriptions.indexOf("45 RPM") > -1) { info.type = "单曲"; } } console.log(info); if (data.images) { GM_download(data.images[0].uri, info.title + '.jpg'); } let doubanWindow = window.open(doubanURL, JSON.stringify(info)); }); }; GM_registerMenuCommand("添加条目", NewSubject, ''); } )();