1
{
  "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"
  }
}


the code is manifest.json

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
  }
});

the above code is background.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();
}

the above code content.js .

All above code is working fine on chrome browser(131.0.6778.205) but not on edge (131.0.2903.112). can i now where the mistake is? and i have reg on registry in path of

Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Edge\NativeMessagingHosts\com.example.macaddress .

when the Above code is Working fine on chrome browser but not on Edge can i Know where the problem is?

2
  • Are you sure the file is executable? Commented Apr 1 at 1:43
  • Yes, the file is executable Commented Apr 2 at 4:06

1 Answer 1

0

I suggest referring to Debugging native messaging to debug your extension/host. You can add

--enable-logging=stderr --v=1

as a command line option when you launch Microsoft Edge. After you find out the error, you can follow the common errors and tips under the same section.

If any, you can also share your error messages with us if you are not sure how to deal with them.

Sign up to request clarification or add additional context in comments.

9 Comments

Uncaught TypeError: Cannot read properties of undefined (reading 'sendMessage') Context <url> Stack Trace content.js:16 (anonymous function) chrome.runtime.sendMessage(dataToSend, (response) => {
Native message error: Specified native messaging host not found. Failed to send message to native host: { "success": false, "error": "Specified native messaging host not found." }
@Azure_Star This native message error is listed in the documentation. Have you already checked the tips?
Yes tried All Method But still can't?
@Azure_Star Can I have a check of the manifest file of your native app host?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.