I am trying to send message from a dummy html page on my desktop directory to chrome extension host using chrome native messaging.
here is my host's json file:
{
"name": "com.myhost",
"description": "Chrome Native Messaging API Example Host",
"path": "myhost.exe",
"type": "stdio",
"allowed_origins": [
"chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/"
]
}
this json is registered under Computer\HKEY_CURRENT_USER\Software\Google\Chrome\NativeMessagingHosts\com.myhost
here is my extension's manifest.json file:
{
// Extension ID: knldjmfmopnpolahpmmgbagdohdnhkik
"key": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDcBHwzDvyBQ6bDppkIs9MP4ksKqCMyXQ/A52JivHZKh4YO/9vJsT3oaYhSpDCE9RPocOEQvwsHsFReW2nUEc6OLLyoCFFxIb7KkLGsmfakkut/fFdNJYh0xOTbSN8YvLWcqph09XAY2Y/f0AL7vfO1cuCqtkMt8hFrBGWxDdf9CQIDAQAB",
"name": "Native Messaging Example",
"version": "1.0",
"manifest_version": 2,
"description": "Send a message to a native application.",
"app": {
"launch": {
"local_path": "main.html"
}
},
"icons": {
"128": "icon-128.png"
},
"permissions": [
"nativeMessaging","tabs","webNavigation","system.display","*://dummy.html/*"
],
"background": {
"background_page": "background.html"
},
"externally_connectable": {
"matches": ["*://dummy.html/*"],
"ids": ["*"]
},
"content_scripts" : [{
"matches" : ["*://*/*"],
"js" : ["content_script.js"],
"run_at": "document_end"
}]
}
I am able to send and receive message from main.html page. However, I couldn't send any message from dummy.html file to extension host by using following commands:
chrome.runtime.sendMessage('knldjmfmopnpolahpmmgbagdohdnhkik', { command: 'heyyyy' },
function(response){
});
window.postMessage("knldjmfmopnpolahpmmgbagdohdnhkik", { text: "heyyy" });
When trying to communicate I get the following errors:
Port error: Could not establish connection. Receiving end does not exist.
Error in event handler for 'undefined': Cannot read property 'message' of undefined
TypeError: Cannot read property 'message' of undefined
any suggestions?
Thanks
Related
I'm trying to load an HTML file from the file system of my Chrome extension. I've added "modal.html" as a web accessible resource in my manifest:
{
"name": "Test Extension",
"action": {},
"manifest_version": 3,
"version": "0.1",
"description": "Just learning for now",
"permissions": [
"activeTab",
"scripting",
],
"background": {
"service_worker": "background.js"
},
"web_accessible_resources": [
{
"resources": [ "modal.html" ],
"matches": [ "https://mypage.co.uk/*" ]
}
]
}
My background.js file tries to load modal.html with an xhr:
function initPage() {
let node = document.createElement("div");
node.setAttribute("id", "mySpecialId");
document.body.appendChild(node);
var xhr = new XMLHttpRequest();
xhr.open("GET", chrome.runtime.getURL('modal.html'), true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
document.getElementById("mySpecialId").innerText = xhr.responseText;
}
}
xhr.send();
}
chrome.action.onClicked.addListener((tab) => {
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: initPage
});
});
I get the error:
Denying load of chrome-extension://lmdjgmkmficfccbahcpgnmaplajdljid/modal.html. Resources must be listed in the web_accessible_resources manifest key in order to be loaded by pages outside the extension.
I'm sure this is a really basic problem, but I just can't figure it out.
I got this working by adding the use_dynamic_url key in the web_accessible_resources declaration:
"web_accessible_resources": [
{
"resources": [ "modal.html" ],
"matches": [ "https://www.mypage.co.uk/*" ],
"use_dynamic_url": true
}
]
I don't yet understand why this worked though!
Edit: This was a bug in Chrome. The original code was correct and worked after a reboot.
Why is this function from Chrome not working? I'm trying this example:
https://developer.chrome.com/docs/extensions/mv3/content_scripts/#programmatic.
I'm developing an extension for chrome and sending a message from popup.js to service worker background.js and get error in executeScript.
popup.js
chrome.runtime.sendMessage({ from: "newScript"});
manifest.json
{
"manifest_version": 3,
"name": "TennisBet",
"version": "1.0",
"description": "Extrension for bet on tennis.",
"action": {
"default_icon": {
"256": "images/tennis256.png",
"128": "images/tennis128.png",
"64": "images/tennis64.png",
"32": "images/tennis32.png",
"24": "images/tennis24.png",
"16": "images/tennis16.png"
},
"default_popup": "popup/popup.html"
},
"background": {
"service_worker": "background-wrapper.js"
},
"host_permissions": ["*://*/*"],
"permissions": [
"tabs",
"alarms",
"activeTab",
"declarativeContent",
"storage"
]
}
background.js
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
switch(request.from){
case "error":
console.log(request.message);
break;
case "checkTabs":
tabsWorker();
break;
case "newScript":
chrome.scripting.executeScript({ // Error in event handler: TypeError: Cannot read property 'executeScript' of undefined
file: "content_scripts/cscript.js"
});
break;
default:
console.log("Message listener status active");
break;
}
});
The executeScript method in ManifestV3 has changed and is now in chrome.scripting API:
https://developer.chrome.com/docs/extensions/reference/scripting/
Add this line in manifest.json:
"permissions": ["scripting"]
background.js
chrome.scripting.executeScript({
target: {tabId: id, allFrames: true},
files: ['content_scripts/cscript.js'],
});
I am trying to send a message from my chrome extension popup window to the content script of the page, but the listener is not picking up the sent messages.
popup.js
chrome.runtime.sendMessage({ greeting: "hello" })
main.js (content script, part of the DOM)
The Listener is not picking up the sent messages:
chrome.runtime.onMessage.addListener(function (request) {
console.log("message received")
if (request.greeting == "hello") {
console.log("hello message")
}
})
manifest.json
{
"manifest_version": 3,
"name": "Go Go Ninja Computers",
"version": "0.54",
"description": "This extension is trying to send a message.",
"background": {
"scripts": [
"background.js"
],
"persistent": false
},
"permissions": [
"storage"
],
"content_scripts":
[
{
"matches": ["*://*/*"],
"js": ["main.js"],
"css" : ["style.css"],
"run_at": "document_end"
}
],
"browser_action": {
"default_title": "Ninja Pirate",
"default_popup": "popup.html"
}
}
I am just following these basic instructions, so I'm quite baffled why this isn't working? I'm also not sure if 'background.js' needs to be included, but without it, I cannot use console.log in popup.js.
I'm building a chrome extension which should connect to a console application.
This is my manifest file:
{
"name": "my extension",
"version": "0.0.1",
"manifest_version": 2,
"description": "my extension",
"icons": {
"16": "icons/icon16.png",
"48": "icons/icon48.png",
"128": "icons/icon128.png"
},
"default_locale": "en",
"permissions": [
"nativeMessaging"
],
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": [
"src/inject/inject.js"
]
}
]
}
I have this line in inject.js:
chrome.runtime.connectNative("org.my_company.my_application");
which throws a type error because connectNative is undefined, how could it be undefined when I have the permission for native messaging ? what should I do to solve the problem ?
By the way I've seen this question: Google Chrome Native Messaging Example returns: Uncaught TypeError: chrome.runtime.connectNative is not a function but the question is for chrome app and I'm building an extension.
my chrome version is 64 on windows 7 x64.
It turned out that native messaging isn't allowed in content scripts and should be done in a background page.
I'm attempting to follow the documentation here to pass a variable from my background script, to my content script.
http://code.google.com/chrome/extensions/messaging.html
Manifest:
{
"name": "name",
"description": "desc",
"version": "1.0",
"manifest_version": 2,
"content_scripts": [
{
"matches": [ "http://*/*", "https://*/*" ],
"js": ["content.js"]
}
],
"background": {
"scripts": ["background.js"]
},
"permissions": [
"tabs",
"http://*/*",
"https://*/*"
],
"options_page": "options.html",
"browser_action":
{
"default_icon": "icon.png",
"default_title": "Settings",
"default_popup": "settingspanel.html"
}
}
background.js file:
chrome.extension.onConnect.addListener(function(port) {
port.onMessage.addListener(function(msg) {
port.postMessage({counter: msg.counter+1});
});
});
content.js
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
sendResponse({counter: request.counter+1});
});
I receive an error on the background script that says "Cannot read property 'onConnect' of undefined"
Probably your problem is Issue #131623: Reloading extension while developer tools are open breaks chrome object and js console.
Until the fix gets through you have to close the Developer Tools before reloading the extension.