Search code examples
javascriptbookmarklet

Show bookmarklet output in new tab


I have a very easy problem (I guess) but it keeps me busy for quite a time. When running this small snippet in my bookmarklet:

javascript:document.getElementById("detailPanel-body").innerHTML;

The current page will be changed to the output of the bookmarklet. However, what I want, is that the bookmarklet opens a new tab with the output of the javascript code.


Solution

  • If you use browsers where navigating to Data URI is not blocked (like Firefox), you can try this:

    javascript: {
      void window.open(`data:text/plain;charset=UTF-8,${
        encodeURIComponent(document.getElementById('detailPanel-body').innerHTML)
      }`);
    }
    

    If you use browsers where navigating to Data URI is blocked (like Chrome), you can try this:

    javascript: {
      void (window.open().document.body.textContent =
        document.getElementById('detailPanel-body').innerHTML);
    }