jump-to-anchor

Context menu item to jump to the closest anchor to the selected text (if any) or to the right-click point otherwise.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name jump-to-anchor
// @version 0.2
// @description Context menu item to jump to the closest anchor to the selected text (if any) or to the right-click point otherwise.
// @license MIT
// @author eight04 <[email protected]>, YFdyh000
// @incompatible chrome
// @incompatible opera
// @incompatible safari
// @include *
// @require https://greasyfork.org/scripts/33034-gm-context/code/GM_context.js?version=219427
// @grant none
// @namespace https://greasyfork.org/users/3017-yfdyh000
// ==/UserScript==

/* global GM_context */

(function () {
  const all_context = ["page", "selection", "editable", "image", "link"];
  const menuitem = {
    label: "Jump to anchor (from click location)",
    onclick: function() { jumpToAnchor() }
  };
  const menuitem_selected = {
    label: "Jump to anchor (from highlighted selection)",
    onclick: function () { jumpToAnchor() } // TODO: keep the selection
  };

  GM_context.add({
    context: all_context,
    items: [menuitem]
  });
  GM_context.add({
    context: all_context,
    items: [menuitem_selected],
    oncontext(e) {
      if (!getSelection()) return false;
    }
  });

  function getSelection() {
    return document.getSelection().toString();
  }
  var x, y;
  window.addEventListener('click', function (e) {
    // Avoid grabbing for the actual selection
    // Doesn't seem to execute on single click anyways
    //  but add for good measure
    if (e.button === 2) {
      x = e.clientX;
      y = e.clientY;
    }
  }, true);
  function jumpToAnchor() {
    x = Math.max(0, Math.min(window.innerWidth, x));
    y = Math.max(0, Math.min(window.innerHeight, y));

    var node;
    var hasSelection = getSelection();
    if (hasSelection) {
      // For some reason, we can't just check ourselves here for
      //  getSelection().anchorNode, as it is always present
      node = document.getSelection().anchorNode;
    }
    else {
      var caretPosition = document.caretPositionFromPoint(x, y);
      node = caretPosition.offsetNode;
    }

    function findDeepestLastChild(elem) {
      var oldElem;
      do {
        oldElem = elem;
        elem = elem.lastElementChild;
      } while (elem);
      return oldElem;
    }
    function foundAnchor(nde) {
      if (nde.id || (nde.name && nde.nodeName.toLowerCase() === 'a')) {
        location.hash = '#' + (nde.id || nde.name);
        return true;
      }
    }

    do {
      if (foundAnchor(node)) {
        break;
      }

      if (node.previousElementSibling) {
        node = findDeepestLastChild(node.previousElementSibling);
      }
      else {
        node = node.parentNode;
      }
    } while (node);
  }
})();