mirror of
https://github.com/mdn/webextensions-examples.git
synced 2026-04-16 06:18:35 +02:00
Check config on Windows (#292)
* add in a basic Windows config check script * feedback from wbamberg
This commit is contained in:
@@ -21,6 +21,8 @@ To get this working, there's a little setup to do.
|
||||
3. Edit "ping_pong_win.bat" to refer to the location of "ping_pong.py" on your computer.
|
||||
4. Add a registry key containing the path to "ping_pong.json" on your computer. See [App manifest location ](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Native_messaging#App_manifest_location) to find details of the registry key to add.
|
||||
|
||||
To assist in troubleshooting on Windows, there is a script called `check_config_win.py`. Running this from the command line should give you an idea of any problems.
|
||||
|
||||
## Testing the example ##
|
||||
|
||||
Then just install the add-on as usual, by visiting about:debugging, clicking "Load Temporary Add-on", and selecting the add-on's "manifest.json".
|
||||
@@ -29,5 +31,5 @@ You should see a new browser action icon in the toolbar. Open the console ("Tool
|
||||
|
||||
Sending: ping
|
||||
Received: pong
|
||||
|
||||
|
||||
If you don't see this output, see the [Troubleshooting guide](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Native_messaging#Troubleshooting) for ideas.
|
||||
|
||||
64
native-messaging/check_config_win.py
Executable file
64
native-messaging/check_config_win.py
Executable file
@@ -0,0 +1,64 @@
|
||||
'''
|
||||
A Python script that attempts to check that the configuration of your
|
||||
nativeMessaging app is set up correctly
|
||||
|
||||
Currently requires Python 3.
|
||||
|
||||
If you find more issues with setting this up, let's see if we can add to this
|
||||
script.
|
||||
'''
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import winreg
|
||||
|
||||
key_path = 'Software\\Mozilla\\NativeMessagingHosts\\ping_pong'
|
||||
# Assuming current user overrides local machine.
|
||||
key_roots = ['HKEY_CURRENT_USER', 'HKEY_LOCAL_MACHINE']
|
||||
|
||||
found_key = False
|
||||
|
||||
for root in key_roots:
|
||||
key = winreg.OpenKey(getattr(winreg, root), key_path)
|
||||
try:
|
||||
print('Checking:', root, key_path)
|
||||
res = winreg.QueryValueEx(key, '')
|
||||
except FileNotFoundError:
|
||||
print('...error finding key')
|
||||
continue
|
||||
|
||||
found_key = True
|
||||
break
|
||||
|
||||
if not found_key:
|
||||
raise ValueError('Could not find a registry entry, aborting.')
|
||||
|
||||
json_path = res[0]
|
||||
print('Path from registry key is:', json_path)
|
||||
if not os.path.exists(json_path):
|
||||
raise ValueError('JSON file does not exist:', json_path)
|
||||
|
||||
try:
|
||||
bat_data = json.load(open(json_path, 'r'))
|
||||
except json.decoder.JSONDecodeError:
|
||||
raise ValueError('Parsing error. Is {} a JSON file?'.format(json_path))
|
||||
|
||||
bat_path = bat_data['path']
|
||||
print('Path from JSON is:', bat_path)
|
||||
if not os.path.exists(bat_path):
|
||||
raise ValueError('.bat does not exist:', bat_path)
|
||||
|
||||
py_lines = open(bat_path, 'r').readlines()
|
||||
py_path = None
|
||||
for line in py_lines:
|
||||
if line.startswith('call python '):
|
||||
py_path = line[12:].replace('\\\\', '\\').strip()
|
||||
|
||||
if not py_path:
|
||||
raise ValueError('No python script in the batch file.')
|
||||
|
||||
print('Path from batch file is:', py_path)
|
||||
if not os.path.exists(py_path):
|
||||
raise ValueError('Python file does not exist:', py_path)
|
||||
|
||||
print('Looks good! Give it a try from Firefox.')
|
||||
Reference in New Issue
Block a user