BOSS - bookmark.os saver script

Adds a shortcut to bookmark the current page with bookmark.os <https://bookmarkos.com/help/basics>. CTRL + ALT + ']' opens a pop-up to bookmark the current page, CTRL + ALT + '[' shows all bookmarks in a new tab.

目前為 2023-12-16 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         BOSS - bookmark.os saver script
// @namespace    <https://github.com/lundeen-bryan>
// @version      1.0.0
// @description  Adds a shortcut to bookmark the current page with bookmark.os <https://bookmarkos.com/help/basics>. CTRL + ALT + ']' opens a pop-up to bookmark the current page, CTRL + ALT + '[' shows all bookmarks in a new tab.
// @author       lundeen-bryan
// @match        *://*/*
// @icon         https://bookmarkos.com//favicon-32x32.png
// @license      GPL
// @run-at       'document-end'
// @noframes     true
// ==/UserScript==

/**
 * This function extracts meta information from the current webpage and opens a new window
 * with a URL that includes this meta information as parameters for bookmarking purposes.
 */
function bookmarkPage() {
  // Get all meta elements on the current page
  var metas = document.getElementsByTagName("meta");
  var metaInfo = {};
  var metaDescription = null;
  var ogDescription = null;
  var twitterDescription = null;
  var ogType = null;
  var metaKeywords = null;

  // Loop through the meta elements to find specific meta information
  for (var i = 0; i < metas.length; i++) {
    // Get the current meta element in the loop
    var meta = metas[i];

    // Check if the 'name' attribute of the meta element is 'description'
    if (meta.getAttribute("name") == "description") {
      // If it is, assign the value of the 'content' attribute to 'metaDescription'
      metaDescription = metas[i].getAttribute("content");
    }
    // Check if the 'property' attribute of the meta element is 'og:description'
    else if (meta.getAttribute("property") == "og:description") {
      // If it is, assign the value of the 'content' attribute to 'ogDescription'
      ogDescription = metas[i].getAttribute("content");
    }
    // Check if the 'name' attribute of the meta element is 'twitter:description'
    else if (meta.getAttribute("name") == "twitter:description") {
      // If it is, assign the value of the 'content' attribute to 'twitterDescription'
      twitterDescription = metas[i].getAttribute("content");
    }
    // Check if the 'property' attribute of the meta element is 'og:type'
    else if (meta.getAttribute("property") == "og:type") {
      // If it is, assign the value of the 'content' attribute to 'ogType'
      ogType = metas[i].getAttribute("content");
    }
    // Check if the 'name' attribute of the meta element is 'keywords'
    else if (meta.getAttribute("name") == "keywords") {
      // If it is, assign the value of the 'content' attribute to 'metaKeywords'
      metaKeywords = metas[i].getAttribute("content");
    }
  }

  // Store the extracted meta information in an object
  metaInfo["meta_description"] =
    metaDescription || ogDescription || twitterDescription;
  metaInfo["meta_keywords"] = metaKeywords;
  metaInfo["og_type"] = ogType;

  // Calculate the window position for the new window, center the modal window on active screen
  var dualScreenLeft =
    window.screenLeft != undefined ? window.screenLeft : screen.left;
  var dualScreenTop =
    window.screenTop != undefined ? window.screenTop : screen.top;
  width = window.innerWidth
    ? window.innerWidth
    : document.documentElement.clientWidth
    ? document.documentElement.clientWidth
    : screen.width;
  height = window.innerHeight
    ? window.innerHeight
    : document.documentElement.clientHeight
    ? document.documentElement.clientHeight
    : screen.height;

  var left = width / 2 - 750 / 2 + dualScreenLeft;
  var top = height / 2 - 450 / 2 + dualScreenTop;

  // Open a new window with the bookmarking URL and meta information as parameters
  window.open(
    "https://bookmarkos.com/save?url=" +
      encodeURIComponent(location.href) +
      "&title=" +
      encodeURIComponent(document.title) +
      "&meta_description=" +
      encodeURIComponent(metaInfo["meta_description"]) +
      "&meta_keywords=" +
      encodeURIComponent(metaInfo["meta_keywords"]) +
      "&og_type=" +
      encodeURIComponent(metaInfo["og_type"]),
    "bookmark",
    "left=" +
      left +
      ",top=100,width=750,height=450,toolbar=0,location=0,resizable=0"
  );
}

function gotoBm() {
  // Shows bookmarks sorted by time, insted of the senseless sort by title default
  window.open("https://bookmarkos.com/access_extension");
}

function keyPressEvent(event) {
  var kcode = event.keyCode ? event.keyCode : event.which;
  if (event.ctrlKey && event.altKey) {
    if (kcode == 221) bookmarkPage(); // ']'
    if (kcode == 219) gotoBm(); // '['
  }
}

// Call the bookmarkPage function to initiate the bookmarking process
// bookmarkPage();