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.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला 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);
  }
})();