更改页面加载时的所有网址
假设我有一个页面,我想在其中向每个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该只使用 document.links 属性。这会将“?name=fc”附加到所有链接(a 和 img 等)。
You should just use the document.links attribute. This will append "?name=fc" to all links (a's and img's etc).
正如 Mike Lewis 在另一个答案中指定的那样
循环 1 和 3
您可以使用他的答案编写一个循环,但您仍然需要用于图像的src
的循环 2
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 theloop 2
for images'src
s