Pjax Everywhere

enable pjax for some sites, speed up access to the site.

当前为 2019-01-05 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Pjax Everywhere
// @namespace    https://github.com/slaier/pjax-everywhere
// @version      0.1
// @author       newbie <[email protected]>
// @description  enable pjax for some sites, speed up access to the site.
// @description:zh 为一些网站开启pjax, 加速访问该网站。
// @include      https://git-scm.com/book/*
// @require      https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js
// @require      https://cdn.jsdelivr.net/npm/[email protected]/jquery.pjax.min.js
// @require      https://cdn.jsdelivr.net/npm/[email protected]/nprogress.min.js
// @resource     nprogressCSS     https://cdn.jsdelivr.net/npm/[email protected]/nprogress.css
// @resource     loadingCSS     https://raw.githubusercontent.com/slaier/pjax-everywhere/master/loading.css?t=1546656015
// @resource     config     https://raw.githubusercontent.com/slaier/pjax-everywhere/master/config.json
// @grant       GM_addStyle
// @grant       GM_getResourceText
// ==/UserScript==

(function() {
    'use strict';

    // Your code here...
    // 加载nprogress的CSS
    let nprogressCSS = GM_getResourceText ("nprogressCSS");
    GM_addStyle (nprogressCSS);
    // loading 动画
    let loadingCSS = GM_getResourceText ("loadingCSS");
    GM_addStyle (loadingCSS);
    let loading = '<div id="loading-wrap" style="display: none;"  ontouchmove="event.preventDefault()" onscroll="event.preventDefault()"><div id="loading-anime" style="background-image:url(https://raw.githubusercontent.com/slaier/pjax-everywhere/master/loading.png)"></div></div>';
    $('body').append(loading);
    // 读取配置文件
    let config = GM_getResourceText('config');
    config = JSON.parse(config);
    $.pjax.defaults.timeout = config['timeout'];

    let configs = config['config'];
    for(let i = 0; i < configs.length; ++i){
        let arg = configs[i];
        let href = location.href;
        let reg = new RegExp(arg['match'], "gi");
        if (reg.test(href)){
            $(document).pjax(arg['selector'], arg['options']);
            break;
        }
    }
    $(document).on('pjax:start', function() {
        NProgress.start();
        $("#loading-wrap").show();
    });
    $(document).on('pjax:end',   function() {
        NProgress.done();
        $("#loading-wrap").fadeOut(300);
    });
})();