Greasy Fork

Overleaf PDF 预览界面显示页码

在 Overleaf 的 PDF 预览界面中显示页码

目前为 2022-05-01 提交的版本。查看 最新版本

// ==UserScript==
// @name              Overleaf PDF Viewer Page Numbers
// @name:zh-CN        Overleaf PDF 预览界面显示页码
// @namespace         acoder.me
// @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(() => {
        document.body.style.overflow = 'hidden'
        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('page');
        function displayCurrentPage(){
            for (let i = 0; i < pages.length; ++i) {
                if (isInViewport(pages[i])) {
                    btn.innerText = `${i + 1}/${pages.length}`;
                    break;
                }
            }
        };
        btn.onclick = () => {
            displayCurrentPage();
        };
        let pdfControls = document.getElementsByClassName("pdfjs-controls");
        if(pdfControls.length >= 0) {
            pdfControls[0].addEventListener('mouseover', () => {displayCurrentPage();});
        }
        btns.appendChild(btn);
        clearInterval(id);
    }, 1000);
})();