javascript - How can I use Greasemonkey to reorganize a <ul> list? -


i tried adapt someone's script stack overflow post, did wrong. i've got no experience javascript or jquery, so... please help?


the target-page html looks this:

<div class="container7">     <div class="userdata">         <h4></h4>         <ul id="userinfo" class="set textsize">             <li class="name"></li>             <li class="joindate"></li>             <li class="title"></li>             <li class="custom"></li>             <li class="description"></li>         </ul>     </div> </div> 

i want modify this:

<div class="container7">     <div class="userdata">         <h4></h4>         <ul id="userinfo" class="set textsize">             <li class="joindate"></li>             <li class="name"></li>             <li class="custom"></li>             <li class="description"></li>             <li class="title"></li>         </ul>     </div> </div> 


tried this:

// ==userscript== // @name        reorganize li // @namespace   blah // @description reorganize li // @include     https://www.thewebsite.com/* // @version     1 // @grant       none // ==/userscript==  var container    = document.queryselector (".userdata"); var firsttargdiv = container.queryselector (".userdata > li.name:first-child"); var lasttargdiv  = container.queryselector (".userdata > li.joindate:last-child");  //-- swap last first. container.insertbefore (lasttargdiv, firsttargdiv);  //-- move old first last. container.appendchild (firsttargdiv); 

as start -- flipping first two. didn't work. wasn't sure had done wrong. i've tried testing different queryselector values, nothing seems work.

that code work (sort of) except css selectors, passed queryselector, off. html gave, should be:

var container    = document .queryselector ("div.userdata > ul"); var firsttargdiv = container.queryselector ("li.name"); var lasttargdiv  = container.queryselector ("li.joindate"); 

also, <li>s not have ended quite wanted.


to sort uniform collection of nodes append them container in order want them. appending existing nodes moves nodes.

if list of nodes (the <li>s in case) each have unique identifier or contained text, use array define arbitrary sort order.
in case, each <li> has unique css class we'll define sort order so:

var classorder = ["joindate", "name", "custom", "description", "title"]; 

we can use array snag each <li> class , append in desired order.

using jquery, complete userscript become:

// ==userscript== // @name     reorganize li // @include  https://www.thewebsite.com/* // @require  http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js // @grant    gm_addstyle // ==/userscript== /*- @grant gm_addstyle directive needed work around design      change introduced in gm 1.0.   restores sandbox. */ var classorder  = ["joindate", "name", "custom", "description", "title"]; var containernd = $("div.userdata > ul");  (var j = 0, l = classorder.length;  j < l;  j++) {     var targetnode  = containernd.find ('li.' + classorder[j]);     containernd.append (targetnode); } 


can see code in action @ jsfiddle.


Comments

Popular posts from this blog

php - render data via PDO::FETCH_FUNC vs loop -

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

The canvas has been tainted by cross-origin data in chrome only -