8

मैं वर्तमान में क्रोम में टैब पर मौजूद सामग्री पृष्ठ में हेरफेर करना चाहता हूं और यदि यह नीचे नहीं कर सकता है तो मुझे रास्ता खोजने की आवश्यकता है वो करें!क्रोम नेटिव मैसेजिंग एपीआई chrome.runtime.connectNative एक फंक्शन नहीं है

हे सब मैं अपने सी # कार्यक्रम के साथ काम कर रहा हूं ताकि यह संदेश आगे और आगे पास हो सके। मैंने स्टैक ओवरफ्लो पर कोड के बहुत सारे डेमो देखे हैं और यह मुख्य रूप से मैं क्या कर रहा हूं लेकिन ऐसा लगता है कि सभी उदाहरण मेरे अंत में काम नहीं कर रहे हैं।

मुद्दा मैं कर रहा हूँ मैं की त्रुटि हो रही है है:

Connecting to native messaging host com.google.chrome.example.echo 
Uncaught TypeError: chrome.runtime.connectNative is not a function 

enter image description here

जब भी मैं बंदरगाह के लिए "कनेक्ट" की कोशिश करो।

सुनिश्चित नहीं हैं कि क्या मैं गलत कर रहा हूँ के बाद से मैं यहाँ पर अन्य tetorials का पालन किया है और वे सभी राज्य के लिए यह काम करता है लगता है ....

जे एस main.js:

var port = null; 
var getKeys = function (obj) { 
    var keys = []; 
    for (var key in obj) { 
     keys.push(key); 
    } 
    return keys; 
} 
function appendMessage(text) { 
    document.getElementById('response').innerHTML += "<p>" + text + "</p>"; 
} 
function updateUiState() { 
    if (port) { 
     document.getElementById('connect-button').style.display = 'none'; 
     document.getElementById('input-text').style.display = 'block'; 
     document.getElementById('send-message-button').style.display = 'block'; 
    } else { 
     document.getElementById('connect-button').style.display = 'block'; 
     document.getElementById('input-text').style.display = 'none'; 
     document.getElementById('send-message-button').style.display = 'none'; 
    } 
} 
function sendNativeMessage() { 
    message = { "text": document.getElementById('input-text').value }; 
    port.postMessage(message); 
    appendMessage("Sent message: <b>" + JSON.stringify(message) + "</b>"); 
} 
function onNativeMessage(message) { 
    appendMessage("Received message: <b>" + JSON.stringify(message) + "</b>"); 
} 
function onDisconnected() { 
    appendMessage("Failed to connect: " + chrome.runtime.lastError.message); 
    port = null; 
    updateUiState(); 
} 
function connect() { 
    var hostName = "com.google.chrome.example.echo"; 
    appendMessage("Connecting to native messaging host <b>" + hostName + "</b>") 
    console.log("Connecting to native messaging host " + hostName); 
    port = chrome.runtime.connectNative(hostName); 
    port.onMessage.addListener(onNativeMessage); 
    port.onDisconnect.addListener(onDisconnected); 
    updateUiState(); 
} 
document.addEventListener('DOMContentLoaded', function() { 
    document.getElementById('connect-button').addEventListener(
     'click', connect); 
    document.getElementById('send-message-button').addEventListener(
     'click', sendNativeMessage); 
    updateUiState(); 
}); 

manifest.json:

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

रजिस्ट्री:

REG ADD "HKCU\Software\Google\Chrome\NativeMessagingHosts\com.google.chrome.example.echo" /ve /t REG_SZ /d "%~dp0com.google.chrome.example.echo-win.json" /f 

सी # कोड:

using Newtonsoft.Json; 
using Newtonsoft.Json.Linq; 
using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Text; 

namespace talkWithChromeCSharp 
{ 
    class Program 
    { 
     public static void Main(string[] args) 
     { 
      JObject data; 
      while ((data = Read()) != null) 
      { 
       var processed = ProcessMessage(data); 
       Write(processed); 
       if (processed == "exit") 
       { 
        return; 
       } 
      } 
     } 

     public static string ProcessMessage(JObject data) 
     { 
      var message = data["message"].Value<string>(); 
      switch (message) 
      { 
       case "test": 
        return "testing!"; 
       case "exit": 
        return "exit"; 
       default: 
        return "echo: " + message; 
      } 
     } 

     public static JObject Read() 
     { 
      var stdin = Console.OpenStandardInput(); 
      var length = 0; 

      var lengthBytes = new byte[4]; 
      stdin.Read(lengthBytes, 0, 4); 
      length = BitConverter.ToInt32(lengthBytes, 0); 

      var buffer = new char[length]; 
      using (var reader = new StreamReader(stdin)) 
      { 
       while (reader.Peek() >= 0) 
       { 
        reader.Read(buffer, 0, buffer.Length); 
       } 
      } 

      return (JObject)JsonConvert.DeserializeObject<JObject>(new string(buffer))["data"]; 
     } 

