I'm trying to load the native messaging example app in an external webpage as follows:

test.html

<html>
<head>
</head>
<body>
<iframe src="chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/main.html"</iframe>
</body>    
</html>

I have added the web_accessible_resources property in the app's manifest:

manifest.json

{
  // <some code snipped>
  "web_accessible_resources": ["main.html"]
}

However, when I use the app within test.html, I get the following error in the console, coming from a JavaScript file within the app:

Developer Console

Uncaught TypeError: Object # has no method 'connectNative' main.js:51

The problematic line of code is the following:

main.js

port = chrome.runtime.connectNative(hostName);

Here's a screenshot - the problem happens when I click on the Connect button (which calls the connect function):

JavaScript Error: Uncaught TypeError: Object #<Object> has no method 'connectNative'

Obviously, this works when the app is run standalone. How can I go about loading the app in a webpage?

有帮助吗?

解决方案

You can't. With very few exceptions, Chrome avoids proprietary extensions to the open web. You're asking for exactly that: an external web page that has nonstandard abilities simply because it's opened in Chrome rather than another browser. As the web is today, that kind of behavior would lead to a poor developer and user experience. The native-messaging documentation suggests that the functionality you want is available to Chrome extensions and Chrome apps. And as you're finding out, that's indeed the case: it works as an app, but not as a web-standard iframe. This is by design.

You probably want to write a Chrome extension. Your webpage should detect whether the Chrome extension is installed and whether the user's browser is Chrome. If it's not Chrome, it should give an appropriate error message. If it is Chrome but the extension is not installed, it should urge the user to install your extension.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top