Регистрация хоста расширения Chrome не работает в Windows - PullRequest
1 голос
/ 11 октября 2019

Я хочу запустить консольное приложение, нажав кнопку на веб-странице. Консольное приложение получит информацию и поместит ее в буфер обмена, а затем я получу эту информацию на веб-странице. Я слежу за этим блогом
Я делал это 3-4 раза, все остальное выглядит хорошо, но консольное приложение не вызывается / не выполняется.

Я получаю эти ошибки.
на консоли веб-страницы
Unchecked runtime.lastError: The message port closed before a response was received.
в фоновом файле
Unchecked runtime.lastError: Specified native messaging host not found.
Unchecked runtime.lastError: The message port closed before a response was received.

мои кодыявляются

manifest.json

{
"name": "EID Reader",
"version": "1.0",
"manifest_version": 2,
"description": "Read Emirates ID",

"permissions": [ "contextMenus", "activeTab", "clipboardRead", "nativeMessaging" ],
"icons": {
  "16": "eid16.png",
  "48": "eid48.png",
  "128": "eid128.png"
},
"background": {
  "scripts": [ "eid.js" ]
},
"content_scripts": [
  {
    "matches": [ "http://*/*", "https://*/*", "file://*/*"],
    "js": [ "content_script.js", "jquery-3.3.1.js" ],
    "all_frames": true,
    "run_at": "document_start"
  }
]
}

content_script.js

    // Listener to catch the event raised by the webpage button
       document.addEventListener("EID_EVENT", function (data) {
       // send message to background process to read emirates ID and send back the data
       chrome.runtime.sendMessage("ifligfijbkpijeafdfbpljjibfbppmeb", function (response) {
       });
    });

// Listener to catch the data coming from the background process
chrome.extension.onMessage.addListener(function (msg, sender, sendResponse) {
    if (msg.action == 'EID_DATA') {
        //Parse the data and fill the form accordingly
        try {
            var json = $.parseJSON(msg.response);
            $(json).each(function (i, val) {
                $.each(val, function (key, value) {
                    if (key == 'EIDNumber')
                        $("#txtNumber").val(value);
                    if (key == 'Name')
                        $("#txtName").val(value);
                    if (key == 'Email')
                        $("#txtEmail").val(value);
                    if (key == 'PassportNumber')
                        $("#txtPassport").val(value);
                });
            });
        }
        catch (e) {
            var error = "error" + e;
        }
    }
});

eid.js (фон)

var port = null;
var tabId = null;
/* listener for messages coming from the content_scrip */
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
    tabId=sender.tab.id;
    var hostName = "ae.eid.chrome";
    port = chrome.runtime.connectNative(hostName);     
    port.onDisconnect.addListener(onDisconnected); 
});


/* THIS WILL BE CALLED ONCE EXE FINISH */
function onDisconnected() {

    port = null;
    SendResponse();
}

function SendResponse() {
    //create a textarea, focus on it, and make a "paste" command to get the clipboard, then send the pasted value back to the content_script
    bg = chrome.extension.getBackgroundPage();
    bg.document.body.innerHTML = ""; // clear the background page
    var helper = null;
    if (helper == null) {
        helper = bg.document.createElement("textarea");
        helper.style.position = "absolute";
        helper.style.border = "none";
        document.body.appendChild(helper);
    }

    //Focus the textarea
    helper.select();

    // perform a Paste in the selected control, here the textarea
    bg.document.execCommand("Paste"); 

    // Send data back to content_script
    chrome.tabs.sendMessage(tabId, { action: "EID_DATA", response: helper.value }, function (response) { });
}

ae.eid. chrome.json

{
"name": "ae.eid.chrome",
"description": "chrome extension to read EID",
"path": "EIDSampleConsole.exe",
"type": "stdio",
"allowed_origins": [
  "chrome-extension://ifligfijbkpijeafdfbpljjibfbppmeb/"
]
}

install_host.bat

REG ADD "HKCU\Software\Google\Chrome\NativeMessagingHosts\ae.eid.chrome" /ve /t REG_SZ /d "%~dp0ae.eid.chrome.json" /f

Я потратил 2 дня, но ничего не помогло.
Я делаю ошибку или Google Chrome предотвратил или изменил способзарегистрировать хост.

1 Ответ

0 голосов
/ 15 ноября 2019

Я решил все проблемы и опубликовал все шаги на

http://www.codingsips.com/emirates-id-reader-and-google-chrome-via-extension-and-console-app/

Также я опубликовал расширение Chrome, если вы выполните указанные выше шаги, то же расширение будет работать для вас

расширение хрома

https://chrome.google.com/webstore/detail/adcs-eid-reader/ipcncgpbppgjclagpdlodiiapmggolkf

...