Greasy Fork

Greasy Fork is available in English.

网址纠错

在您访问了错误的网址时提示您,并可设置白名单和删除白名单

当前为 2025-01-17 提交的版本,查看 最新版本

作者
xiaote_XT
评分
0 0 0
版本
1.4
创建于
2025-01-15
更新于
2025-01-17
大小
10.1 KB
许可证
暂无
适用于
所有网站

// ==UserScript==
// @name 网址纠错
// @namespace https://xiaote.data.blog/wzjc
// @version 1.4
// @description 在您访问了错误的网址时提示您,并可设置白名单和删除白名单
// @author xiaote_XT
// @match *://*/*
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_addStyle
// @grant GM_registerMenuCommand
//https://update.greasyfork.icu/scripts/523856/%E7%BD%91%E5%9D%80%E7%BA%A0%E9%94%99.meta.js
// ==/UserScript==


var text1 = "您可能想要访问的网址可能是";
var text2 = ",是否需要跳转至推荐网址?";


//设置界面-头
(function() {
'use strict';

// 从存储中获取白名单,如果没有则默认为空数组
let whitelist = loadWhitelist();

// 注册一个菜单命令,用于打开设置界面
GM_registerMenuCommand("网址纠错白名单设置", openSettings);

function loadWhitelist() {
let storedWhitelist = GM_getValue('whitelist', '[]');
console.log('Loaded whitelist:', storedWhitelist);
return JSON.parse(storedWhitelist);
}

function saveWhitelist() {
let whitelistString = JSON.stringify(whitelist);
console.log('Saving whitelist:', whitelistString);
GM_setValue('whitelist', whitelistString);
}

function openSettings() {
// 创建一个包含设置界面的 div 元素
let settingsDiv = document.createElement('div');
settingsDiv.id = 'urlCorrectionSettings';
settingsDiv.style.position = 'fixed';
settingsDiv.style.top = '50%';
settingsDiv.style.left = '50%';
settingsDiv.style.transform = 'translate(-50%, -50%)';
settingsDiv.style.backgroundColor = 'white';
settingsDiv.style.padding = '20px';
settingsDiv.style.border = '1px solid black';
settingsDiv.style.zIndex = '9999';//层级设置,确保在最高层

// 创建输入框和按钮元素
let html = `

网址纠错白名单设置

操作说明

输入:点击“添加白名单网址:”后空白处可输入内容

添加:点击“添加”添加至白名单(添加后需要点“保存”)

保存:点击“保存”保存修改后的白名单

取消:点击“取消”不执行任何修改并关闭此窗口

删除:点击对应网址后的“删除”删除对应网址

添加白名单网址:


添加
保存
取消

当前白名单:

    `;
    settingsDiv.innerHTML = html;

    // 填充当前白名单列表
    let whitelistList = settingsDiv.querySelector('#whitelistList');
    for (let url of whitelist) {
    let listItem = document.createElement('li');
    listItem.textContent = url;
    listItem.innerHTML += ` 删除`;
    whitelistList.appendChild(listItem);
    }

    // 添加样式
    GM_addStyle(`
    #urlCorrectionSettings input, #urlCorrectionSettings button {
    margin: 5px;
    }
    #urlCorrectionSettings ul {
    list-style-type: none;
    padding: 0;
    }
    .removeButton {
    margin-left: 10px;
    }
    `);

    // 添加按钮点击事件
    settingsDiv.querySelector('#addButton').addEventListener('click', () => {
    let input = settingsDiv.querySelector('#whitelistInput');
    let url = input.value;
    if (url) {
    whitelist.push(url);
    let listItem = document.createElement('li');
    listItem.textContent = url;
    listItem.innerHTML += ` 删除`;
    whitelistList.appendChild(listItem);
    input.value = '';
    saveWhitelist(); // 保存白名单
    }
    });

    settingsDiv.querySelector('#saveButton').addEventListener('click', () => {
    saveWhitelist();
    closeSettings();
    });

    settingsDiv.querySelector('#cancelButton').addEventListener('click', () => {
    closeSettings();
    });

    // 为删除按钮添加事件监听
    settingsDiv.querySelectorAll('.removeButton').forEach(button => {
    button.addEventListener('click', () => {
    let listItem = button.parentNode;
    let url = listItem.textContent.replace('删除', '').trim();
    let index = whitelist.indexOf(url);
    if (index > -1) {
    whitelist.splice(index, 1);
    listItem.remove();
    saveWhitelist();
    alert("删除成功")
    }
    });
    });

    // 将设置界面添加到页面
    document.body.appendChild(settingsDiv);
    }

    function closeSettings() {
    let settingsDiv = document.getElementById('urlCorrectionSettings');
    if (settingsDiv) {
    document.body.removeChild(settingsDiv);
    }
    }
    //设置界面-尾

    // 使用对象存储错误网址和对应的正确网址
    let urlMap = {
    "bilibil.com": "https://bilibili.com",//b站
    "douying.com": "https://douyin.com",//抖音
    "baidv.com": "https://baidu.com",//百度
    "alibaba.com": "https://taobao.com",//阿里巴巴海外版转至淘宝
    "ta0bao.com": "https://taobao.com",//淘宝
    "1668.com": "https://1688.com",//阿里1688
    "sinq.com.cn": "https://sina.com.cn",//新浪网
    "168.com": "https://163.com",//网易
    "jdl.cn": "https://jd.com",//京东
    "wei6o.com": "https://weibo.com",//微博
    "iqiy1.com": "https://iqiyi.com",//爱奇艺
    //qq.com诱导性网站字典
    "v.qqq.com": "https://v.qq.com",
    "360123.com": "https://qq.com",
    "lo1.qq.com": "https://lol.qq.com",
    "pvp.qqq.com": "https://pvp.qq.com",
    "mp.weix1n.qq.com": "https://mp.weixin.qq.com",
    "qzoen.qq.com": "https://qzone.qq.com",
    "qql.com": "https://qq.com",
    //
    "youk1.com": "https://youku.com",//优酷
    "ba1du.com": "https://baidu.com",//百度
    "sogov.com": "https://sogou.com",//搜狗
    "sougou.com": "https://sogou.com",//搜狗
    "soo.com": "https://so.com",//360搜索
    "163.com/gamez": "https://163.com/games",//网易
    "www.icbc1.com.cn": "https://www.icbc.com.cn",//工商银行
    "www.cmbch1na.com": "https://www.cmbchina.com",//招商银行
    "www.al1pay.com": "https://www.alipay.com",//支付宝
    "59.com": "https://58.com",//58同城
    "ctripp.com": "https://ctrip.com",//携程旅行
    "www.dianp1ng.com": "https://www.dianping.com",//大众点评
    "wenku.ba1du.com": "https://wenku.baidu.com",//百度文库
    "blog.csd1.net": "https://blog.csdn.net",//流氓博客
    "zhih1.com": "https://zhihu.com",//知乎
    "tout1ao.com": "https://toutiao.com",//头条
    "lfeng.com": "https://ifeng.com",//凤凰网
    "thepaperr.cn": "https://thepaper.cn",//澎湃新闻
    "xiaote.com": "https://xiaote.data.blog",//小特网
    //Chrome诱导性网站字典
    "chrome.zuitie.cn": "https://www.google.cn/intl/zh-CN/chrome/",
    "www.chrome.net.cn": "https://www.google.cn/intl/zh-CN/chrome/",
    "http://chrome.stywru.cn/": "https://www.google.cn/intl/zh-CN/chrome/",
    "https://www.bear20.com/window/4211/472030921.html?f=bdj_695886": "https://www.google.cn/intl/zh-CN/chrome/",
    "https://chrome.cmrrs.com/": "https://www.google.cn/intl/zh-CN/chrome/",
    "https://baoku.360.cn/sinfo/104384025_4002818.html": "https://www.google.cn/intl/zh-CN/chrome/",
    "https://chromcn.cn/": "https://www.google.cn/intl/zh-CN/chrome/",
    "https://chrome.softbd.cn/": "https://www.google.cn/intl/zh-CN/chrome/",
    "http://soft.wxxznkj.cn/": "https://www.google.cn/intl/zh-CN/chrome/",
    "https://soft7.ydxiazai.com/pcgame/fps/764276.html": "https://www.google.cn/intl/zh-CN/chrome/",
    "https://sem.duba.net/sem/childbd/f336.html?sfrom=196&keyID=000017&TFT=3&msclkid=94b7b1bd86061e331528d6343bdc9af2": "https://www.google.cn/intl/zh-CN/chrome/",
    //可能需要访问Chrome下载地址的网址
    "https://cn.bing.com/search?q=%E8%B0%B7%E6%AD%8C%E6%B5%8F%E8%A7%88%E5%99%A8": "https://www.google.cn/intl/zh-CN/chrome/",
    "https://www.pcsoft.com.cn/article/61837.html": "https://www.google.cn/intl/zh-CN/chrome/",
    //
    "91.com": "https://xiaote.data.blog",
    "91.com": "https://xiaote.data.blog",
    "91.com": "https://xiaote.data.blog",
    "91.com": "https://xiaote.data.blog"
    // 可以继续添加更多的键值对
    };

    // 从 sessionStorage 中获取 hasCancelled 的状态,如果不存在则默认为 false
    let hasCancelled = sessionStorage.getItem('hasCancelled') === 'true';


    function checkAndCorrectUrl() {
    let currentUrl = window.location.href;
    for (let wrongUrl in urlMap) {
    let correctUrl = urlMap[wrongUrl];
    let currentUrlObj = new URL(currentUrl);
    let isInWhitelist = false;
    for (let whiteUrl of whitelist) {
    if (new URL(whiteUrl).href === currentUrlObj.href) {
    isInWhitelist = true;
    break;
    }
    }
    if (currentUrl.includes(wrongUrl) &&!hasCancelled &&!isInWhitelist) {
    if (confirm(`${text1} ${correctUrl}${text2}`)) {
    window.location.href = correctUrl;
    } else {
    hasCancelled = true;
    sessionStorage.setItem('hasCancelled', 'true');
    }
    }
    }
    }


    checkAndCorrectUrl();
    })();