离线CSDN文章加载后自动跳转到CSDN官网的问题解决

现象描述:最近找到了几篇CSDN的离线博客,当时打开后隔了3秒左右就自动跳转到CSDN的官网;


原因排查:是html文件代码中设置了一段判断逻辑【当判断指定位置的图片加载失败时,会执行一段 onerror 里的代码,而这段 onerror 对应的代码逻辑正是:3秒后自动跳转到CSDN官网】


解决方案

1、单个文件:用类似 Sublime Text、NotePad....等,这类代码编辑器打开离线的html格式的博客文章,然后全文搜索关键词 "οnerrοr='setTimeout",你就会发现类似下面的这段代码:

javascript 复制代码
<div style="display:none;">
      <img src="" onerror='setTimeout(function(){if(!/(csdn.net|iteye.com|baiducontent.com|googleusercontent.com|360webcache.com|sogoucdn.com|bingj.com|baidu.com)$/.test(window.location.hostname)){window.location.href="\x68\x74\x74\x70\x73\x3a\x2f\x2f\x77\x77\x77\x2e\x63\x73\x64\x6e\x2e\x6e\x65\x74"}},3000);'>
</div>

果断删除上述这三行代码,然后保存。再次双击打开,就不会自动跳转到CSDN官网了!
2、批量文件:如果是批量的离线csdn文档的话,再按照上述的方法操作,会比较低效耗时,因此我将提供一段 python 代码,实现批量修改代码的操作:

python 复制代码
import os
import re
import argparse

def remove_redirect_code(html_content):
    """移除HTML内容中的跳转代码"""
    # 定义要匹配的跳转代码的正则表达式
    pattern = re.compile(
        r'<img\s+src=""\s+onerror=\'setTimeout\(function\(\)\{if\(!/\(csdn\.net|iteye\.com|baiducontent\.com|googleusercontent\.com|360webcache\.com|sogoucdn\.com|bingj\.com|baidu\.com\)$/.test\(window\.location\.hostname\)\)\{window\.location\.href="\\x68\\x74\\x74\\x70\\x73\\x3a\\x2f\\x2f\\x77\\x77\\x77\\x2e\\x63\\x73\\x64\\x6e\\x2e\\x6e\\x65\\x74"\}\},3000\);\'>',
        re.IGNORECASE
    )
    
    # 使用sub函数删除匹配到的代码
    modified_content = pattern.sub('', html_content)
    
    # 如果内容有修改,返回True和修改后的内容
    if modified_content != html_content:
        return True, modified_content
    return False, html_content

def process_html_files(directory):
    """处理目录下的所有HTML文件"""
    # 检查目录是否存在
    if not os.path.exists(directory):
        print(f"错误:目录 '{directory}' 不存在")
        return
    
    # 遍历目录及其子目录中的所有文件
    for root, _, files in os.walk(directory):
        for file in files:
            # 只处理.html和.htm文件
            if file.lower().endswith(('.html', '.htm')):
                file_path = os.path.join(root, file)
                
                try:
                    # 读取文件内容
                    with open(file_path, 'r', encoding='utf-8') as f:
                        content = f.read()
                    
                    # 移除跳转代码
                    modified, new_content = remove_redirect_code(content)
                    
                    # 如果内容有修改,则写回文件
                    if modified:
                        with open(file_path, 'w', encoding='utf-8') as f:
                            f.write(new_content)
                        print(f"已修改: {file_path}")
                    else:
                        print(f"未修改: {file_path}")
                except Exception as e:
                    print(f"处理文件 {file_path} 时出错: {str(e)}")

def main():
    """主函数,解析命令行参数并执行处理"""
    parser = argparse.ArgumentParser(description='移除HTML文件中的CSDN跳转代码')
    parser.add_argument('directory', help='要处理的目录路径')
    args = parser.parse_args()
    
    process_html_files(args.directory)

if __name__ == "__main__":
    main()

这段 python 代码使用方法也比较简单,只要输入以下命令行即可(主要替换内容):

bash 复制代码
# 将下面命令行中【/path/to/your/html/files】替换成【自己本地存放批量csdn博客文档的目录】
# 例如:python remove_csdn_redirect.py  ./所有csdn博客文章目录
python remove_csdn_redirect.py   /path/to/your/html/files