Edge浏览器Native Messaging功能失效问题排查求助
Edge浏览器Native Messaging功能失效问题排查求助
各位大佬好,我遇到一个头疼的问题:我开发的这个Manifest V3扩展在Chrome 131.0.6778.205上完全正常,但放到Edge 131.0.2903.112上就不行了,核心是Native Messaging这块没反应。我已经按照要求在Edge的注册表路径Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Edge\NativeMessagingHosts\com.example.macaddress配置了注册表项,但还是没效果,想请教大家可能哪里出问题了?
先把我的扩展代码贴出来,方便大家排查:
扩展代码文件
manifest.json
{ "manifest_version": 3, "name": "Mac Address", "version": "1.0", "description": "Mac Address", "permissions": [ "activeTab", "nativeMessaging", "tabs" ], "background": { "service_worker": "background.js" }, "content_scripts": [ { "matches": ["url of product"], "js": ["content.js"] } ], "host_permissions": [ "url of product" ], "icons": { "16": "icons/icon.png", "48": "icons/icon.png", "128": "icons/icon.png" } }
background.js
const nativeHostName = 'com.example.macaddress'; chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { if (request.action === "sendToNativeHost") { console.log("Sending message to native host:", request.data); chrome.runtime.sendNativeMessage(nativeHostName, request.data, (response) => { if (chrome.runtime.lastError) { console.error("Native message error:", chrome.runtime.lastError.message); sendResponse({ success: false, error: chrome.runtime.lastError.message }); } else { console.log("Received response from native host:", response); sendResponse({ success: true, response }); } }); return true; // Indicates that the response will be sent asynchronously } });
content.js
function monitorAndHandleClick() { const targetElement = document.querySelector('label#lblLogin.mybutton.btnPadding'); if (targetElement) { console.log("Target element found:", targetElement); targetElement.addEventListener('click', () => { const dataToSend = { action: "sendToNativeHost", data: { url: window.location.href, elementId: targetElement.id, timestamp: new Date().toISOString() } }; console.log("Sending message to background script:", dataToSend); chrome.runtime.sendMessage(dataToSend, (response) => { if (response && response.success) { console.log("Message sent to native host successfully."); } else { console.error("Failed to send message to native host:", response); } }); }); } else { console.error("Target element not found."); } } if (window.location.href === 'url of product') { console.log("Target URL matched, starting click monitoring."); monitorAndHandleClick(); }
已完成的配置
- 已经在Edge对应的注册表路径
Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Edge\NativeMessagingHosts\com.example.macaddress添加了注册表项,指向Native Host的配置文件。
我自己想到的几个排查方向,但还没找到问题,求大佬指点
- 注册表配置细节:Edge的Native Messaging注册表项有没有什么特殊要求?比如配置文件的路径是不是得用绝对路径?转义符有没有写错?
- Native Host配置文件兼容性:Chrome和Edge的Native Host配置文件会不会有差异?比如
allowed_origins字段是不是得包含Edge扩展的ID?毕竟Chrome和Edge的扩展ID不一样 - 权限或沙箱问题:Edge的安全策略会不会阻止Native Host启动?比如Native Host程序有没有足够的运行权限?或者Edge的沙箱限制了进程间通信?
- 控制台日志:我是不是漏看了Edge扩展背景页的控制台报错?比如
chrome.runtime.lastError有没有给出具体的错误信息? - 版本兼容性:Manifest V3在Edge 131这个版本有没有已知的Native Messaging相关bug?
备注:内容来源于stack exchange,提问作者Azure_Star
相关产品推荐
相关产品推荐

