Overleaf PDF Viewer Page Numbers

show page numbers in PDF preview panel

目前為 2024-03-27 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==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)
        );
    }
})();