Greasemonkey脚本用于查找具有特定条件的行 [英] Greasemonkey script to find rows with certain conditions

查看:103
本文介绍了Greasemonkey脚本用于查找具有特定条件的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了一些不同的方法在表格中查找列包含特定链接的行。

I tried some different ways do find rows in a table where a columns contain a particular link.

我的目标:当链接到xyz时,替换图标与图像相同的行。

My goal: replace an icon when a link to xyz is in this same row as the image.

这是我到目前为止的代码段:

This is my snippet so far:

var rows = document.getElementsByTagName("tr");
for(var i = rows.length - 1; i >= 0; i--) {     
        var links = rows[i].getElementsByTagName("a");
        for(var k = links.length - k; k >= 0; k--) {
            if (links[k].href =="http://www.XXXX.net/forum/index.php?showforum=121"){
                var images = rows[i].getElementsByTagName("img");
                    for (var j=0;j<images.length;j++) {
                    images[j].src = "http://www.XXXX.net/forum/folder_post_icons/icon7.gif";
                    }
            }
        }
}

I我很确定这不是最好的概念。但正如您可能会看到我尝试搜索所有行中的链接,一旦找到论坛121的链接,我会尝试替换此特定行中的所有图像。

I'm pretty sure this is not really the best concept. But as you might see I try to search links in all rows and once the link to forum "121" is found, I try to replace all images in this particular row.

我得到的是现场的每个图像都被替换。

What I get is every image at the site getting replaced.

推荐答案

因为它很简单,所以这里有一个完整的脚本。

它使用 jQuery ,这里是一个方便的jQuery参考。请参阅选择器部分(与CSS选择器几乎相同)。

Since it's simple enough, here's a complete script that does that.
It uses jQuery and here's a handy jQuery reference. See, especially, the Selectors section (which are almost the same as CSS Selectors).

Re:什么我得到的是网站上的每张图片都被替换。 ...

这可能是因为搜索条件过于宽泛。如果它是一个设计不佳(使用表格布局)页面,每个图像可能在一个带有目标链接的表格行!

This maybe because the search criteria is too broad. If it's a poorly designed (uses table layouts) page, every image may be in a table row with a target link!

当发布Greasemonkey问题时,链接到目标网页,或者至少发布足够的网页HTML,我们可以调整GM脚本来匹配。

When posting Greasemonkey questions, link to the target page, or at the very minimum, post enough of the page's HTML that we can adjust the GM script to match.

无论如何,这将有效,可能有关目标页面的更多信息:

Anyway, this will work, possibly pending more information about the target page:

// ==UserScript==
// @name     _Replace image on custom-targeted row
// @include  http://www.XXXX.net/forum/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js
// ==/UserScript==

//--- This may need tuning based on information not provided!
var targetLinks = $("tr a[href*='showforum=121']");

//--- Loop through the links and rewrite images that are in the same row.
targetLinks.each ( function () {
    //--- This next assumes that the link is a direct child of tr > td.
    var thisRow = $(this).parent ().parent ();

    //--- This may need tuning based on information not provided!
    var images  = thisRow.find ("td img");

    //--- Replace all target images in the current row.
    images.each ( function () {
        $(this).attr (  
            'src', 
            'http://www.XXXX.net/forum/folder_post_icons/icon7.gif'
        );
    } );
} );

这篇关于Greasemonkey脚本用于查找具有特定条件的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