Simple Note taker

Simple Note taker allows you to easily take notes while browsing the web. Simply select any text on a webpage.

目前為 2024-06-09 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Simple Note taker
// @description  Simple Note taker allows you to easily take notes while browsing the web. Simply select any text on a webpage.
// @author       momoehab
// @match      *://*/*
// @license MIT
// @version 0.0.1.20240609220313
// @namespace https://greasyfork.org/users/1315328
// ==/UserScript==

function getSelectedText() {
    var text = "";
    if (typeof window.getSelection != "undefined") {
        text = window.getSelection().toString().trim();
    } else if (typeof document.selection != "undefined" && document.selection.type == "Text") {
        text = document.selection.createRange().text.trim();
    }
    return text.toUpperCase(); // Convert selected text to uppercase
}

function handleSelection() {
    var selectedText = getSelectedText();
    if (selectedText) {
        const storedText = localStorage.getItem(selectedText);
        if (storedText) {
            const shouldDelete = confirm("Info: " + storedText + "\nWant to delete?");
            if (shouldDelete) {
                var searchTerm = storedText; // Get the text of the item to delete
                removeSpanAndUnderline(searchTerm);
                localStorage.removeItem(selectedText);

            }

        } else {
            const newText = prompt("You selected: " + selectedText + "\nEnter details:");
            if (newText !== null) {
                localStorage.setItem(selectedText, newText);
            }
        }
    }
}

document.addEventListener("mouseup", handleSelection);
document.addEventListener("keyup", function (event) {
    if (event.key === "Enter") {
        handleSelection();
    }
});

// Underline previously selected words with dotted line and show data as alt text on mouseover
document.addEventListener("DOMContentLoaded", function () {
    var storedWords = Object.keys(localStorage);
    storedWords.forEach(function (word) {
        var bodyHTML = document.body.innerHTML;
        var re = new RegExp("\\b(" + word.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + ")\\b", "gi");
        var data = localStorage.getItem(word);
        bodyHTML = bodyHTML.replace(re, function (match) {
            return '<span style="text-decoration: underline dotted" data-text="' + data + '" onmouseover="this.title=this.getAttribute(\'data-text\')">' + match + '</span>';
        });
        document.body.innerHTML = bodyHTML;
    });
});

function removeSpanAndUnderline(word) {
    // Get all spans in the document
    var spans = document.getElementsByTagName('span');

    // Iterate through all spans
    for (var i = 0; i < spans.length; i++) {
        var span = spans[i];

        // Check if the span contains the specific word and is underlined
        if (span.textContent.includes(word) && span.style.textDecoration === 'underline') {
            // Remove the underline
            span.style.textDecoration = 'none';

            // Replace the span with its content
            var textNode = document.createTextNode(span.textContent);
            span.parentNode.replaceChild(textNode, span);
        }
    }
}