2

是否可以使用 Greasemonkey 将页面上的文本链接更改为实际图像,并修改这些链接?

假设页面上有一个表格,它在第二列中显示一堆文件名,如下所示:<tr><td></td><td><a href="wwwlala001.html">wwwlala001.jpg</a></td>... </tr>)。是否可以让它在页面加载时,第二列中的所有文件名(不是链接),如 wwwlala001.jpg 更改为这个?:

<img src="http://domain.com/images/wwwlala001.jpg" width="200" height="200" />

我尝试在这里修改代码,但没有运气:

    // ==UserScript==
// @name    _Image delinker
// @include http://dotup.org
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant   GM_addStyle
// ==/UserScript==

var imageExtensions = ["gif", "png", "jpg", "jpeg"];
var imgExtRegex     = new RegExp(
    '\\.(' + imageExtensions.join ('|') + ')$', 'i'
);

/*-- Tune the CSS path, for each site, to only find links that can be
    the image links you care about.
*/
//-- For forums.hardwarezone.com.sg
waitForKeyElements ("page div > a", delinkImage);

function delinkImage (jNode) {
    var imgUrl  = jNode.attr ("href");

    if (imgExtRegex.test (imgUrl) ) {
        //-- Found an image link.  Replace contents.
        jNode.html (
            '<img src="http://domain.com/images/' + imgUrl
            + '" width="200" height="200" class="gmDeLinked" alt="GM replaced image">'
        );
    }
}

GM_addStyle ( "                                 \
    img.gmDeLinked {                            \
        border:             1px solid lime;     \
        max-width:          90vw;               \
    }                                           \
" );
/*
Exception: waitForKeyElements is not defined
@Scratchpad/2:18
*/

谢谢!

4

2 回答 2

2

我不是一个用户脚本专家,但我有一个来自这里的脚本。原始站点已关闭,但这是脚本。

// ==UserScript==
// @name          Inline Image Relinker
// @namespace     /web/20070712085327/http://www.sitzmar.com
// @description   Replaces links to images with the actual image
// @include       *
// ==/UserScript==

(function() 
{
    function getXPath(p, context) 
    { 
        var arr = new Array(); 
        var xpr = document.evaluate(p,context,null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);

        for(i = 0;item = xpr.snapshotItem(i); i++)
        { 
            arr.push(item); 
        } 

        return arr; 
    }

    doc = window.document;
    var xpath = "//A[(contains(@href, '.jpg') or contains(@href, '.jpeg') or contains(@href, '.gif') or contains(@href, '.bmp') or contains(@href, '.png')) and not(contains(@href, '.php') or contains(@href, '.asp'))]";

    results = getXPath(xpath, doc);

    for(i = 0; i < results.length; i++)
    {
        var img = document.createElement("img");
        var source = results[i].getAttribute("href");

        img.setAttribute("src", source);
        img.setAttribute("class", "what");
        results[i].textContent = "";
        results[i].appendChild(img);
    }
}
)();
于 2013-09-10T02:03:27.797 回答
1

这是使用 jQuery的静态站点的经典简单方法:

// ==UserScript==
// @name     _image delinker, static site
// @include  http://dotup.org/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @grant    GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/

var imageExtensions = ["gif", "png", "jpg", "bmp"];
var imgExtRegex     = new RegExp(
    '\\.(' + imageExtensions.join ('|') + ')$', 'i'
);

//-- This jQuery selector is custom for each new site...
var imgLinks    = $("table tr td > a")
//-- Also custom for the site...
var urlBase     = "http://www.dotup.org/uploda/";

//-- Remove non-image links.
imgLinks.filter ( function() {
    return imgExtRegex.test (this.textContent);
} );

imgLinks.each ( function () {
    var jThis       = $(this);  // This is one of the links
    var filename    = $.trim (jThis. text () );

    //-- Rreplace link content with image:
    jThis.html (
        '<img src="' + urlBase + filename + '" height="200" />'
    );
} );

笔记:

  1. 永远不要使用@include *这种脚本!
  2. 这假定图像基本 URL 始终相同。
  3. 初始选择器应针对该站点进行调整,以避免不愉快的副作用。
  4. 如果你敲打一些网站,比如很多快速的图像请求,他们会扼杀你。你只会得到几张图片,和/或你会被禁止/阻止。
  5. 像这样设置高度和宽度会扭曲大多数图像。只需设置一个或另一个。
于 2013-09-10T02:32:10.687 回答