1010
1111HOST_FILENAME = "streamkeys_mpris.py"
1212HOST_MANIFEST_FILENAME = "org.mpris.streamkeys_host.json"
13- HOST_MANIFEST = {
13+ CHROME_HOST_MANIFEST = {
1414 "name" : "org.mpris.streamkeys_host" ,
1515 "description" : "Streamkeys MPRIS native messaging host" ,
1616 "path" : None ,
1717 "type" : "stdio" ,
1818 "allowed_origins" : []
1919}
20+ FIREFOX_HOST_MANIFEST = {
21+ "name" : "org.mpris.streamkeys_host" ,
22+ "description" : "Streamkeys MPRIS native messaging host" ,
23+ "path" : None ,
24+ "type" : "stdio" ,
25+ "allowed_extensions" : []
26+ }
27+ FIREFOX_EXT_ID = "streamkeys@streamkeys.com"
2028XDG_CONFIG_HOME = os .environ .get ("XDG_CONFIG_HOME" ,
2129 default = os .path .expanduser ("~/.config" ))
2230
@@ -31,20 +39,51 @@ def initialize_parser():
3139 p = subparsers .add_parser ("install" ,
3240 help = "Install the native messaging host" )
3341 p .add_argument ("id" , help = "The extension ID" )
34- p .set_defaults (func = main_install )
42+ p .set_defaults (func = main_install , firefox = False )
3543
3644 p = subparsers .add_parser ("uninstall" ,
3745 help = "Uninstall the native messaging host" )
38- p .set_defaults (func = main_uninstall )
46+ p .set_defaults (func = main_uninstall , firefox = False )
47+
48+ p = subparsers .add_parser ("install-ff" ,
49+ help = "Install the native messaging host for"
50+ " Firefox instead of Chrome" )
51+ p .add_argument ("--id" , default = FIREFOX_EXT_ID , help = "The extension ID" )
52+ p .set_defaults (func = main_install , firefox = True )
53+
54+ p = subparsers .add_parser ("uninstall-ff" ,
55+ help = "Uninstall the native messaging host for"
56+ " Firefox instead of Chrome" )
57+ p .set_defaults (func = main_uninstall , firefox = True )
3958 return parser
4059
4160
42- def get_xdg_config_paths ():
61+ def get_chrome_xdg_config_paths ():
4362 return [os .path .join (XDG_CONFIG_HOME , "chromium" ),
4463 os .path .join (XDG_CONFIG_HOME , "google-chrome" )]
4564
4665
47- def install_host (ext_id , install_dir ):
66+ def get_chrome_manifest_paths ():
67+ paths = []
68+ xdg_paths = get_chrome_xdg_config_paths ()
69+ for path in xdg_paths :
70+ if not os .path .exists (path ):
71+ continue
72+ message_hosts = os .path .join (path , "NativeMessagingHosts" )
73+ manifest_path = os .path .join (message_hosts , HOST_MANIFEST_FILENAME )
74+ paths .append (manifest_path )
75+ return paths
76+
77+
78+ def get_firefox_manifest_paths ():
79+ paths = []
80+ message_hosts = os .path .expanduser ("~/.mozilla/native-messaging-hosts" )
81+ manifest_path = os .path .join (message_hosts , HOST_MANIFEST_FILENAME )
82+ paths .append (manifest_path )
83+ return paths
84+
85+
86+ def install_host (ext_id , install_dir , firefox = False ):
4887 # Copy the host script
4988 host_path = os .path .join (install_dir , HOST_FILENAME )
5089 os .makedirs (install_dir , exist_ok = True )
@@ -56,37 +95,39 @@ def install_host(ext_id, install_dir):
5695 os .chmod (host_path , 0o744 )
5796
5897 # Create the manifest file
59- xdg_paths = get_xdg_config_paths ()
60- manifest = dict (HOST_MANIFEST )
61- manifest ["path" ] = host_path
62- manifest ["allowed_origins" ].append ("chrome-extension://%s/" % ext_id )
63- for path in xdg_paths :
64- if not os .path .exists (path ):
65- continue
66- message_hosts = os .path .join (path , "NativeMessagingHosts" )
67- manifest_path = os .path .join (message_hosts , HOST_MANIFEST_FILENAME )
68-
69- os .makedirs (message_hosts , exist_ok = True )
70- with open (manifest_path , "w" ) as f :
98+ if firefox :
99+ manifest_paths = get_firefox_manifest_paths ()
100+ manifest = dict (FIREFOX_HOST_MANIFEST )
101+ manifest ["path" ] = host_path
102+ manifest ["allowed_extensions" ].append (ext_id )
103+ else :
104+ manifest_paths = get_chrome_manifest_paths ()
105+ manifest = dict (CHROME_HOST_MANIFEST )
106+ manifest ["path" ] = host_path
107+ manifest ["allowed_origins" ].append ("chrome-extension://%s/" % ext_id )
108+
109+ for path in manifest_paths :
110+ os .makedirs (os .path .dirname (path ), exist_ok = True )
111+ with open (path , "w" ) as f :
71112 json .dump (manifest , f , indent = 2 )
72- os .chmod (manifest_path , 0o644 )
113+ os .chmod (path , 0o644 )
73114
74115
75- def uninstall_host (install_dir ):
116+ def uninstall_host (install_dir , firefox = False ):
76117 # Remove host script
77118 host_path = os .path .join (install_dir , HOST_FILENAME )
78119 if os .path .isfile (host_path ):
79120 os .remove (host_path )
80121
81122 # Remove manifest file
82- xdg_paths = get_xdg_config_paths ()
83- for path in xdg_paths :
84- if not os . path . exists ( path ) :
85- continue
86- message_hosts = os . path . join ( path , "NativeMessagingHosts" )
87- manifest_path = os . path . join ( message_hosts , HOST_MANIFEST_FILENAME )
88- if os .path .isfile (manifest_path ):
89- os .remove (manifest_path )
123+ if firefox :
124+ manifest_paths = get_firefox_manifest_paths ()
125+ else :
126+ manifest_paths = get_chrome_manifest_paths ()
127+
128+ for path in manifest_paths :
129+ if os .path .isfile (path ):
130+ os .remove (path )
90131
91132
92133def setup_logger (level = logging .DEBUG ):
@@ -109,11 +150,12 @@ def main():
109150
110151
111152def main_install (args ):
112- # Chrome's extension IDs are in hexadecimal but using a-p, referred
113- # internally as "mpdecimal". See https://stackoverflow.com/a/2050916
114- if (len (args .id ) != 32
115- or any (ord (c ) not in range (97 , 113 ) for c in args .id )):
116- raise RuntimeError ("Not valid extension ID: %s" % args .id )
153+ if not args .firefox :
154+ # Chrome's extension IDs are in hexadecimal but using a-p, referred
155+ # internally as "mpdecimal". See https://stackoverflow.com/a/2050916
156+ if (len (args .id ) != 32
157+ or any (ord (c ) not in range (97 , 113 ) for c in args .id )):
158+ raise RuntimeError ("Not valid extension ID: %s" % args .id )
117159
118160 try :
119161 from gi .repository import GLib , Gio # noqa: F401
@@ -127,11 +169,11 @@ def main_install(args):
127169 raise RuntimeError ("Required dependency `python3-pydbus' not"
128170 " found" )
129171
130- install_host (args .id , args .install_dir )
172+ install_host (args .id , args .install_dir , firefox = args . firefox )
131173
132174
133175def main_uninstall (args ):
134- uninstall_host (args .install_dir )
176+ uninstall_host (args .install_dir , firefox = args . firefox )
135177
136178
137179if __name__ == "__main__" :
0 commit comments