MCPcopy Create free account
hub / github.com/zxlie/FeHelper / _injectLogBridge

Function _injectLogBridge

apps/background/monkey.js:316–381  ·  view source on GitHub ↗
(tabId, allFrames, frameId)

Source from the content-addressed store, hash-verified

314// 通过 window.postMessage 接收 MAIN world 抛出的日志,再转发到 background。
315// 返回 Promise,调用方需 await 以保证桥接器在 user script 之前就位(否则启动期日志会丢)。
316const _injectLogBridge = (tabId, allFrames, frameId) => {
317 try {
318 return chrome.scripting.executeScript({
319 target: _buildTarget(tabId, allFrames, frameId),
320 func: function () {
321 if (window.__fhMonkeyBridgeReady) return;
322 window.__fhMonkeyBridgeReady = true;
323 window.addEventListener('message', function (e) {
324 if (!e || !e.data) return;
325 var d = e.data;
326 // 1) 日志桥
327 if (d.__fh_monkey_log === true) {
328 try {
329 chrome.runtime.sendMessage({
330 type: 'fh-dynamic-any-thing',
331 thing: 'page-monkey-log',
332 params: d.payload || {}
333 });
334 } catch (_) {}
335 return;
336 }
337 // 2) @require 代理:通过 background fetch 远程脚本,
338 // 避免页面 CSP/CORS 限制 MAIN world 直接 fetch
339 if (d.__fh_monkey_require === true && d.url && d.reqId) {
340 var reqId = d.reqId, url = d.url;
341 try {
342 chrome.runtime.sendMessage({
343 type: 'fh-dynamic-any-thing',
344 thing: 'page-monkey-require-fetch',
345 params: { url: url }
346 }, function (resp) {
347 try {
348 window.postMessage({
349 __fh_monkey_require_resp: true,
350 reqId: reqId,
351 ok: !!(resp && resp.ok),
352 text: (resp && resp.text) || '',
353 err: (resp && resp.err) || ''
354 }, '*');
355 } catch (_) {}
356 });
357 } catch (err) {
358 try {
359 window.postMessage({
360 __fh_monkey_require_resp: true,
361 reqId: reqId, ok: false, text: '',
362 err: String(err && err.message || err)
363 }, '*');
364 } catch (_) {}
365 }
366 }
367 }, false);
368 },
369 world: 'ISOLATED',
370 injectImmediately: true
371 }).catch((err) => {
372 log({
373 id: '__bridge__', name: 'page-monkey', level: 'error',

Callers 1

_injectScriptFunction · 0.85

Calls 3

_buildTargetFunction · 0.85
logFunction · 0.70
addEventListenerMethod · 0.45

Tested by

no test coverage detected