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();
}
приведенный выше код content.js .
Весь приведенный выше код отлично работает в браузере Chrome (131.0.6778.205), но не на Edge (131.0.0.205). 2903.112). можно теперь где ошибка? и у меня есть регистрация в реестре по пути
[/code] код — Manifest.json [code]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 } }); [/code] приведенный выше код — background.js [code]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(); } [/code] приведенный выше код content.js . Весь приведенный выше код отлично работает в браузере Chrome (131.0.6778.205), но не на Edge (131.0.0.205). 2903.112). можно теперь где ошибка? и у меня есть регистрация в реестре по пути [code]Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Edge\NativeMessagingHosts\com.example.macaddress[/code] . когда приведенный выше код работает нормально в браузере Chrome, но не в Edge, могу ли я узнать, в чем проблема?