Pinjasaur
4/7/2017 - 12:20 AM

(Mass)drop.com product page copy to clipboard

Bookmarklet to copy a shareable URL of a (Mass)drop.com product page (appends "?mode=guest_open") to href so unregistered users can view the page.

Firefox doesn't allow execCommand in non-user contexts i.e. from a bookmarklet so a prompt is generated to manually copy from.

javascript:void(function(){"use strict";if(/(mass)?drop.com\/buy\/.+/.test(location.href)){var textarea=document.createElement("textarea");textarea.style.position="absolute";textarea.style.overflow="hidden";textarea.style.clip="rect(0 0 0 0)";textarea.style.height="1px";textarea.style.width="1px";textarea.style.margin="-1px";textarea.style.padding="0";textarea.style.border="0";textarea.value=(location.protocol+"//"+location.hostname+location.pathname+"?mode=guest_open");document.body.appendChild(textarea);if(/firefox/i.test(navigator.userAgent)){prompt("Copy to clipboard",textarea.value)}else{try{textarea.select();document.execCommand("copy")}catch(e){prompt("Copy to clipboard",textarea.value)}}document.body.removeChild(textarea)}})();
javascript:void(function() {
  "use strict";

  // Check for valid URL
  if (/(mass)?drop.com\/buy\/.+/.test(location.href)) {
    var textarea = document.createElement("textarea");

    // Hide the textarea
    // Can't be 'display: none' since `execCommand` needs to be
    // "user initiated" (e.g. cannot interact with 'display: none')
    textarea.style.position = "absolute";
    textarea.style.overflow = "hidden";
    textarea.style.clip     = "rect(0 0 0 0)";
    textarea.style.height   = "1px";
    textarea.style.width    = "1px";
    textarea.style.margin   = "-1px";
    textarea.style.padding  = "0";
    textarea.style.border   = "0";

    textarea.value = (location.protocol + "//" + location.hostname + location.pathname + "?mode=guest_open");
    
    document.body.appendChild(textarea);
    
    // Handle Firefox by displaying a `prompt`
    if (/firefox/i.test(navigator.userAgent)) {
      prompt("Copy to clipboard", textarea.value);
    } else {
      try {
        textarea.select();
        document.execCommand("copy");
      } catch (e) {
        prompt("Copy to clipboard", textarea.value);
      }
    }
    
    document.body.removeChild(textarea);
  }
})();