Advertisement
jcunews

Bookmarklets Menu bookmarklet

Dec 7th, 2022
3,215
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. javascript:((config, menuId, wrapper, popup) => {
  2.  
  3.   /* Bookmarklets Menu
  4.      Version: 1.0.1
  5.      License: AGPL v2
  6.      Author : jcunews
  7.      Sites  : https://www.reddit.com/user/jcunews1
  8.               https://greasyfork.org/en/users/85671-jcunews
  9.               https://pastebin.com/u/jcunews
  10.               https://gist.github.com/jcunews
  11.  
  12.      Example of a bookmarklet menu containing other bookmarklets. Use it as a code template.
  13.      Note:
  14.        Clicking on the darkened area outside of the menu, or running the bokomarklet while the menu is being shown, will close the menu.
  15.        A bookmarklet code should not contain any line based comment as the code will be collapsed into a single line since it's an URL.
  16.        The length of the bookmarklet's URL should not be greater than 65535 UTF-16 characters.
  17.        A release and proven stable code should omit any comment and compact/compress/uglify the code, to minimize the URL length.
  18.        It's recommended to keep development and release code separate.
  19.   */
  20.  
  21.   /* bookmarklet menu code */
  22.  
  23.   if (navigator.userAgent.includes("Firefox/") && document.contentType.endsWith("/xml")) return alert("Bookmarklet menu is disabled on XML content in Firefox.");
  24.   if (wrapper = window[menuId = config.menuId]) return wrapper.remove();
  25.   document.documentElement.insertAdjacentHTML("beforeend", `
  26. <div id="${menuId}">
  27.   <style>
  28.     #${menuId},#${menuId} *{all:revert;margin:0;box-sizing:border-box}
  29.     #${menuId}{position:fixed;left:0;top:0;width:100vw;height:100vh;background:#0005;line-height:1.5em;font-family:sans-serif;cursor:pointer}
  30.     #${menuId} .bookmarkletMenu{position:fixed;left:50%;top:50%;transform:translate(-50%, -50%);border:1px solid #555;background:#eee;cursor:default}
  31.     #${menuId} .bookmarkletMenu>*{display:block;padding:0 .3em}
  32.     #${menuId} h4{background:#777;color:#fff}
  33.     #${menuId} .separator{border-top:1px solid #555}
  34.     #${menuId} a:hover{background:#007;color:#fff}
  35.   </style>
  36.   <div class="bookmarkletMenu"></div>
  37. </div>`
  38.   );
  39.   (wrapper = window[menuId]).onclick = ev => ev.target.id && wrapper.remove();
  40.   popup = wrapper.lastElementChild;
  41.   if (config.menuTitle) popup.appendChild(document.createElement("H4")).textContent = config.menuTitle;
  42.   config.menuItems.forEach((item, ele) => {
  43.     popup.appendChild(ele = document.createElement("A")).textContent = item.title;
  44.     if (item.separatorBefore) ele.classList.add("separator");
  45.     ele.onclick = () => (wrapper.remove(), item.code(), false)
  46.   });
  47.   document.documentElement.focus()
  48.  
  49. })(
  50.  
  51.   /* bookmarklet menu configuration */
  52.   {
  53.  
  54.     /* menu unique ID. to prevent same menu from appearing twice.
  55.        must not conflict with a name of anything, and must not contain any white-space or below characters:
  56.          ~ @ # * + [ ] { } : ; \ , .
  57.     */
  58.     menuId: "myBookmarklets-123987",
  59.  
  60.     /* menu title. use empty string to disable */
  61.     menuTitle: "My Bookmarklets",
  62.  
  63.     /* one or more bookmarklet menu item objects which contain title, and code */
  64.     menuItems: [
  65.  
  66.       {
  67.         title: "Bookmarklet #1",
  68.         code: () => {
  69.           alert("this is bookmarklet #1")
  70.         }
  71.       },
  72.  
  73.       {
  74.         title: "Bookmarklet #2",
  75.         code: () => {
  76.           alert("this is bookmarklet #2")
  77.         }
  78.       },
  79.  
  80.       {
  81.         title: "Bookmarklet #3: Go to Bing",
  82.         code: () => {
  83.           location.href = "https://www.bing.com/"
  84.         },
  85.         separatorBefore: true /* optional setting to display horizontal menu separator before this menu item */
  86.       }
  87.  
  88.       /* and so on... */
  89.  
  90.     ]
  91.  
  92.  }
  93.  
  94. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement