V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
Get Google Chrome
Vimium · 在 Chrome 里使用 vim 快捷键
rateltalk
V2EX  ›  Chrome

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

  •  
  •   rateltalk · Apr 30, 2024 · 2136 views
    This topic created in 727 days ago, the information mentioned may be changed or developed.

    查到的几乎都是使用 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 replies    2024-04-30 10:15:36 +08:00
    zivW
        1
    zivW  
       Apr 30, 2024
    之前 MV3 刚出时 是通过在目标网站注入脚本 hook 掉 window.XMLHttpRequest 实现的,不知道现在有没有更好的方法
    you222
        2
    you222  
       Apr 30, 2024   ❤️ 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  
       Apr 30, 2024
    基于 webrequest 的好像不行。如果是基于 Tab 做好像可以,指定 tabid 对页面注入 js 脚本来获取这个 tab 下的原始 html
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4956 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 09:53 · PVG 17:53 · LAX 02:53 · JFK 05:53
    ♥ Do have faith in what you're doing.