javDB Selection Tools

Double-click + right-click hyphenated word select on javDB.com/*

目前為 2025-04-18 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         javDB Selection Tools
// @namespace    -
// @icon         https://avatars.githubusercontent.com/u/81081327?v=4
// @version      2.0
// @description  Double-click + right-click hyphenated word select on javDB.com/*
// @author       colemeg
// @match        https://javdb.com/*
// @grant        none
// @license      Do What The Fuck You Want To Public License (WTFPL)
// ==/UserScript==
//
//
//            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
//                    Version 2, December 2004
//
// Copyright (C) 2004 Sam Hocevar <[email protected]>
//
// Everyone is permitted to copy and distribute verbatim or modified
// copies of this license document, and changing it is allowed as long
// as the name is changed.
//
//            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
//   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
//
//  0. You just DO WHAT THE FUCK YOU WANT TO.
//
//
(function () {
    // Reusable caret range from point
    function getTextNodeRangeFromPoint(x, y) {
        if (document.caretRangeFromPoint) return document.caretRangeFromPoint(x, y);
        if (document.caretPositionFromPoint) {
            const pos = document.caretPositionFromPoint(x, y);
            const r = document.createRange();
            r.setStart(pos.offsetNode, pos.offset);
            return r;
        }
        return null;
    }

    // 1. Double-click hyphenated word selector
    document.addEventListener('dblclick', function (e) {
        const range = getTextNodeRangeFromPoint(e.clientX, e.clientY);
        if (!range) return;

        const node = range.startContainer;
        if (node.nodeType !== Node.TEXT_NODE) return;

        const text = node.textContent;
        const offset = range.startOffset;
        const regex = /\b[\w]+-[\w]+\b/g;
        let match;
        while ((match = regex.exec(text)) !== null) {
            if (offset >= match.index && offset <= match.index + match[0].length) {
                const sel = window.getSelection();
                const wordRange = document.createRange();
                wordRange.setStart(node, match.index);
                wordRange.setEnd(node, match.index + match[0].length);
                sel.removeAllRanges();
                sel.addRange(wordRange);
                break;
            }
        }
    });

    // 2. Right-click to select hyphenated word (if it's the only one in text)
    document.addEventListener('contextmenu', function (e) {
        const range = getTextNodeRangeFromPoint(e.clientX, e.clientY);
        if (!range) return;

        const node = range.startContainer;
        if (node.nodeType !== Node.TEXT_NODE) return;

        const text = node.textContent;
        const offset = range.startOffset;
        const matches = [...text.matchAll(/\b[\w]+-[\w]+\b/g)];

        // Only act if there's exactly one hyphenated word
        if (matches.length !== 1) return;

        const match = matches[0];
        if (offset < match.index || offset > match.index + match[0].length) return;

        // Select and prevent native menu
        e.preventDefault();

        const sel = window.getSelection();
        const wordRange = document.createRange();
        wordRange.setStart(node, match.index);
        wordRange.setEnd(node, match.index + match[0].length);
        sel.removeAllRanges();
        sel.addRange(wordRange);
    });
})();