-
Notifications
You must be signed in to change notification settings - Fork 19
/
notification_capture.py
66 lines (59 loc) · 1.7 KB
/
notification_capture.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# coding: utf-8
from objc_util import *
import console
from functools import partial
NSNotificationCenter=ObjCClass('NSNotificationCenter')
#logging.basicConfig(filename='log.txt',format='%(levelname)s:%(message)s', level=logging.DEBUG)
class GlobalNotificationObserver(object):
def __init__(self, excludes=('UI','NS','_UI')) :
#self.captured=[]
self.captured={}
self.center=NSNotificationCenter.defaultCenter()
self.excludes=excludes
self._observer=None
self._blk=None
def _block(self,_cmd,notification):
try:
name=str(ObjCInstance(notification).name())
if name.startswith(self.excludes):
return
obj=str(ObjCInstance(notification).object())
userInfo=str(ObjCInstance(notification).userInfo())
#self.captured.append(ObjCInstance(notification),)
self.captured[name]=ObjCInstance(notification)
#console.hud_alert(name)
except:
pass
def start(self):
if self._observer:
raise Exception('observer already started')
self._blk=ObjCBlock(self._block,
restype=None,
argtypes=[c_void_p,c_void_p])
self._observer = \
self.center.addObserverForName_object_queue_usingBlock_(None,None,None,self._blk)
retain_global(self)
import weakref
def on_die(killed_ref):
killed_ref.stop()
def stop(self):
import objc_util
if self._observer:
self.center.removeObserver_(self._observer)
self._observer=None
release_global(self)
def display_notifications(self):
import pprint
pprint.pprint(self.captured)
def reset(self):
self.stop()
self.captured={}
if __name__=='__main__':
g=GlobalNotificationObserver(excludes='zzz')
g.start()
import ui,time
v=ui.View()
v.present('sheet')
time.sleep(5)
g.stop()
print(list(g.captured.keys()))