You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Chrome扩展获取的URL保存到本地电脑的文本文件?

解决Chrome扩展保存URL到本地文本文件的方案

方案1:直接生成新文本文件下载(最简单,无需本地程序)

这个方法不用额外写本地脚本,靠Chrome自带的下载API就能实现URL追加,适合快速上手:

    1. 在扩展的popup.js或background.js里添加保存逻辑:
// 点击按钮触发的函数
function saveUrlToFile() {
  chrome.tabs.query({active: true, currentWindow: true}, tabs => {
    const currentUrl = tabs[0].url;
    // 把URL转成Blob格式
    const blob = new Blob([currentUrl + '\n'], {type: 'text/plain'});
    const blobUrl = URL.createObjectURL(blob);
    // 调用下载API,指定文件名和追加模式
    chrome.downloads.download({
      url: blobUrl,
      filename: 'saved_urls.txt',
      conflictAction: 'append' // 已有文件就追加内容
    }, downloadId => {
      if (chrome.runtime.lastError) {
        console.error(chrome.runtime.lastError);
      } else {
        console.log('URL已追加到文件');
      }
    });
  });
}
    1. 在manifest.json里添加必要权限:
{
  "permissions": ["tabs", "downloads"]
}
  • 注意:文件会存在Chrome默认下载目录,要改路径的话得手动调整Chrome的下载设置。

方案2:用Native Messaging追加到指定本地文件(自定义路径)

如果要把URL写到自己指定的文件夹里,就得用Native Messaging,下面是简化的新手版步骤:

步骤1:写本地Python脚本(简单易读)

创建url_saver.py文件,内容如下:

import sys
import json
import os

def main():
    # 读取Chrome发来的消息
    input_data = sys.stdin.readline()
    data = json.loads(input_data)
    url = data.get('url')
    
    # 替换成你想要保存的文件路径
    save_path = "C:/Users/你的用户名/Documents/saved_urls.txt"
    
    # 追加写入URL,每次加一行
    with open(save_path, 'a', encoding='utf-8') as file:
        file.write(url + '\n')
    
    # 给Chrome返回成功响应
    response = json.dumps({"status": "success"})
    sys.stdout.write(response + '\n')
    sys.stdout.flush()

if __name__ == "__main__":
    main()

步骤2:配置Native Messaging清单文件

创建com.yourname.urlsaver.json(文件名要和后面调用的一致):

{
  "name": "com.yourname.urlsaver",
  "description": "Save URLs to local file",
  "path": "C:/你放脚本的路径/url_saver.py",
  "type": "stdio",
  "allowed_origins": ["chrome-extension://你的扩展ID/"]
}
  • 怎么找扩展ID:打开Chrome扩展管理页(chrome://extensions/),开启开发者模式,就能看到你的扩展ID,替换上面的你的扩展ID。
  • 把这个配置文件放到对应系统的目录:
    • Windows:打开注册表编辑器,创建项HKEY_LOCAL_MACHINE\SOFTWARE\Google\Chrome\NativeMessagingHosts\com.yourname.urlsaver,把默认值设为配置文件的完整路径。
    • macOS:放到~/Library/Application Support/Google/Chrome/NativeMessagingHosts/目录下。
    • Linux:放到~/.config/google-chrome/NativeMessagingHosts/目录下。

步骤3:扩展里调用Native Messaging

在popup.js里添加调用逻辑:

function saveUrlViaNative() {
  chrome.tabs.query({active: true, currentWindow: true}, tabs => {
    const currentUrl = tabs[0].url;
    // 发送URL给本地脚本
    chrome.runtime.sendNativeMessage('com.yourname.urlsaver', {url: currentUrl}, response => {
      if (chrome.runtime.lastError) {
        console.error(chrome.runtime.lastError);
      } else {
        console.log('URL已保存到指定文件');
      }
    });
  });
}
  • 在manifest.json里添加权限:
{
  "permissions": ["tabs", "nativeMessaging"]
}

方案3:剪贴板+AutoHotkey自动写入(零编程门槛)

如果觉得上面的方法还是复杂,可以用剪贴板+AutoHotkey的组合,完全不用写复杂的扩展逻辑:

    1. 扩展里只做一件事:把当前页面URL复制到剪贴板
function copyUrlToClipboard() {
  chrome.tabs.query({active: true, currentWindow: true}, tabs => {
    const currentUrl = tabs[0].url;
    navigator.clipboard.writeText(currentUrl + '\n').then(() => {
      // 可以加个通知提示
      chrome.notifications.create({
        type: 'basic',
        iconUrl: 'icon.png',
        title: 'URL已复制',
        message: '按Ctrl+Shift+S写入文件'
      });
    });
  });
}
    1. 下载安装AutoHotkey(免费工具),创建url_paster.ahk脚本:
; 监听Ctrl+Shift+S快捷键,自动把剪贴板内容追加到文件
^+s::
FileAppend, %Clipboard%, C:\Users\你的用户名\Documents\saved_urls.txt, UTF-8
MsgBox, URL已写入文件
return
    1. 运行这个脚本,之后在Chrome里点击扩展按钮复制URL,再按Ctrl+Shift+S,就能自动把URL写到指定文件里。

内容的提问来源于stack exchange,提问作者SebastianCoder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 15:55:27