Natives Messaging im Edge-Browser

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Natives Messaging im Edge-Browser

by Guest » 07 Jan 2025, 12:42

Code: Select all

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

Der Code ist manifest.json

Code: Select all

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
}
});
Der obige Code ist background.js

Code: Select all

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();
}
der obige Code content.js .
Der gesamte oben genannte Code funktioniert einwandfrei im Chrome-Browser (131.0.6778.205), aber nicht im Edge-Browser (131.0.6778.205). 2903.112). Kann ich jetzt herausfinden, wo der Fehler liegt? und ich habe eine Registrierung in der Registrierung im Pfad von

Code: Select all

Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Edge\NativeMessagingHosts\com.example.macaddress
.
Wenn der obige Code im Chrome-Browser gut funktioniert, aber nicht auf Edge, kann ich dann wissen, wo das Problem liegt?

Top