Greasy Fork

Overleaf PDF Viewer Page Numbers

show page numbers in PDF preview panel

目前为 2024-03-27 提交的版本。查看 最新版本

// ==UserScript==
// @name              Overleaf PDF Viewer Page Numbers
// @name:zh-CN        Overleaf PDF 预览界面显示页码
// @version           0.0.4
// @description       show page numbers in PDF preview panel
// @description:zh-cn 在 Overleaf 的 PDF 预览界面中显示页码
// @author            wanng
// @match             https://www.overleaf.com/project/*
// @icon              https://www.overleaf.com/favicon.ico
// @grant             none
// @license           MIT
// @namespace https://greasyfork.org/users/326819
// ==/UserScript==

(function() {
    'use strict';

    const checkInterval = setInterval(() => {
        const pages = document.querySelectorAll('.pdfjs-viewer-inner .pdfViewer .page');
        if (pages.length > 0) {
            clearInterval(checkInterval); // 停止定时器,因为我们找到了页面

            const btns = document.querySelector('.pdfjs-controls .btn-group');
            if (!btns) return;

            const total_len = pages.length; // 获取总页数
            const btn = btns.lastElementChild.cloneNode();
            btn.href = '#';
            btn.innerText = 'p/P'; // 初始化按钮文本

            btn.onclick = () => {
                for (let i = 0; i < pages.length; ++i) {
                    if (isInViewport(pages[i])) {
                        btn.innerText = `${i + 1}/${total_len}`; // 更新显示当前页数/总页数
                        break;
                    }
                }
            };

            btns.appendChild(btn);
        }
    }, 500); // 每500毫秒检查一次

    function isInViewport(element) {
        const rect = element.getBoundingClientRect();
        return (
            rect.top >= 0 &&
            rect.left >= 0 &&
            rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
            rect.right <= (window.innerWidth || document.documentElement.clientWidth)
        );
    }
})();