Overleaf PDF Viewer Page Numbers

show page numbers in PDF preview panel

您需要先安裝使用者腳本管理器擴展,如 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 预览界面显示页码
// @namespace         a23187.cn
// @version           0.0.1
// @description       show page numbers in PDF preview panel
// @description:zh-cn 在 Overleaf 的 PDF 预览界面中显示页码
// @author            a23187
// @match             https://www.overleaf.com/project/*
// @icon              https://www.overleaf.com/favicon.ico
// @grant             none
// @license           MIT
// ==/UserScript==

(function() {
    'use strict';
    const id = setInterval(() => {
        const btns = document.querySelector('.pdfjs-controls .btn-group');
        if (!btns) {
            return;
        }

        const btn = btns.lastElementChild.cloneNode();
        btn.href = '#';
        btn.innerText = 'p/P';

        function isInViewport(e) {
            const rect = e.getBoundingClientRect();
            const midPoint = (window.innerHeight || document.documentElement.clientHeight) / 2;
            return rect.top <= midPoint && midPoint <= rect.bottom;
        }
        const pages = document.getElementsByClassName('pdf-page-container page-container ng-scope');
        btn.onclick = () => {
            for (let i = 0; i < pages.length; ++i) {
                if (isInViewport(pages[i])) {
                    btn.innerText = `${i + 1}/${pages.length}`;
                    break;
                }
            }
        };

        btns.appendChild(btn);

        clearInterval(id);
    }, 1000);
})();