#!/usr/bin/env python3 # # Native messaging examples: # # https://github.com/mdn/webextensions-examples/blob/master/native-messaging/app/ping_pong.py import subprocess import nativemessaging while True: receivedMessage = nativemessaging.get_message() subprocess.call(['google-chrome-stable', receivedMessage['link']]) nativemessaging.send_message(nativemessaging.encode_message(''))
import nativemessaging as nm message = "" while True: message = nm.get_message() nm.send_message(nm.encode_message(message)) if message != None: with open("sharedtext.txt", "w") as f: f.write(message) message = None
import nativemessaging import plugpag import re import update if update.update(): nativemessaging.send_message( nativemessaging.encode_message(json.dumps({"result: -107"}))) else: result = '' f = open("temp.txt", "w+") f.write('conexão') while True: try: message = nativemessaging.get_message() if "amount" in message: message["amount"] = re.sub("[^0-9]", "", message["amount"]) result = plugpag.main(message) else: result = "Mensagem recebida invalida" + message except Exception as e: result = str(e) nativemessaging.send_message(nativemessaging.encode_message(result))
if __name__ == "__main__": # gamepad-remapper@r01 is sent as an arg if run by browser if "gamepad-remapper@r01" in sys.argv: gvars = {} while True: # wait for message from browser message = nm.get_message() if message["action"] == "start": # create kill variable gvars["kill"] = threading.Event() # start mapping using threading (so that the loop works while still waiting for messages from browser) gvars["t"] = threading.Thread(target=mapping, args=(gvars["kill"], message["config"])) gvars["t"].start() # send state and mode variables to browser nm.send_message(nm.encode_message({"state": "started", "mode": message["config"]["name"]})) elif message["action"] == "stop": # activate kill variable gvars["kill"].set() gvars["t"].join() # send state and mode variables to browser nm.send_message(nm.encode_message({"state": "stopped", "mode": None})) elif message["action"] == "tester": # open tester, piping stdout to /dev/null (otherwise breaks nativemessaging) with open(os.devnull, "w") as fp: subprocess.Popen("./test.py", stdout=fp) nm.send_message(nm.encode_message("LAUNCHED TEST")) else: print("GAMEPAD REMAPPER") # if file in args, otherwise ask for file if len(sys.argv) > 1 and os.path.isfile(sys.argv[1]):
def send(self, message): encoded = nativemessaging.encode_message(message) logger.info('SENDING: %s', message) nativemessaging.send_message(encoded)