thread win

sort comments by favorites

2014-07-31 या दिनांकाला. सर्वात नवीन आवृत्ती पाहा.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला 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.2
// ==/UserScript==


var savedstuff;

var winNode;
var unwinNode;
var copy;



function sortFav(a,b) {



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

}
function getFav(a) 
{
   var thing = a.getElementsByTagName("SPAN");
   if (thing.length ==0 ) {
          return 1;
    }   

    thing = thing[0].getElementsByTagName("SPAN");


    if (thing.length ==0) {
    	
       thing = a.getElementsByTagName("SPAN");
       if (thing.length ==0 ) {
          return 1;
       }
       thing =thing[0].getElementsByTagName("A");
           

       thing =new Array(thing[2]);

	if (thing == undefined) {
          thing= new Array();
          
        }	

    } else {


    thing = thing[0].getElementsByTagName("SPAN");
    

    thing = thing[0].getElementsByTagName("A");
    }
    var fav =0;
    re = new RegExp("\\d+")


    if (thing.length > 0 &&thing[0] != undefined ) { 

      var m =re.exec(thing[0].innerHTML);
      fav=m[0]

        }


    return (1*fav)+1;

}




function  unwin () {

    copy.replaceChild(winNode,unwinNode);

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


    for (var i = 0; i < page.snapshotLength; i++) {
   
	var parent=page.snapshotItem(i);
	
	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  threadwin () {

    copy.replaceChild(unwinNode,winNode);


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


    for (var i = 0; i < page.snapshotLength; i++) {
   
	var parent=page.snapshotItem(i);
	    
	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);
	

	
    }