Mercurial > firefox-enso
comparison enso_pyxpcom.py @ 2:4f22928c7a2c
Added firefox selection context, so it's now possible to use enso in firefox (otherwise, the command-c injection wasn't working b/c I guess you can't inject events into your own process).
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Thu, 08 May 2008 13:00:26 -0700 |
parents | 6e4335fa3321 |
children | 21f5e1dc0a4c |
comparison
equal
deleted
inserted
replaced
1:6e4335fa3321 | 2:4f22928c7a2c |
---|---|
6 _ensoStarted = False | 6 _ensoStarted = False |
7 | 7 |
8 ENSO_PATH = os.path.join(os.getenv("HOME"), "Documents/enso-svn") | 8 ENSO_PATH = os.path.join(os.getenv("HOME"), "Documents/enso-svn") |
9 LOGGING_FILE = os.path.join(os.getenv("HOME"), "enso_pyxpcom.log") | 9 LOGGING_FILE = os.path.join(os.getenv("HOME"), "enso_pyxpcom.log") |
10 | 10 |
11 def getFuelApplication(): | |
12 from xpcom import components | |
13 fuel_cls = components.classes["@mozilla.org/fuel/application;1"] | |
14 fuel_abs = fuel_cls.createInstance() | |
15 fuel_if = components.interfaces.fuelIApplication | |
16 return fuel_abs.queryInterface(fuel_if) | |
17 | |
18 def getCommandDispatcher(): | |
19 ci = components.interfaces | |
20 cc = components.classes | |
21 | |
22 Application = getFuelApplication() | |
23 document = Application.activeWindow.activeTab.document | |
24 | |
25 mediator = cc["@mozilla.org/appshell/window-mediator;1"].getService(ci.nsIWindowMediator) | |
26 window = mediator.getMostRecentWindow("navigator:browser") | |
27 return window.document.commandDispatcher | |
28 | |
29 def firefoxSelectionHook(): | |
30 import AppKit | |
31 | |
32 app = AppKit.NSWorkspace.sharedWorkspace().activeApplication() | |
33 if app["NSApplicationProcessIdentifier"] == os.getpid(): | |
34 return getFirefoxSelection() | |
35 else: | |
36 from enso.platform.osx import selection | |
37 | |
38 return selection._old_get() | |
39 | |
40 def getFirefoxSelection(): | |
41 text = "" | |
42 dispatcher = getCommandDispatcher() | |
43 | |
44 if dispatcher.focusedElement: | |
45 start = dispatcher.focusedElement.selectionStart | |
46 end = dispatcher.focusedElement.selectionEnd | |
47 if start != end: | |
48 text = dispatcher.focusedElement.value[start:end] | |
49 elif dispatcher.focusedWindow: | |
50 sel = dispatcher.focusedWindow.getSelection() | |
51 if sel.rangeCount >= 1: | |
52 text = sel.toString() | |
53 | |
54 return {"text" : text} | |
55 | |
56 def installFirefoxSelectionHook(): | |
57 from enso.platform.osx import selection | |
58 | |
59 selection._old_get = selection.get | |
60 selection.get = firefoxSelectionHook | |
61 | |
11 def getAbout(): | 62 def getAbout(): |
12 global _ensoStarted | 63 global _ensoStarted |
13 | 64 |
14 if not _ensoStarted: | 65 if not _ensoStarted: |
15 logging.info("Importing Enso.") | 66 logging.info("Importing Enso.") |
16 sys.path.append(ENSO_PATH) | 67 sys.path.append(ENSO_PATH) |
17 | 68 |
18 import enso | 69 import enso |
70 | |
71 logging.info("Installing Firefox selection hook.") | |
72 installFirefoxSelectionHook() | |
19 logging.info("Launching Enso.") | 73 logging.info("Launching Enso.") |
20 _ensoStarted = True | 74 _ensoStarted = True |
21 enso.run() | 75 enso.run() |
22 | 76 |
23 return "Enso is running." | 77 return "Enso is running." |