thread win

sort comments by favorites

当前为 2014-08-05 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name           thread win
// @namespace      bperkins
// @description    sort comments by favorites
// @include        http://www.metafilter.com/*
// @include http://ask.metafilter.com/*
// @include http://metatalk.metafilter.com/*
// @version        1.4
// @grant       none
// ==/UserScript==


var savedstuff;

var winNode;
var unwinNode;
var copy;



function sortFav(a,b) {



    return (-(getFav(a)-getFav(b)));

}
function getFav(a) 
{
    var toplevel = a.getElementsByTagName("SPAN");
    var notloggedin = 0;

    if (toplevel.length ==0 ) {
	return 1;
    }   

    var bylinespan = toplevel[0].getElementsByTagName("SPAN");

    if (bylinespan.length ==0) {
        notloggedin =1
	bylinespan = a.getElementsByTagName("SPAN");
    }
    if (bylinespan.length ==0 ) {
	return 1;
    }
    
	
    var favspan;

    if (bylinespan[0].getAttribute("class") == "staffp" 	   ) { 
	 favspan = bylinespan[2].getElementsByTagName("SPAN");
    } else {
    favspan = bylinespan[0].getElementsByTagName("SPAN");
    }

     if   (favspan.length ==0) {
	 //No extra span layer in MeTalk
	 favspan=bylinespan
     }

     var fava = favspan[0].getElementsByTagName("A");

     



    var fav =0;
    re = new RegExp("\\d+")


	if (fava.length > 0 &&fava[0] != undefined ) { 
	    
	    var m;
	    if (notloggedin) {
		if (fava.length >2) {
		    m=re.exec(fava[2].innerHTML);
		} else {
		    return 1
		}
		
	    } else {
		m=re.exec(fava[0].innerHTML);
	    }
	    fav=m[0];

	}


    return (1*fav)+1;

}




function  unwin () {

    copy.replaceChild(winNode,unwinNode);

 
    var parent=getCommentParent ();
	
    var newstuff = parent.childNodes;
		
    while ( newstuff.length >0 ) {
	    
	parent.removeChild(newstuff[0]);
    }
	

    for (var j = 0; j < savedstuff.length; j++) {
	parent.appendChild(savedstuff[j]);
    }   
	
    
}

function getCommentParent () {
	

    page = document.evaluate(
			     "//div[@id='page']",
			     document,
			     null,
			     XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
			     null);

   
    for (var i = 0; i < page.snapshotLength; i++) {
	stuff = page.snapshotItem(i).childNodes;
	for (var j = 0; j < stuff.length; j++) {	
	    if (stuff[j].nodeName.toUpperCase()=="DIV" &&
		(stuff[j].getAttribute("class") == "comments" ||
		 stuff[j].getAttribute("class") == "comments bestleft"||
		 stuff[j].getAttribute("class") == "comments best")) {
		return  page.snapshotItem(i);
	    }
	}
    }	
    page = document.evaluate(
			     "//body[@id='body']",
			     document,
			     null,
			     XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
			     null);
 
    for (var i = 0; i < page.snapshotLength; i++) {
	stuff = page.snapshotItem(i).childNodes;
	for (var j = 0; j < stuff.length; j++) {
	    if (stuff[j].nodeName.toUpperCase()=="DIV" &&
		(stuff[j].getAttribute("class") == "comments" ||
		 stuff[j].getAttribute("class") == "comments bestleft"||
		 stuff[j].getAttribute("class") == "comments best")) {
		return page.snapshotItem(i);
	    }
			
	}	
    }

}



function  threadwin () {

    copy.replaceChild(unwinNode,winNode);


    var parent= getCommentParent();
	    
			
    var stuff = parent.childNodes;
    var commentidxs = new Array;
    var comments = new Array;
    var objects = new Array ;
    savedstuff=new Array;

    var k=0;     
    var favs =""

	for (var j = 0; j < stuff.length; j++) {
			
				 
	    if (stuff[j].nodeName.toUpperCase()=="DIV" &&
		(stuff[j].getAttribute("class") == "comments" ||
		 stuff[j].getAttribute("class") == "comments bestleft"||
		 stuff[j].getAttribute("class") == "comments best")) {
		
     
		commentidxs.push(j);
		comments.push(stuff[j]);
	    } 
   
	    objects.push(stuff[j]);
	    savedstuff.push(stuff[j]);

	}

    comments.sort(sortFav);

	for (var j = 0; j < comments.length; j++) {

	    objects[commentidxs[j]]=comments[j];

	}


    while ( stuff.length >0 ) {

	parent.removeChild(stuff[0]);
    }


    for (var j = 0; j < objects.length; j++) {
	parent.appendChild(objects[j]);
    }   

    
}


res = document.evaluate(
			"//div[@class='copy']",
			document,
			null,
			XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
			null);

copy=res.snapshotItem(0)

    if (copy.parentNode.id == "page" ) {


	winNode = document.createElement('a');
	winNode.innerHTML = "<small>[threadwin]</small> ";
	winNode.href ="#";
	winNode.addEventListener("click", threadwin, true);
	
	copy.appendChild( winNode );
	
	unwinNode = document.createElement('a');
	unwinNode.innerHTML = "<small>[unwin]</small>";
	unwinNode.href ="#";
	unwinNode.addEventListener("click", unwin, true);
	

	
    }