更改页面加载时的所有网址

发布于 2024-10-26 12:30:13 字数 463 浏览 1 评论 0原文

假设我有一个页面,我想在其中向每个 url(还有图像等)附加一个参数(例如 ?name=fc)。

我正在尝试使用正则表达式使用greasemonkey 来执行此操作,但它似乎不起作用(即页面上的链接不会更改)。

这是我的代码

var txt = document.documentElement.innerHTML;
var exp = "/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig" ;
document.documentElement.innerHTML =( txt.replace(exp, "$1?name=fc"));

我的 JavaScript 知识几乎为零,所以请耐心等待。

那么问题来了,JavaScript 出了什么问题?

Let's suppose I have a page in which I'd like to append a parameter (for example ?name=fc) to each and every url (also images etc.).

I'm trying to do this with greasemonkey using a regex, but it doesn't seem to work (i.e., the links on the page don't change).

Here is my code

var txt = document.documentElement.innerHTML;
var exp = "/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig" ;
document.documentElement.innerHTML =( txt.replace(exp, "$1?name=fc"));

My JavaScript knowledge is almost zero, so bear with me.

So the question is, what's wrong with that JavaScript?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

为人所爱 2024-11-02 12:30:13
for(var i = 0; i < document.links.length; i++) {
 document.links[i].href += "?name=fc"
}

您应该只使用 document.links 属性。这会将“?name=fc”附加到所有链接(a 和 img 等)。

for(var i = 0; i < document.links.length; i++) {
 document.links[i].href += "?name=fc"
}

You should just use the document.links attribute. This will append "?name=fc" to all links (a's and img's etc).

守望孤独 2024-11-02 12:30:13
//loop 1: change links <a href="..."
var links = document.getElementsByTagName('a');
for(var i = 0, len = links.length; i < len; ++i)
   links[i].href = links[i].href + "?name=fc";

//loop 2: change images <img src="..."
var imgs = document.getElementsByTagName('img');
for(var i = 0, len = imgs.length; i < len; ++i)
   imgs[i].href = imgs[i].href + "?name=fc";

//loop 3: change image maps <area href="..."
var areas = document.getElementsByTagName('area');
for(var i = 0, len = areas.length; i < len; ++i)
   areas[i].href = areas[i].href + "?name=fc";

正如 Mike Lewis 在另一个答案中指定的那样循环 1 和 3 您可以使用他的答案编写一个循环,但您仍然需要用于图像的 src循环 2

//loop 1: change links <a href="..."
var links = document.getElementsByTagName('a');
for(var i = 0, len = links.length; i < len; ++i)
   links[i].href = links[i].href + "?name=fc";

//loop 2: change images <img src="..."
var imgs = document.getElementsByTagName('img');
for(var i = 0, len = imgs.length; i < len; ++i)
   imgs[i].href = imgs[i].href + "?name=fc";

//loop 3: change image maps <area href="..."
var areas = document.getElementsByTagName('area');
for(var i = 0, len = areas.length; i < len; ++i)
   areas[i].href = areas[i].href + "?name=fc";

As specified by Mike Lewis in the other answer in place of loop 1 and 3 you could write one single loop using his answer, but still you need the loop 2 for images' srcs

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文