知乎排版优化

知乎排版优化!

目前為 2017-08-24 提交的版本,檢視 最新版本

// ==UserScript==
// @name         知乎排版优化
// @namespace    https://www.zhihu.com/
// @version      1.5
// @description  知乎排版优化!
// @author       chenglinz <onepiece8971@163.com>
// @match        https://www.zhihu.com/question/*
// @match        https://www.zhihu.com/people/*
// @match        https://www.zhihu.com/topic/*
// @match        https://www.zhihu.com/search?*
// @match        https://www.zhihu.com
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    var path = location.pathname.slice(0);
    (function (){
        if (path == '/') {
            var re = document.querySelector('.TopstorySideBar');
            re.remove();
            var inner = document.querySelector('.Topstory-mainColumn');
            inner.style.marginLeft = '150px';
            inner.style.marginRight = '150px';
        }
    })();
    (function (){
        if (path.match(/\/question\/\d+/g)) {
            var re = document.querySelector('.Question-sideColumn');
            re.remove();
            var inner = document.querySelector('.Question-mainColumn');
            inner.style.marginLeft = '155px';
        }
    })();
    (function (){
        if (path.match(/\/topic\/.*/g)) {
            // 知乎老版本中间居中.
            var inner = document.querySelector('.zu-main-content-inner');
            inner.style.marginLeft = '140px';
            inner.style.marginRight = '150px';
            var backtotop = document.querySelector('.zh-backtotop');
            backtotop.style.marginLeft = '350px';
            // 修改侧边栏宽度以适应居中
            var rightInner = document.querySelector('.zu-main-sidebar');
            rightInner.className='zu-main-sidebar';
            rightInner.style.width = '120px';
            rightInner.style.marginLeft = '-130px';
        }
    })();
    (function (){
        if (path.match(/\/people\/.*/g)) {
            var right = document.querySelector('.Profile-sideColumn');
            right.style.visibility = "hidden";
            var inner = document.querySelector('.Profile-mainColumn');
            inner.style.marginLeft = '155px';
        }
    })();
    // 只应用到主页,问题页,话题页,搜索页
    if (path.match(/\/search?.*/g)) {
        var re = document.querySelector('.zu-main-sidebar');
        re.style.visibility = "hidden";
        var inner = document.querySelector('.zu-main-content-inner');
        inner.style.marginLeft = '150px';
        inner.style.marginRight = '150px';
        var backtotop = document.querySelector('.zh-backtotop');
        backtotop.style.marginLeft = '350px';
    }
    (function(){
        // 按h键,导航不随页面滚动固定,再次按h取消
        document.body.addEventListener("keydown", (e) => {
            if (e.keyCode === 72) {
              let header = document.getElementsByTagName('header')[0];
              let sticky = document.querySelector('.Sticky--holder');
              header.style.position = header.style.position == "relative" ? "fixed" : "relative";
              sticky.style.position = sticky.style.position == "absolute" ? "relative" : "absolute";
            }
        });
    })();
})();