     public static void Write(JToken data) 
     { 
      var json = new JObject(); 
      json["data"] = data; 

      var bytes = System.Text.Encoding.UTF8.GetBytes(json.ToString(Formatting.None)); 

      var stdout = Console.OpenStandardOutput(); 
      stdout.WriteByte((byte)((bytes.Length >> 0) & 0xFF)); 
      stdout.WriteByte((byte)((bytes.Length >> 8) & 0xFF)); 
      stdout.WriteByte((byte)((bytes.Length >> 16) & 0xFF)); 
      stdout.WriteByte((byte)((bytes.Length >> 24) & 0xFF)); 
      stdout.Write(bytes, 0, bytes.Length); 
      stdout.Flush(); 
     } 
    } 
} 

com.google.chrome.example.echo-win.json फ़ाइल:

{ 
    "name": "com.google.chrome.example.echo", 
    "description": "Chrome Native Messaging API Example Host", 
    "path": "native-messaging-example-host.bat", 
    "type": "stdio", 
    "allowed_origins": [ 
    "chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/" 
    ] 
} 

एचटीएमएल main.html:

<!DOCTYPE html> 

<html lang="en" xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <meta charset="utf-8" /> 
    <script src='main.js'></script> 
</head> 
<body> 
    <button id='connect-button'>Connect</button> 
    <input id='input-text' type='text' /> 
    <button id='send-message-button'>Send</button> 
    <div id='response'></div> 
</body> 
</html> 
,210

दृश्य स्टूडियो में मेरे निर्देशिका संरचना:

C:\Users\t||||||\Documents\Visual Studio 2012\Projects\talkWithChromeCSharp\talkWithChromeCSharp 
    -APP 
    |-icon-128.png 
    |-main.html 
    |-main.js 
    |-manifest.json 
    -bin 
    |-Debug 
     |-Newtonsoft.Json.dll 
     |-talkWithChromeCSharp.exe 
     |-etc etc... 
    |-Release 
    -obj 
    -Properties 
    -regs 
    |-com.google.chrome.example.echo-win.json 
    |-install_host.bat 
    |-etc etc... 

डिबग वी.एस. शुरू करने के बाद मैं प्लगइन स्थापित और क्रोम ब्राउज़र पर main.html फ़ाइल को लोड और "कनेक्ट" बटन पर क्लिक करें। वह तब होता है जब मुझे वह त्रुटि मिलती है।

मुझे क्या याद आ रही है?

अद्यतन

इसके लिए सही आईडी है। मैंने इसे इस तरह से रखा क्योंकि मैं "कुंजी" अनुमान लगा रहा हूं जो आईडी निर्धारित करता है। // apps/

+0

"" allowed_origins ": [ " क्रोम एक्सटेंशन: // knldjmfmopnpolahpmmgbagdohdnhkik/" ] "-> यह उदाहरण पृष्ठ से एक कॉपी पेस्ट है .. wha टी आपकी एक्सटेंशन आईडी है? सही एक – DannyZB

+0

@DanielBatkilin गलत का उपयोग करने का प्रयास करें। उस कुंजी के लिए यह सही आईडी है। – StealthRT

+0

@Tpepeemm मैंने टैग बदल दिया है। यह बात बताने के लिए धन्यवाद। – StealthRT

उत्तर

2

आप को खोलने के लिए main.html सीधे, बल्कि क्रोम पर Chrome के ऐप्स लॉन्चर से नहीं जा सकते!

https://chromium.googlesource.com/chromium/src/+/master/chrome/common/extensions/docs/examples/api/nativeMessaging/app/manifest.json

मुझे लगता है कि ग्रहण करेंगे जहां आप अपने प्रकट हो गया है। बदलें उचित एक

साथ और "allowed_origins" के बजाय उदाहरण से एक के अपने वास्तविक एक्सटेंशन आईडी का उपयोग में "कुंजी"

https://developer.chrome.com/apps/manifest/key

वहाँ अधिक त्रुटियों हो सकता है, लेकिन जो कर रहे हैं बस मैं पहली बार देखता हूं।

python "%~dp0/native-messaging-example-host" %* 

इसके बजाय, बैच फ़ाइल के रूप में बदलना चाहिए:

+1

हाँ - 'chrome: // apps /' से लॉन्च किया गया यह काम किया। लेकिन मैं इसे 'http: // localhost' या 'https: // myserver.com/sameapp' जैसे लॉन्च कैसे कर सकता हूं और उसी तरह से कनेक्ट कर सकता हूं? – YumYumYum

+1

मुझे पूरी तरह से यकीन नहीं है, लेकिन मुझे लगता है कि आप ऐप को क्रोम वेब स्टोर में जोड़ते हैं और लोग इसे स्थानीय रूप से इंस्टॉल करते हैं। – Chet

+0

