简体   繁体   中英

Tampermonkey script to edit links in source

I've recently started working at a company as a student where the workload is minimal. Many of the jobs they do manually, I can solve with a simple script, thus giving me loads of free time.

The downside to this is my account does not have internet access outside the website relevant to my workplace. I've been sniffing around and notice their redirect proxy server has a really amateurish system to block websites, it basically scans the web address and looks for certain keywords. By simply adding ?google to a web address, I can access it without issue.

But, every single link has to be edited with "?google" at the end, which is not really efficient. So I'm looking to write a script that looks up links (CSS/JS/Pictures/etc..) and automatically adds ?google at the end.

For example:

https://cdn.sstatic.net/stackoverflow/all.css?v=4a57bb936dd5

would become:

https://cdn.sstatic.net/stackoverflow/all.css?v=4a57bb936dd5?google

Since I haven't worked yet with Tampermonkey yet I wonder if anyone knows a simple and efficient way to do this?

Well after fiddling a bit I managed to write a script, here's the code if anyone's interested.

// ==UserScript==
// @name          Google Add
// @namespace     
// @description   
// @include       *
// ==/UserScript==

var srcs = document.links;
var links = document.getElementsByTagName("link");
var scripts = document.getElementsByTagName("script");
var imgs = document.getElementsByTagName("img");
var iframes = document.getElementsByTagName("iframe");


for (i = 0; i < links.length; i++ ) {
    links[i].href = links[i].href+'?google';        
}

for (i = 0; i < scripts.length; i++ ) {
    scripts[i].src = scripts[i].src+'?google'; 
}

for (i = 0; i < imgs.length; i++ ) {
    imgs[i].src = imgs[i].src+'?google'; 
}

for (i=0; i<srcs.length; i++)
{
    srcs[i].href = srcs[i].href+'?google';
}

for (i=0; i<iframes.length; i++){

    iframes[i].src = iframes[i].src+'?google'; 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM