Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sublime Text package for browser-extension-reload-firefox and browser-extension-reload-server
import websocket
try:
import thread
except ImportError:
import _thread as thread
import time
import json
import ssl
import sublime, sublime_plugin, os
ws = None
addons = None
class BrowserExtensionReload( sublime_plugin.EventListener ):
def on_post_save(self, view):
global ws, addons
filename = os.path.splitext(view.file_name())[0]
print("[BrowserExtensionReload] on_post_save")
cAddon = None
if addons != None:
for addon in addons:
if cAddon != None:
break
if addon["path"] in filename:
cAddon = addon
break
for path in addon["watch"]:
print(path)
if path in filename:
cAddon = addon
break
if cAddon != None:
print("[BrowserExtensionReload] sending reload query...")
ws.send("{ \"query\": \"reload\", \"data\": \""+ cAddon["id"] +"\" }")
def on_message(ws, message):
global addons
print("[BrowserExtensionReload] " + message)
data = json.loads(message)
if data["query"] == "initialize":
addons = data["data"]
def on_error(ws, error):
print("[BrowserExtensionReload] " + error)
def on_close(ws):
print("[BrowserExtensionReload] ### closed ###")
def on_open(ws):
print("[BrowserExtensionReload] ### opened ###")
ws.send("{ \"query\": \"initialize\" }")
def run():
global ws
websocket.enableTrace(True)
ws = websocket.WebSocketApp("ws://127.0.0.1:7879",
on_message = on_message,
on_error = on_error,
on_close = on_close)
ws.on_open = on_open
ws.run_forever()
if __name__ == "User.BrowserExtensionReload":
thread.start_new_thread(run, ())
@thoughtsunificator

This comment has been minimized.

Copy link
Owner Author

@thoughtsunificator thoughtsunificator commented Dec 30, 2020

This Sublime Text package will check the path of the file you save against the list of paths sent by Firefox to see if it is part of an extension, if it is it will send a reload query and Firefox will reload the extension.

Prerequisites

Installing

  • Place BrowserExtensionReload.py in the Packages/User folder of your Sublime Text installation.
  • Extract the content of websocket_client.zip in the Lib/python3.3 folder of your Sublime Text installation.
@thoughtsunificator

This comment has been minimized.

Copy link
Owner Author

@thoughtsunificator thoughtsunificator commented Dec 30, 2020

Note: websocket_client.zip is a slightly modified version of https://pypi.org/project/websocket_client/ that works with Python 3.3.

@thoughtsunificator

This comment has been minimized.

Copy link
Owner Author

@thoughtsunificator thoughtsunificator commented Jan 2, 2021

Note: Extracting the content of websocket_client.zip to Lib/python3.3 will load the Python module into Sublime Text 3 so that the User packages can import it. See https://forum.sublimetext.com/t/installing-python-libraries/14170/5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.