// ==UserScript== // @name PTH iTunes Cover Search // @version 0.9 // @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=before.previousElementSibling; 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'); 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 a=document.createElement('a'); div.appendChild(a); div.appendChild(messageDiv); a.innerHTML='Get image from iTunes'; a.href='javascript:void(0);'; a.addEventListener('click', getAlbum.bind(undefined, artist, album, image, div, messageDiv), false); } 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 a=document.createElement('a'); imageTd.appendChild(a); a.innerHTML='Get image from iTunes'; a.href='javascript:void(0);'; a.addEventListener('click', getUploadAlbum.bind(undefined, artist, album, image, imageTd, messageDiv), false); } function getUploadAlbum(a, al, im, td, messageDiv) { getAlbum(a.value, al.value, im, td, messageDiv); } function getAlbum(artist, album, im, td, messageDiv) { /*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'; GM_xmlhttpRequest({ method: "GET", url: "https://itunes.apple.com/search?term="+encodeURIComponent(artist+' '+album), 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 img=document.createElement('img'); td.appendChild(img); img.src=r.results[0].artworkUrl60.replace("60x60bb", "10000x10000-999"); input.value = img.src; var evt = document.createEvent("HTMLEvents"); evt.initEvent("keyup", false, true); input.dispatchEvent(evt); } else messageDiv.innerHTML = "no results"; } 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); } } }