Rewrite parts of links using Greasemonkey and FireFox

吃可爱长大的小学妹 提交于 2019-12-18 09:06:26

问题


A friend of mine uploaded about 20 or so galleries of nature shots she's done over the past year or so onto webshots.com, however, I just purchased a paid Flickr account for her as a birthday gift, and I want to download all of her photos from webshots and have them ready for her to upload to Flickr once she gets the email about her account upgrade (she's out of the country - no internet access.)

I don't have access to her webshots account, so I've resorted to Greasemonkey and DownThemAll to start saving her images into folders on my desktop.

I'm somewhat new to javascript, and all the "user scripts" available for Greasemonkey don't exactly do what I need.

When a gallery page is loaded:

(http://[category].webshots.com/album/[album-id]), 

I need the Greasemonkey script to find all links to images:

(http://[category].webshots.com/photo/[photo-page-id])

and re-write them to reflect this scheme:

(http://community.webshots.com/photo/fullsize/[photo-page-id]) 

Is this easy to do? It seems like it would be, but I can't seem to get it right.

Here's my current Greasemonkey script that doesn't work:

// ==UserScript==
// @name           Webshot Gallery Fixer
// @namespace      WGF
// @description    Fixes webshot galleries
// @include        http://*.webshots.com/*
// ==/UserScript==

var links = document.getElementsByTagName("a"); //array
var regex = /^(http:\/\/)([^\.]+)(\.webshots\.com\/photo\/)(.+)$/i;
for (var i=0,imax=links.length; i<imax; i++) {
   links[i].href = liks[i].href.replace(regex,"$1community$3fullsize/$4");
}

回答1:


var links = document.getElementsByTagName("a"); //array
var regex = /^(http:\/\/)([^\.]+)(\.webshots\.com\/photo\/)(.+)$/i;
for (var i=0,imax=links.length; i<imax; i++) {
   links[i].href = links[i].href.replace(regex,"$1community$3fullsize/$4");
}

ought to do the trick




回答2:


Your code was fine but for a typo:

links[i].href = liks[i] .href.replace(regex,"$1community$3fullsize/$4");

Replace liks with links and it works.

Open up the Firebug console, turn most of the warnings on, and reload the page. You can see errors your script might cause (plus a metric ton of errors from the site itself).




回答3:


Quite late answer, but I'll post anyway in case it'll do some good.

I'm kind of newb to this stuff myself, but I don't think it will help to assign a value to links[i].href since it's just a variable. You won't change anything on the page by doing so. I think you should replace this:

links[i].href = links[i].href.replace(regex,"$1community$3fullsize/$4");

with this:

document.getElementsByTagName("a")[i].href = links[i].href.replace(regex,"$1community$3fullsize/$4");


来源:https://stackoverflow.com/questions/3232992/rewrite-parts-of-links-using-greasemonkey-and-firefox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!