यह समस्या है, यह कभी भी अच्छी तरह से प्रलेखित नहीं किया गया था कि इसे http: // या https: // से कैसे उपयोग किया जाए। मुझे chrome: // ऐप्स अवधारणा का उपयोग न करने का उपयोग करने की आवश्यकता है। – YumYumYum

1

आप सी # परियोजना है जो अपने उत्पादन के रूप में एक exe फाइल उत्पन्न उपयोग कर रहे हैं के बाद से, native-messaging-example-host.bat की फ़ाइल मूल उदाहरण है कि इस तरह sth था जैसे नहीं होना चाहिए निम्नानुसार:

@echo off 
Pushd C:\Users\h.aghajani\Desktop\host /*Path directory of your exe*/ 
start native-messaging-example-host.exe /*Name of the execution file*/ 
6

बहुत सारे भ्रम और अच्छी तरह से समझाया नहीं गया जो वास्तव में मेरे लिए काम करता है। इसलिए, यहां मैं `बेवकूफ सबूत 'दस्तावेज़ बनाने की कोशिश कर रहा हूं। (कृपया इस संस्करण में सुधार)

लक्ष्य: विंडोज ओएस, संस्करण 50 का परीक्षण जब तक गूगल क्रोम, मूल निवासी आवेदन करने के लिए संवाद


चरण 1:


डाउनलोड: https://developer.chrome.com/extensions/examples/api/nativeMessaging/app.zip

चरण 2:


लोड गूगल क्रोम करने के लिए डाउनलोड एप्लिकेशन

enter image description here

चरण 3:


क) रजिस्ट्री कुंजी

जोड़ने

REG ADD "HKLM\Software\Google\Chrome\NativeMessagingHosts\com.google.chrome.example.echo" /ve /t REG_SZ /d "C:\\run-my-exe\\manifest.json" /f

enter image description here

ख) एक कस्टम क्रोम निष्पादनकर्ता बनाने के लिए, सी में निम्नलिखित कॉपी: \ रन मेरी-एक्स \ रन क्रोम।बल्ला:

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --enable--native-messaging --native-messaging-hosts="com.google.chrome.example.echo=C:\\run-my-exe\\manifest.json"

चरण 4: होस्ट


क) पुट सी के लिए निम्न: \ रन मेरी-एक्स \ manifest.json

{ 
    "name": "com.google.chrome.example.echo", 
    "description": "Chrome Native Messaging API Example Host", 
    "path": "native-messaging-example-host.bat", 
    "type": "stdio", 
    "allowed_origins": [ 
    "chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/" 
    ] 
} 

बी) सी: \ run-my-exe \ native-messaging-examp के लिए निम्नलिखित डाल दिया le-host.bat

@echo off 
cd %windir%\system32 
start calc.exe 

चरण 5: कैसे मैं अब यह चला सकता हूँ?


क) इस स्क्रिप्ट के साथ खुला क्रोम: C:\\run-my-exe\\run-chrome.bat

ख) क्रोम में chrome://apps

ग) लांच

करने के लिए आइकन

के माध्यम से जाना

enter image description here

नहीं

नीचे के रूप में:

enter image description here

अंतिम आउटपुट:

enter image description here

+0

इन निर्देशों के लिए धन्यवाद। सबसे पहले यह मेरे लिए काम नहीं कर रहा था, लेकिन अब यह है। मैंने पहले ऐप की कोशिश की थी और वर्तमान उपयोगकर्ता के साथ-साथ स्थानीय मशीन में एक ही रजिस्ट्री कुंजी थी। वर्तमान उपयोगकर्ता कुंजी अमान्य थी। हटाने और फिर से चलने के बाद मेरे पास कैल्क पॉपिंग हो गया है। एक प्रश्न, क्या पैरामीटर के साथ exe को कॉल करने का कोई तरीका है? – JustAspMe

+2

@YumYumYum, ** लेकिन प्रतीक्षा करें **, क्या Google 2018 तक क्रोम से क्रोम ऐप्स को निकालने वाला नहीं है? Https://blog.chromium.org/2016/08/from-chrome-apps-to-web.html देखें, इसलिए दूसरे शब्दों में, यह भविष्य में और काम नहीं करेगा? मैंने भविष्य में शामिल करने के लिए अनुरोध करने के लिए https://docs.google.com/forms/d/e/1FAIpQLSenHdpA8_eqKiVOrWDjWe_KTfJmoSBEqFIh6SMwQ-NRDJnx1Q/viewform पर एक अनुरोध सबमिट कर दिया है, लेकिन हमें क्रोम टीम को बताते हुए अधिक लोगों की आवश्यकता है कि मूल संदेश जरूरी। – Pacerier

+0

नहीं - Google क्रोम से क्रोम ऐप्स को बंद करने देता है, वे ग्रह को डरा रहे हैं, समुदाय के रूप में हमें Google क्रोम पर शासन करना चाहिए, न कि Google क्रोम हमें शासन करना चाहिए। – YumYumYum

संबंधित मुद्दे