简体   繁体   English

我如何使用Greasemonkey重组 <ul> 清单?

[英]How can I use Greasemonkey to reorganize a <ul> list?

I tried to adapt someone's script from a Stack Overflow post, but I did something wrong. 我试图从Stack Overflow帖子改编某人的脚本,但是我做错了什么。 I've got essentially no experience with javascript or jQuery, so... Please help? 我基本上没有使用javascript或jQuery的经验,所以...请帮忙?


The target-page HTML looks like this: 目标页面HTML如下所示:

<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 to modify it to look like 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>


I 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 to first.
container.insertBefore (lastTargDiv, firstTargDiv);

//-- Move old first to last.
container.appendChild (firstTargDiv);

As a start -- just flipping the first two. 首先-只需翻转前两个。 It didn't work. 没用 I wasn't sure what I had done wrong. 我不确定自己做错了什么。 I've tried testing different querySelector values, but nothing seems to work. 我尝试测试不同的querySelector值,但似乎没有任何效果。

That code would work (sort of) except that the CSS selectors, passed to querySelector , were off. 除了关闭传递给querySelector的CSS选择器之外,该代码将起作用( querySelector )。 For the HTML you gave, they should be: 对于您提供的HTML,它们应为:

var container    = document .querySelector ("div.userdata > ul");
var firstTargDiv = container.querySelector ("li.name");
var lastTargDiv  = container.querySelector ("li.joindate");

Also, the <li> s would not have ended up quite where you wanted. 另外, <li>不会完全位于您想要的位置。


To sort a uniform collection of nodes just append them to the container in the order you want them. 要对节点的统一集合进行排序,只需将它们按所需的顺序附加到容器中即可。 Appending existing nodes just moves the nodes. 追加现有节点只会移动节点。

If your list of nodes (the <li> s in this case) each have a unique identifier or contained text, use an array to define an arbitrary sort order. 如果您的节点列表(在这种情况下为<li> )每个都有唯一的标识符或包含的文本,请使用数组定义任意的排序顺序。
In this case, each <li> has a unique CSS class so we'll define the sort order like so: 在这种情况下,每个<li>都有一个唯一的CSS类,因此我们将定义排序顺序,如下所示:

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

We can then use this array to snag each <li> by class and append it in the desired order. 然后,我们可以使用该数组按类捕获每个<li> ,并按所需顺序附加它。

Using jQuery, your complete userscript would become: 使用jQuery,您的完整用户脚本将变为:

// ==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==
/*- The @grant GM_addStyle directive is needed to work around a design 
    change introduced in GM 1.0.   It restores the sandbox.
*/
var classOrder  = ["joindate", "name", "custom", "description", "title"];
var containerNd = $("div.userdata > ul");

for (var J = 0, L = classOrder.length;  J < L;  J++) {
    var targetNode  = containerNd.find ('li.' + classOrder[J]);
    containerNd.append (targetNode);
}


You can see the code in action at jsFiddle . 您可以在jsFiddle上查看运行中的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM