How to make a Chrome extension communicate with AHK
-
- Posts: 46
- Joined: 06 May 2017, 13:38
How to make a Chrome extension communicate with AHK
I'm trying to come up with a script that will stop executing once it receives some kind of behind the scene notification from a Google Chrome extension. Does anyone have any idea how would this behind the scene notification be done? How would you make the Chrome extension communicate with AHK?
Re: How to make a Chrome extension communicate with AHK
As I understand, a Chrome extension is JS and I believe browsers in general limits the interaction of JS with the computer at large, however you should be able to store a cookie in certain folders, in AHK you can look for the file and act on it.
Please excuse my spelling I am dyslexic.
Re: How to make a Chrome extension communicate with AHK
I remember converting a simple Python Firefox native messaging example that seems to work pretty much as-is for Chrome.
You should read the following for needed background information:
https://developer.chrome.com/extensions/nativeMessaging
https://developer.mozilla.org/en-US/Add ... _messaging
Here's a very simple Chrome extension that will send the URL of a tab to an AutoHotkey script which the script will then send back to Chrome. (Rather unimaginative, I know, but I can't write JS to save my life And while I'm on the topic, none of this is well written, but it should serve as enough of an example.)
Extract 1.zip (attached) into C:\
You should end up with a directory structure like this:
Inside C:\1, right-click ping_pong.ahk and compile it into an exe. (I was going under the assumption that the AutoHotkey script would need to run under the console subsystem but, unlike in Firefox, this seemingly isn't needed for Chrome so you might want to consider changing host-manifest.json to start AutoHotkey.exe with ping_pong.ahk as the second argument instead.)
Hit Win+R and run chrome --user-data-dir=%TEMP%\tmpChromeProfile (you can, of course, do this with your main Chrome profile but it's easier to test IMHO in another Chrome session).
Open the Extensions page, enable developer mode and load an unpacked extension: C:\1\ext
Inspect the background page of the Native Messaging example extension and if all went well, you should see something like the following when you change pages:
Again, the URL shown in the Chrome console has been sent to it by the AutoHotkey script, after getting it from Chrome itself. If ping_pong.exe isn't started by Chrome, make sure the Registry file has been imported, the script has been compiled and that the extension ID of the one we loaded into Chrome is present in host-manifest.json.
If you want to be able to use your extension without enabling developer mode, install the Chrome group policy templates and whitelist your extension ID in gpedit.msc - you can then install a packed crx of it by dragging it into Chrome. This has the advantage of you not having to see the annoying developer mode prompt on each start of Chrome.
Chrome can only message the script that it itself starts. If you need to talk to another running script, look into the examples given for OnMessage or use Lexikos' ObjRegisterActive.
You should read the following for needed background information:
https://developer.chrome.com/extensions/nativeMessaging
https://developer.mozilla.org/en-US/Add ... _messaging
Here's a very simple Chrome extension that will send the URL of a tab to an AutoHotkey script which the script will then send back to Chrome. (Rather unimaginative, I know, but I can't write JS to save my life And while I'm on the topic, none of this is well written, but it should serve as enough of an example.)
Extract 1.zip (attached) into C:\
You should end up with a directory structure like this:
Import the following into the Registry. (And remember to delete the key when you're done with all this.).
C:\1\ext
C:\1\ext\background.js
C:\1\ext\manifest.json
C:\1\ext\host-manifest.json
C:\1\Jxon.ahk - this is by Coco, originally obtained from https://github.com/cocobelgica/AutoHotk ... r/Jxon.ahk
C:\1\ping_pong.ahk
Code: Select all
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Google\Chrome\NativeMessagingHosts\pk.qwerty12.ahkhrome]
@="C:\\1\\host-manifest.json"
Hit Win+R and run chrome --user-data-dir=%TEMP%\tmpChromeProfile (you can, of course, do this with your main Chrome profile but it's easier to test IMHO in another Chrome session).
Open the Extensions page, enable developer mode and load an unpacked extension: C:\1\ext
Inspect the background page of the Native Messaging example extension and if all went well, you should see something like the following when you change pages:
Again, the URL shown in the Chrome console has been sent to it by the AutoHotkey script, after getting it from Chrome itself. If ping_pong.exe isn't started by Chrome, make sure the Registry file has been imported, the script has been compiled and that the extension ID of the one we loaded into Chrome is present in host-manifest.json.
If you want to be able to use your extension without enabling developer mode, install the Chrome group policy templates and whitelist your extension ID in gpedit.msc - you can then install a packed crx of it by dragging it into Chrome. This has the advantage of you not having to see the annoying developer mode prompt on each start of Chrome.
Chrome can only message the script that it itself starts. If you need to talk to another running script, look into the examples given for OnMessage or use Lexikos' ObjRegisterActive.
- Attachments
-
- 1.zip
- Extension and uncompiled native host
- (4.19 KiB) Downloaded 336 times
Who is online
Users browsing this forum: AlFlo, captainsmackyou, mikeyww and 65 guests