0

I try to make simple chrome extension that takes active directory username from input field and send to native app - powershell script - that use active directory module to find username and return his phone and email in JSON. Now i have error: "Unchecked runtime.lastError: Error when communicating with the native messaging host." All the code is below.

manifest.json
 {
   "name": "AD Extension",
   "description": "Get phone and email from AD",
   "version": "1.0",
   "manifest_version": 3,
   "icons": {},
   "permissions": [
     "nativeMessaging",
     "activeTab"
   ],
   "host_permissions": [
     "https://*/*",
     "http://*/*"
   ],
   "action": {
     "default_popup": "popup.html"
   },
   "background": {
     "service_worker": "background.js"
   }
 }
popup.html
<!DOCTYPE html>
<html>
  <head>
    <title>AD Extension</title>
    <script defer src="background.js"></script>
  </head>
  <body>
    <label for="username">Enter AD username:</label>
    <input type="text" id="username" name="username"><br>
    <button id="get-info">Get Phone and Email</button>
    <div id="info"></div>
  </body>
</html>
popup.js
document.addEventListener("DOMContentLoaded", function () {
const button = document.getElementById("get-info");
button.addEventListener("click", function () {
const username = document.getElementById("username").value;
chrome.runtime.sendNativeMessage(
            "com.smm.adextension",
command: "get_user_info", username: username },
function (response) {
//Parse the JSON output into a JavaScript object
const outputObj = JSON.parse(response);
console.log(outputObj);
//Access the phone and email properties of the output object
const phone = outputObj.phone;
const email = outputObj.email;
console.log(phone, email);
const infoDiv = document.getElementById("info");
console.log(infoDiv);
infoDiv.innerHTML = "Phone: " + phone + "<br>Email: " + email;
            }
        );
    });
});
background.js
const port = chrome.runtime.connectNative('com.smm.adextension');
port.onMessage.addListener(function(request, sender, sendResponse) {
if (request.command === "get_user_info") {
chrome.runtime.sendNativeMessage(
        "com.smm.adextension",
command: "get_user_info" },
function(response) {
console.log(response);
sendResponse(response);
        }
      );
    }
return true;
  });
nativeapp.json
{
    "name": "com.smm.adextension",
    "description": "Native messaging app for AD Extension",
    "path": "./nativeApp.ps1",
    "type": "stdio",
    "allowed_origins": [
      "chrome-extension://dhfgiijbgbcpjinfhhhlniaegdjpldbm/"
    ]
  }
nativeapp.ps1
//Import the ActiveDirectory module
Import-Module ActiveDirectory

//Retrieve the phone number and email address for the specified user
$user = Get-ADUser -Identity $args[0] -Properties telephoneNumber, emailAddress

$phone = $user.telephoneNumber

$email = $user.emailAddress

//Create a PowerShell object containing the phone and email values
$output = @{ phone = $phone; email = $email }


//Convert the object to JSON format and write it to standard output
$outputJson = ConvertTo-Json $output
Write-Output $outputJson

I added path to native app with its name in registry. allowed_origins field in the nativeApp.json file matches the ID of the extension.

  • nativeMessaging doesn't pass arguments, it uses stdin/stdout pipes, see the documentation for the exact description of the protocol. In PowerShell you'll probably have to use some special class to access the pipes in binary mode. – wOxxOm Mar 17 '23 at 22:59
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Mar 18 '23 at 12:48

0 Answers0