油猴js 获取替换网页链接并重定向

文章介绍了如何编写一个UserScript,通过修改Window.location.href属性,将GitHub页面的域名重定向到githubfast.com,提供了一种便捷的页面访问方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

场景

适用一些镜像网站进行重定向,比如Github。

代码

// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      2024-02-06
// @description  try to take over the world!
// @author       You
// @match        https://github.com/XiangLinPro/IT_book
// @icon         https://www.google.com/s2/favicons?sz=64&domain=github.com
// @grant        none
// ==/UserScript==

(function() { 
    // 获取当前网页链接
    var currentUrl = window.location.href; 
    // 替换域名为 "githubfast.com"
    var newUrl = currentUrl.replace(window.location.hostname, 'githubfast.com'); 
    // 重新访问新链接
    window.location.href = newUrl;
})();

Window:location 属性

location.href常见的几种形式
self.location.href;//当前页面打开URL页面
window.location.href;//当前页面打开URL页面
this.location.href;//当前页面打开URL页面
location.href;// 当前页面打开URL页面
parent.location.href;//在父页面打开新页面
top.location.href;//在顶层页面打开新页面

参考

https://developer.mozilla.org/zh-CN/docs/Web/API/Window/location

在这里插入图片描述

https://www.w3school.com.cn/jsref/prop_loc_href.asp

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值