Search code examples
javascriptbookmarklet

Bookmarklet That Changes the Background to Your URL


I have been trying to make a bookmarklet that changes the background to the URL you provide the the a prompt.

javascript:var newImage = prompt("URL Image", "url('https://i.ytimg.com/vi/Lr5E2KBfEGE/maxresdefault.jpg')");var wae=document.body;wae.style.backgroundImage=newImage,wae.style.backgroundRepeat="no-repeat",wae.style.backgroundAttachment="fixed";void 0;

So far I have been successful, but the prompt will always have "url('https://i.ytimg.com/vi/Lr5E2KBfEGE/maxresdefault.jpg')"
Is it possible to make it so all you have to enter in the prompt is just the URL?


Solution

  • Simply don't pass the second parameter in the prompt field, and append the return of the prompt to the backgroundImage.

    var newImage = prompt("URL Image");
    var wae = document.body;
    wae.style.backgroundImage = "url(" + newImage + ")", wae.style.backgroundRepeat = "no-repeat", wae.style.backgroundAttachment = "fixed";
    void 0;