Greasy Fork

Greasy Fork is available in English.

Collapse Sidebar in Acdemic Websites

Make elements on multiple websites collapsible

当前为 2023-07-20 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Collapse Sidebar in Acdemic Websites
// @namespace    https://github.com/henflower/Sci-scripts/blob/main/Collapse-sidebar.js
// @version      0.21
// @description  Make elements on multiple websites collapsible
// @author       Henflower
// @match        *://www.nature.com/articles/*
// @match        *://www.sciencedirect.com/*
// @match        *://www.ncbi.nlm.nih.gov/pmc/articles/*
// @match        *://www.science.org/doi/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const collapsibleElements = [
        {
            hostname: 'nature.com',
            selector: '.c-article-extras.u-hide-print',
            articleSelector: 'main.c-article-main-column'
        },
        {
            hostname: 'sciencedirect.com',
            selector: '.RelatedContent, .TableOfContents.text-s',
            articleSelector: '.col-lg-12.col-md-16.pad-left.pad-right.u-padding-s-top'
        },
        {
            hostname: 'ncbi.nlm.nih.gov',
            selector: '.pmc-sidebar, .usa-nav',
            articleSelector: '.pmc-article'
        }
        /*
        ,{
            hostname: 'science.org',
            selector: '.article__aside',
            articleSelector: '.article__body'
        }
        */
    ];

    function addCollapsibleButton(element, article) {
        const button = document.createElement('button');
        button.innerText = '-';
        button.style.position = 'fixed';
        button.style.top = '10px';
        button.style.right = '10px';
        button.style.zIndex = 9999;
        button.style.width = '25px';
        button.style.height = '25px';
        button.style.borderRadius = '50%';
        button.style.opacity = '0.8';
        button.style.textAlign = 'center';
        button.addEventListener('click', () => {
            if (element.style.display === 'none') {
                element.style.display = 'block';
                article.style.width = ''; // Reset the width of the article
                button.innerText = '-';
            } else {
                element.style.display = 'none';
                article.style.width = '100%'; // Increase the width of the article
                button.innerText = '+';
            }
        });
        document.body.appendChild(button);
    }

    const currentHostname = window.location.hostname;
    const currentCollapsibleElements = collapsibleElements.filter(
        (element) => currentHostname.includes(element.hostname)
    );
    currentCollapsibleElements.forEach((element) => {
        const el = document.querySelector(element.selector);
        const article = document.querySelector(element.articleSelector);
        if (el && article) {
            addCollapsibleButton(el, article);
        }
    });
})();