V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Get Google Chrome
Vimium · 在 Chrome 里使用 vim 快捷键
s609926202
V2EX  ›  Chrome

Chrome 插件 v3 如何获取源站指定网络请求的响应内容?

  •  
  •   s609926202 · 17 天前 · 425 次点击

    查到的几乎都是使用 webRequest ,但是这个在 v3 中好像已弃用:

    chrome.webRequest.onCompleted.addListener(
        function(details) {
            console.log("Request completed: ", details);
        },
        {urls: ["<all_urls>"]}
    );

    目的是不用通过 dom 抓取内容,通过请求的响应内容能直接获取到更完善的数据,DOM 也是通过响应内容渲染的。

    用到了号称插件界的 nextjs 脚手架: https://github.com/PlasmoHQ/plasmo

    3 条回复    2024-04-30 10:15:36 +08:00
    zivW
        1
    zivW  
       17 天前
    之前 MV3 刚出时 是通过在目标网站注入脚本 hook 掉 window.XMLHttpRequest 实现的,不知道现在有没有更好的方法
    you222
        2
    you222  
       17 天前   ❤️ 1
    可以试下 chrome.debugger ,不过需要开启浏览器的调试模式

    chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
    const onEvent = async (source, method, params) => {
    if (method === "Network.responseReceived") {
    const { type, response } = params
    console.log(response.body)
    }
    }
    chrome.debugger.onEvent.addListener(onEvent)
    })
    wnck
        3
    wnck  
       17 天前
    基于 webrequest 的好像不行。如果是基于 Tab 做好像可以,指定 tabid 对页面注入 js 脚本来获取这个 tab 下的原始 html
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   4138 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 05:15 · PVG 13:15 · LAX 22:15 · JFK 01:15
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.