Use Greasemonkey to change Text to Images (on a static site)?

user1445975

Is it possible to use Greasemonkey to change text links on a page to actual images, and also to modify those links?

Let's say there's a table on a page that is displaying a bunch of filenames in the second column like this: <tr><td></td><td><a href="wwwlala001.html">wwwlala001.jpg</a></td>...</tr>). Is it possible to have it so that when the page loads, all of the filenames (not the links) in the second column like wwwlala001.jpg change to this?:

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

I tried modifying the code here, but I had no luck:

    // ==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
*/

Thank you!

Brock Adams

Here is the classic, simple approach for a static site, using 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" />'
    );
} );

Notes:

  1. NEVER use @include * for this kind of script!
  2. This assumes that the image base URL is always the same.
  3. The initial selector should be tuned to the site, to avoid unpleasant side effects.
  4. If you hammer some sites, like this for a lot of rapid image requests, they will throttle you. You'll only get a few images, and/or you'll get banned/blocked.
  5. Setting both the height and width like that will distort most images. Just set one or the other.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Use separate bundle for images

来自分类Dev

Plone setting up static images directory

来自分类Dev

Change Animation Speed Series of Images

来自分类Dev

Jekyll变量site.static_files为null

来自分类Dev

Change website text with python

来自分类Dev

how to use PinStrap plugin for bootstrap site?

来自分类Dev

How can I extract text from images?

来自分类Dev

Text appears on image hover with images in a series

来自分类Dev

Use custom scrapy imagePipeline to download images and overwrite existing images

来自分类Dev

Greasemonkey脚本未更新

来自分类Dev

创建预定的Greasemonkey脚本

来自分类Dev

Deviantart的Greasemonkey / Tempermonkey脚本

来自分类Dev

使用GreaseMonkey更改URL

来自分类Dev

Greasemonkey - 调整输入字段

来自分类Dev

Change text selection type in a JTextArea

来自分类Dev

Invalid use of this in a non static member function

来自分类Dev

Is it possible to use fonts for Images/icons in Android

来自分类Dev

Invalid XPath: string-join(.//div[@class="static"]/text(), "")?

来自分类Dev

无效的XPath:string-join(.// div [@ class =“ static”] / text(),“”)?

来自分类Dev

如何禁用Greasemonkey的智能编码?

来自分类Dev

Greasemonkey脚本来拦截按键

来自分类Dev

Greasemonkey通过通配符阻止cookie

来自分类Dev

Greasemonkey:在表格中添加链接

来自分类Dev

在Greasemonkey中更改文本颜色

来自分类Dev

Greasemonkey脚本来拦截按键

来自分类Dev

Greasemonkey:在表格中添加链接

来自分类Dev

Greasemonkey iframe内容访问jQuery

来自分类Dev

Greasemonkey if / else超时与日期

来自分类Dev

如何使用 Greasemonkey 隐藏元素