// ==UserScript== // @name PTH iTunes Cover Search // @version 1.5 // @description Search iTunes for cover art // @author Chameleon // @include http*://passtheheadphones.me/* // @grant GM_xmlhttpRequest // @namespace https://greasyfork.org/users/87476 // @downloadURL none // ==/UserScript== (function() { 'use strict'; if(window.location.href.indexOf('upload.php') != -1) showUpload(); if(window.location.href.indexOf('torrents.php?action=editgroup&groupid=') != -1) showEdit(); if(window.location.href.indexOf('torrents.php?id=') != -1) saveArtist(); })(); function saveArtist() { var artist=document.getElementsByTagName('h2')[0].textContent.split(' - ')[0]; window.localStorage.lastArtist=artist; } function showEdit() { var before=document.getElementsByClassName('edit_form')[0].getElementsByTagName('br')[0]; var image=document.getElementsByName('image')[0]; var artist=window.localStorage.lastArtist; if(!artist) artist=''; var album=document.getElementsByTagName('h2')[0].getElementsByTagName('a')[0].innerHTML; var div=document.createElement('div'); before.parentNode.insertBefore(div, before); var messageDiv=document.createElement('div'); if(image.parentNode.innerHTML.indexOf('Auto-rehost') == -1) { var a=document.createElement('a'); div.parentNode.insertBefore(a, div); div.parentNode.insertBefore(document.createTextNode(' '), a); a.href='javascript:void(0);'; a.innerHTML = 'Auto-rehost: Off'; a.addEventListener('click', toggleAutoRehost.bind(undefined, a, image, messageDiv), false); image.addEventListener('keyup', rehost.bind(undefined, image, messageDiv), false); if(window.localStorage.autoUpload == "true") { image.setAttribute('autorehost', 'true'); a.innerHTML = 'Auto-rehost: On'; } } var optionsDiv=document.createElement('div'); optionsDiv.style.display='none'; var search=document.createElement('input'); optionsDiv.appendChild(search); search.setAttribute('placeholder', 'search'); search.value=(artist.value+' '+album.value).trim(); var country=document.createElement('input'); optionsDiv.appendChild(country); country.setAttribute('placeholder', 'country code'); country.value='US'; var toggle=document.createElement('a'); toggle.href='javascript:void(0);'; toggle.innerHTML = 'Show search'; toggle.addEventListener('click', toggleDiv.bind(undefined, toggle, optionsDiv), false); var a=document.createElement('a'); div.appendChild(a); div.appendChild(document.createTextNode(' | ')); div.appendChild(toggle); div.appendChild(optionsDiv); div.appendChild(messageDiv); a.innerHTML='Get image from iTunes'; a.href='javascript:void(0);'; a.addEventListener('click', getAlbum.bind(undefined, search, country, image, div, messageDiv), false); } function toggleDiv(a, div) { if(a.innerHTML.indexOf('Show') != -1) { a.innerHTML = a.innerHTML.replace('Show', 'Hide'); div.style.display=''; } else { a.innerHTML = a.innerHTML.replace('Hide', 'Show'); div.style.display='none'; } } function showUpload() { var artist=document.getElementById('artist'); var album=document.getElementById('title'); var image=document.getElementById('image'); var imageTd=image.parentNode; var messageDiv=document.createElement('div'); imageTd.appendChild(messageDiv); var div=imageTd; var optionsDiv=document.createElement('div'); optionsDiv.style.display='none'; var search=document.createElement('input'); optionsDiv.appendChild(search); search.setAttribute('placeholder', 'search'); search.value=(artist.value+' '+album.value).trim(); var country=document.createElement('input'); optionsDiv.appendChild(country); country.setAttribute('placeholder', 'country code'); country.value='US'; var toggle=document.createElement('a'); toggle.href='javascript:void(0);'; toggle.innerHTML = 'Show search'; toggle.addEventListener('click', toggleDiv.bind(undefined, toggle, optionsDiv), false); var a=document.createElement('a'); div.appendChild(a); div.appendChild(document.createTextNode(' | ')); div.appendChild(toggle); div.appendChild(optionsDiv); a.innerHTML='Get image from iTunes'; a.href='javascript:void(0);'; a.addEventListener('click', getAlbum.bind(undefined, country, search, image, imageTd, messageDiv), false); var imageDiv=document.createElement('div'); imageDiv.setAttribute('id', 'iTunesImageDiv'); imageDiv.setAttribute('style', 'text-align: center;'); div.appendChild(imageDiv); } function getAlbum(country, search, im, td, messageDiv) { //console.log(im); /*var xhr = new XMLHttpRequest(); xhr.open('GET', "https://itunes.apple.com/search?"+encodeURIComponent(artist+' '+album)); xhr.onreadystatechange = xhr_func.bind(undefined, messageDiv, xhr, gotAlbum.bind(undefined, im, td, messageDiv), getAlbum.bind(undefined, a, al, im, td, messageDiv)); xhr.send();*/ //console.log("https://itunes.apple.com/search?term="+encodeURIComponent(artist+' '+album)); messageDiv.innerHTML = 'Searching for image on iTunes'; var s="https://itunes.apple.com/search?term="+encodeURIComponent(search.value); if(country.value.length > 0 && country.value != 'US') { s+="&country="+encodeURIComponent(country.value); } GM_xmlhttpRequest({ method: "GET", url: s, onload: function(response) { if(response.status == 200) {gotAlbum(im, td, messageDiv, response.responseText); } else { messageDiv.innerHTML = 'iTunes error: '+response.status; } } }); } function gotAlbum(input, td, messageDiv, response) { var r=JSON.parse(response); if(r.results.length > 0) { var div=document.getElementById('iTunesImageDiv'); div.innerHTML='Current: 1 | '; if(r.results.length > 1) { div.setAttribute('results', JSON.stringify(r.results)); div.setAttribute('index', '0'); var a=document.createElement('a'); a.innerHTML='Next'; a.href='javascript:void(0);'; a.addEventListener('click', changeImage.bind(undefined, div, 1, input), false); div.appendChild(a); div.appendChild(document.createElement('br')); } var a=document.createElement('a'); a.href='javascript:void(0);'; div.appendChild(a); var img=document.createElement('img'); a.setAttribute('imageSize', 'large'); a.addEventListener('click', changeSize.bind(undefined, a, img, input, r.results[0].artworkUrl60), false); a.appendChild(img); img.src=r.results[0].artworkUrl60.replace("60x60bb", "10000x10000-999"); input.value = img.src; if(window.localStorage.iTunesSize == 'small') a.click(); else { var evt = document.createEvent("HTMLEvents"); evt.initEvent("keyup", false, true); input.dispatchEvent(evt); } } else messageDiv.innerHTML = "no results"; } function changeImage(div, amount, input) { var r=JSON.parse(div.getAttribute('results')); var index=parseInt(div.getAttribute('index')); index+=amount; if(index < 0) index=0; if(index >= r.length) index=r.length-1; div.setAttribute('index', index); div.innerHTML='Current: '+(index+1)+' | '; if(index != r.length-1) { //div.setAttribute('results', JSON.stringify(r)); div.setAttribute('index', index); var a=document.createElement('a'); a.innerHTML='Next'; a.href='javascript:void(0);'; a.addEventListener('click', changeImage.bind(undefined, div, 1, input), false); div.appendChild(a); if(index !== 0) div.appendChild(document.createTextNode(' | ')); } if(index !== 0) { //div.setAttribute('results', JSON.stringify(r)); div.setAttribute('index', index); var a=document.createElement('a'); a.innerHTML='Previous'; a.href='javascript:void(0);'; a.addEventListener('click', changeImage.bind(undefined, div, -1, input), false); div.appendChild(a); } div.appendChild(document.createTextNode(' | ')); var a=document.createElement('a'); div.appendChild(a); a.href='javascript:void(0);'; a.innerHTML = 'Copy to input'; var img=document.createElement('img'); a.addEventListener('click', triggerKeyup.bind(undefined, input, img), false); div.appendChild(document.createElement('br')); var a=document.createElement('a'); a.href='javascript:void(0);'; div.appendChild(a); a.setAttribute('imageSize', 'large'); a.addEventListener('click', changeSize.bind(undefined, a, img, input, r[index].artworkUrl60), false); a.appendChild(img); var artwork=r[index].artworkUrl60.replace("60x60bb", "10000x10000-999"); if(window.localStorage.iTunesSize == 'small') artwork=r[index].artworkUrl60.replace("60x60bb", "600x600bb"); img.src=artwork; //input.value = img.src; } function triggerKeyup(input, img) { input.value=img.src; var evt = document.createEvent("HTMLEvents"); evt.initEvent("keyup", false, true); input.dispatchEvent(evt); } function changeSize(a, img, input, url) { if(a.getAttribute('imageSize') == 'large') { window.localStorage.iTunesSize='small'; a.setAttribute('imageSize', 'small'); url=url.replace("60x60bb", "600x600bb"); } else { window.localStorage.iTunesSize='large'; a.setAttribute('imageSize', 'large'); url=url.replace("60x60bb", "10000x10000-999"); } input.value=url; img.src=url; var evt = document.createEvent("HTMLEvents"); evt.initEvent("keyup", false, true); input.dispatchEvent(evt); } function rehost(imageInput, span) { if(imageInput.getAttribute('autorehost') != "true") return; var whitelisted = ["imgur.com", "ptpimg.me"]; if(imageInput.value.length < 1) return; for(var i=0; iretrying in 1 second'; window.setTimeout(repeatFunc, 1000); } } }