ZBLOG

如果不是blob URL,就是一个常规URL,如何做呢?

如果 Worker 是通过常规 URL(而非 Blob URL)加载的脚本,可以通过以下方法 Hook 其中的 fetch 函数。核心思路是 拦截脚本请求并动态注入代码,或通过 Service Worker 劫持网络请求


方法 1:拦截脚本请求并修改(Man-in-the-Middle)

适用于你能控制 Worker 的加载过程(例如在开发环境或浏览器扩展中)。

步骤

  1. 覆写 Worker 构造函数,拦截目标 URL。
  2. fetch 获取原始脚本,并在其中注入 Hook。
  3. 生成新的 Blob URL,传递给原始 Worker

代码实现

  1. // 保存原始的 Worker
  2. const OriginalWorker = window.Worker;
  3. // 覆写 Worker
  4. window.Worker = function (scriptURL) {
  5. if (typeof scriptURL === 'string' && scriptURL.includes('your-target-script.js')) {
  6. // Step1: Fetch the original worker script
  7. fetch(scriptURL)
  8. .then((res) => res.text())
  9. .then((scriptText) => {
  10. // Step2: Inject the fetch hook
  11. const hookedScript = `
  12. // Override fetch
  13. const originalFetch = window.fetch;
  14. window.fetch = function (...args) {
  15. console.log("[Hooked Fetch] Request:", args);
  16. // Add your custom logic here (e.g., modify requests/responses)
  17. return originalFetch.apply(this, args);
  18. };
  19. // Original worker code below
  20. ${scriptText}
  21. `;
  22. // Step3: Create a new Blob URL for the modified script
  23. const blob = new Blob([hookedScript], { type: "application/javascript" });
  24. const hookedBlobUrl = URL.createObjectURL(blob);
  25. // Step4: Initialize the Worker with the hooked version
  26. return new OriginalWorker(hookedBlobUrl);
  27. });
  28. } else {
  29. // Fallback to normal behavior for other URLs
  30. return new OriginalWorker(scriptURL);
  31. }
  32. };

适用场景

  • 适用于你能够控制页面逻辑的情况(如调试、安全测试)。
  • 可用于 Chrome Extensions(需声明 webRequest 权限)。

限制

  • CSP(内容安全策略)可能阻止修改后的脚本执行
  • 无法用于 HTTPS + Strict CSP 网站

方法 2:使用 Service Worker 全局劫持 Fetch

如果你不能修改 Worker,但可以注册一个 Service Worker,则可以全局拦截所有 fetch(包括 Worker)。

步骤

  1. 注册 Service Worker(作用域必须覆盖目标 Worker)。
  2. 监听 fetch 事件并篡改请求/响应

代码实现

(1) Main Thread - Register Service Worker

  1. if ('serviceWorker' in navigator) {
  2. navigator.serviceWorker.register('/sw-proxy.js', { scope: '/' })
  3. .then(() => console.log('SW Registered!'));
  4. }

(2) sw-proxy.js - Intercept Fetch

  1. self.addEventListener('fetch', (event) => {
  2. const url = event.request.url;
  3. // Only intercept specific requests (optional)
  4. if (url.includes('api.example.com')) {
  5. console.log('[SW] Intercepted fetch:', url);
  6. // Optionally modify request/response:
  7. event.respondWith(
  8. fetch(event.request)
  9. .then((response) => {
  10. console.log('[SW] Fetched:', response);
  11. return response;
  12. })
  13. );
  14. }
  15. });

适用场景

  • Works even if you can’t modify the original Worker.
  • Can inspect/modify all network requests from Workers.

限制

  • Requires HTTPS (or localhost in dev).
  • Cannot directly modify the JS code inside Workers, only their network calls.

方法3:Chrome扩展 + Debugger API(高级方案)

如果上述方法受限,可以使用 Chrome Debugger API (需要扩展权限):

  1. chrome.debugger.attach({ tabId }, "1.0", () => {
  2. chrome.debugger.sendCommand(
  3. { tabId },
  4. "Debugger.evaluateOnCallFrame",
  5. {
  6. callFrameId,
  7. expression: `
  8. window.fetch = function(...args) {
  9. console.log('Hooked!', args);
  10. return originalFetch(...args);
  11. };
  12. `
  13. }
  14. );
  15. });

⚠️ Requires debugger permission and user approval.


总结对比

方法 可行性 侵入性 适用场景
MITM Script Injection ★★★★☆ High Dev tools / Extensions
Service Worker Proxy ★★★☆☆ Medium All pages under SW scope
Chrome Debugger API ★★☆☆☆ Low Advanced debugging

选择最适合你的方式!如果有更多细节需求,可以进一步优化方案。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=23178

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?