Quick nav

Adding quick-navigation links

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Quick nav
// @version      2.2
// @description  Adding quick-navigation links
// @author       nicael
// @include        *://*.stackexchange.com/questions/*
// @include        *://*stackoverflow.com/questions/*
// @include        *://*serverfault.com/questions/*
// @include        *://*superuser.com/questions/*
// @include        *://*askubuntu.com/questions/*
// @include        *://*stackapps.com/questions/*
// @include        *://*mathoverflow.net/questions/*
// @grant        none
// @namespace    https://greasyfork.org/users/9713
// ==/UserScript==

$("body").append('<div style="text-align:right; position:fixed;top:20%;left:1px;"><div class="quick-nav" style="margin: 0 auto;width:80%;overflow:scroll;padding:5px;text-align:left;background-color:rgba(200,200,200,0.4)"><b>Quick navigation </b><span class="lsep">|</span> <a href="#" id="sts">settings<hr><a href="#">Back to question</a><hr></div></div>');

if(!localStorage.quickNavRealtime){
    localStorage.quickNavRealtime="enable";
}

if(localStorage.quickNavRealtime=="enable"){
    var realtime = " checked";
}
if(localStorage.quickNavRealtime=="disable"){
    var realtime = "";
}
if(!localStorage.quickNavRealtimeFreq){
    localStorage.quickNavRealtimeFreq="5";
}

$("#sts").toggle(function(){
    $(this).after('<div id="panel-sts"><input type="checkbox" id="enable-realtime"'+realtime+'> real-time updates<hr></div>');


},function(){
    $(this).next().remove();
})

$("body").on("change","#enable-realtime",function(){
    if($(this).attr("checked")==undefined){
        localStorage.quickNavRealtime="disable";
    } else {
        localStorage.quickNavRealtime="enable";
    }
    location.reload();
})





if($(document).width()<1350){$(".quick-nav").hide();}else{$(".quick-nav").show();}

navControl();


$(window).resize(function(){
    if($(document).width()<1350){$(".quick-nav").fadeOut();}else{$(".quick-nav").fadeIn();}
    navControl();
})

function navControl(){
    var width = ($(document).width()-$("#question-header").css("width").replace("px",""))/2-30;
    $(".quick-nav").parent().css({"width":width+"px"});
}

quickNavFill();

function quickNavFill(){
    console.log("update");
    var answers = [];
    var votes = [];
    var owners = [];
    var reps = [];
    $(".answer").each(function(){
        answers.push($(this).data("answerid"));
        votes.push($(this).find(".vote-count-post").text());
        owners.push($(this).find(".post-signature:last a[href^='/users']").text());
        var rep = $(this).find(".post-signature:last .reputation-score").text();
        if($(this).find(".post-signature:last .community-wiki").length==0){
            if(rep!=""){
                reps.push(rep);
            } else {
                reps.push("del");
            }
        } else {
            reps.push("cw");
        }

    });
    $(".quick-link").each(function(){
        $(this).next().remove();
        $(this).remove();
    })
    for(var i=0;i<answers.length;i++){
        if(reps[i]=="cw"){
            $(".quick-nav").append('<a class="quick-link" href="#'+answers[i]+'">'+(i+1)+'. ('+votes[i]+') Community Wiki Answer</a><br>');

        } else if(reps[i]=="del"){
            $(".quick-nav").append('<a class="quick-link" href="#'+answers[i]+'">'+(i+1)+'. ('+votes[i]+') Answer by deleted user</a><br>');
        } else {
            $(".quick-nav").append('<a class="quick-link" href="#'+answers[i]+'">'+(i+1)+'. ('+votes[i]+') Answer  by '+owners[i]+' ('+reps[i]+')</a><br>');

        }
    }
    if(answers.length>18){
        $(".quick-nav").css({"height":"400px"})
    }



}

if(localStorage.quickNavRealtime=="enable"){
    setInterval(function(){quickNavFill()},250);
}