Skip to content

Instantly share code, notes, and snippets.

@toraritte
Last active February 10, 2023 16:54
Show Gist options
  • Select an option

  • Save toraritte/e9d7a01dba1fd6a55c65b36274cd35cb to your computer and use it in GitHub Desktop.

Select an option

Save toraritte/e9d7a01dba1fd6a55c65b36274cd35cb to your computer and use it in GitHub Desktop.

Revisions

  1. toraritte revised this gist Feb 10, 2023. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion quick-parse-and-print-JS-bookmarklet.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,8 @@
    This one is for printing internal docs to PDF by simply clicking on a bookmark:
    ### 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(); })();
  2. toraritte revised this gist Feb 10, 2023. 1 changed file with 29 additions and 0 deletions.
    29 changes: 29 additions & 0 deletions quick-parse-and-print-JS-bookmarklet.md
    Original 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();
    ```
  3. toraritte created this gist Feb 10, 2023.
    5 changes: 5 additions & 0 deletions quick-parse-and-print-JS-bookmarklet.md
    Original 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(); })();
    ```