Keul Bookmark

un double clic dans le texte sauvegarde la position dans l'URL / Recharger la page la fait défiler sur la position

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name     Keul Bookmark
// @version  1
// @grant    none
// @match    *://*/*
// @description un double clic dans le texte sauvegarde la position dans l'URL / Recharger la page la fait défiler sur la position
// @namespace https://greatest.deepsurf.us/users/805853
// ==/UserScript==


window.addEventListener("load", load_bookmark);
window.addEventListener("dblclick", save_bookmark);

function save_bookmark() {
  var userSelection = window.getSelection ? window.getSelection() : document.selection.createRange();
  var nodePosition = 0;
  var nodeIterator = document.createNodeIterator(document.body,NodeFilter.SHOW_TEXT);
  while (currentNode = nodeIterator.nextNode()) {
    nodePosition++;
    if(currentNode==userSelection.anchorNode) {
      return window.location.hash = "#bookmark-" + nodePosition + "-" + userSelection.anchorOffset;
    }
  }
}

function load_bookmark() {
  var bookmark=window.location.hash.split("-")
  if(bookmark[0]!='#bookmark' || bookmark.length!=3) return;
  var nodePosition = 0;
  var nodeIterator = document.createNodeIterator(document.body,NodeFilter.SHOW_TEXT);
  while (currentNode = nodeIterator.nextNode()) {
    nodePosition++;
    if(nodePosition == bookmark[1]) {
      var backupNode = currentNode.cloneNode(true);
      var startText = document.createTextNode(currentNode.data.substring(0,bookmark[2]));
      var endText = document.createTextNode(currentNode.data.substring(bookmark[2],currentNode.data.length));
      var bookmarkText = document.createElement("span")
      bookmarkText.setAttribute("style","height:0.7em;display:inline-block;border:2px solid red;");
      var finalNode = document.createElement("span");
      finalNode.appendChild(startText);
      finalNode.appendChild(bookmarkText);
      finalNode.appendChild(endText);
      currentNode.parentNode.replaceChild(finalNode,currentNode);
      bookmarkText.scrollIntoView({behavior: "smooth", block: "center", inline: "center"});
      // currentNode.parentNode.replaceChild(backupNode, currentNode); // put old text back after scroll?
      return;
    }
  }
}