Search code examples
javascripthtmldombrowserbookmarklet

Bookmarklet to briefly flash a message


Trying to develop a Bookmarklet to copy contents of a specific field on webpage AND then briefly flash a confirmation message. Already have both parts working separately. Can't figure out how to combine them to be able to then put that code into URL field of Bookmarklet.

javascript: (function(){var copyText = document.getElementById("mergeFields-input-text");copyText.select();document.execCommand("Copy"); 
function tempAlert(msg,duration)
{
     var el = document.createElement("div");
     el.setAttribute("style","position:absolute;top:5%;left:20%;background-color:white;");
     el.innerHTML = msg;
     setTimeout(function(){
      el.parentNode.removeChild(el);
     },duration);
     document.body.appendChild(el);
}

var d = document.getElementById('mergeFields-input-text');
d.onclick = function(){ tempAlert("Copied",5000); })();


Solution

  • No need to add the 'onclick' event. Try this bookmarklet:

    javascript:(function() {
        var copyText = document.getElementById("mergeFields-input-text");
        copyText.select();
        document.execCommand("Copy");
    
        tempAlert("Copied", 5000);
        function tempAlert(msg, duration) {
            var el = document.createElement("div");
            el.setAttribute("style","position:absolute;top:5%;left:20%;background-color:white;");
            el.innerHTML = msg;
            document.body.appendChild(el);
    
            setTimeout(function(){
                    el.parentNode.removeChild(el);
                }, duration
            );
        }
    })();