Last active
February 10, 2023 16:54
-
-
Save toraritte/e9d7a01dba1fd6a55c65b36274cd35cb to your computer and use it in GitHub Desktop.
Revisions
-
toraritte revised this gist
Feb 10, 2023 . 1 changed file with 5 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,8 @@ ### What is a bookmarklet? > a small software application stored as a bookmark in a web browser, which typically allows a user to interact with the currently loaded web page in some way. That is, when the URL field of a bookmark contains JavaScript. This one is for printing internal docs to PDF by simply clicking on a bookmark: ```javascript javascript: (() => { let auth_num = document.body.outerHTML.match(/NMED\d{9}/g); let datetime = new Date().toJSON().replace(/[^a-zA-Z0-9]/g, '_'); document.title = `${datetime}_${auth_num}`; window.print(); window.close(); })(); -
toraritte revised this gist
Feb 10, 2023 . 1 changed file with 29 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,4 +2,33 @@ This one is for printing internal docs to PDF by simply clicking on a bookmark: ```javascript javascript: (() => { let auth_num = document.body.outerHTML.match(/NMED\d{9}/g); let datetime = new Date().toJSON().replace(/[^a-zA-Z0-9]/g, '_'); document.title = `${datetime}_${auth_num}`; window.print(); window.close(); })(); ``` ### Breaking it down #### 1. The bookmarklet JavaScript template ```javascript javascript: (() => { /* add arbitrary code here */ })(); ``` #### 2. The "inner" JavaScript ```javascript // These parts are irrelevant as they // are specific to internal HTML documents. let auth_num = document.body.outerHTML.match(/NMED\d{9}/g); let datetime = new Date().toJSON().replace(/[^a-zA-Z0-9]/g, '_'); // Change title of the HTML document as this // will be the default name of the printed PDF. document.title = `${datetime}_${auth_num}`; // Open print dialog (same and CTRL+P). window.print(); // (Optional) Close the browser tab. // Only works if that has been opened programmatically! // (Such as using Vimium plugin in Chrome.) window.close(); ``` -
toraritte created this gist
Feb 10, 2023 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,5 @@ This one is for printing internal docs to PDF by simply clicking on a bookmark: ```javascript javascript: (() => { let auth_num = document.body.outerHTML.match(/NMED\d{9}/g); let datetime = new Date().toJSON().replace(/[^a-zA-Z0-9]/g, '_'); document.title = `${datetime}_${auth_num}`; window.print(); window.close(); })(); ```