Google Books Copy Override

Enable text copy in Google Books

目前為 2024-11-26 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Google Books Copy Override
// @namespace    http://tampermonkey.net/
// @version      1
// @description  Enable text copy in Google Books
// @match        *://play.google.com/books/*
// @match        *://books.googleusercontent.com/books/reader/frame*
// @icon         https://www.gstatic.com/images/branding/product/2x/play_books_96dp.png
// @grant        GM_setClipboard
// @run-at       document-idle
// ==/UserScript==

(function() {
    'use strict';

    const isIframe = window.location.href.includes('books.googleusercontent.com/books/reader/frame');

    if (isIframe) {
        const style = document.createElement('style');
        style.textContent = `* {user-select: text !important} .overlay, .selection-overlay, .page-overlay {display: none !important}`;
        document.head.appendChild(style);

        document.addEventListener('copy', e => e.stopPropagation(), true);
        document.addEventListener('keydown', e => {
            if ((e.ctrlKey || e.metaKey) && e.key === 'c') {
                e.preventDefault();
                const text = window.getSelection().toString().trim();
                if (text) window.parent.postMessage({ type: 'COPY_TEXT', text }, '*');
            }
        }, true);
    } else {
        window.addEventListener('message', e => {
            if (e.data?.type === 'COPY_TEXT' && e.data.text) {
                GM_setClipboard(e.data.text);
            }
        });
    }
})();