-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbrowser_api.py
89 lines (71 loc) · 4.41 KB
/
browser_api.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Copyright (C) 2021 Aurore Fass
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
Message passing APIs for all browsers except Chromium based.
"""
import handle_messages as m
GLOBAL_PM = ['window.postMessage', 'this.postMessage', 'that.postMessage', 'self.postMessage',
'top.postMessage', 'global.postMessage', '.source.postMessage']
def global_post_message(node_value):
""" Checks if the postMessage is the window.postMessage (WA - CS communication). """
if any(pm in node_value for pm in GLOBAL_PM):
return {'mess_type': 'B', 'fun': m.post_message} # Sends
if '.postMessage' in node_value:
return {'mess_type': 'Trash', 'fun': m.do_nothing} # Do nothing as port.PM
return {'mess_type': 'B', 'fun': m.post_message} # Sends
def port_post_message(node_value):
""" Checks if the postMessage is the port.postMessage (WA - BP and CS - BP communication). """
if any(pm in node_value for pm in GLOBAL_PM):
return {'mess_type': 'Trash', 'fun': m.do_nothing} # Do nothing, wrong PM
if '.postMessage' in node_value:
return {'mess_type': 'B2', 'fun': m.post_message} # port.postMessage to send messages
return {'mess_type': 'Trash', 'fun': m.do_nothing} # Do nothing, wrong PM
CS2BP = {'browser.runtime.sendMessage': {'mess_type': 'B1',
'fun': m.browser_runtime_sendMessage}, # Sends
'browser.runtime.connect': {'mess_type': 'B2',
'fun': m.browser_runtime_connect}} # Gets port + message
BP2CS = {'browser.tabs.sendMessage': {'mess_type': 'B1',
'fun': m.browser_tabs_sendMessage}, # Sends
'browser.tabs.connect': {'mess_type': 'B2',
'fun': m.browser_tabs_connect}} # Gets port + message
CS_BP = {'browser.runtime.onMessage.addListener': {'mess_type': 'B1',
'fun': m.browser_runtime_onMessage_addListener},
# Receives
'browser.runtime.onConnect.addListener': {'mess_type': 'B2',
'fun': m.browser_runtime_onConnect_addListener},
# Gets port --> port.postMessage / port.onMessage.addListener
'postMessage': False, # Checks which postMessage
# port.onMessage.addListener to receive messages
'.onMessage.addListener': {'mess_type': 'B2',
'fun': m.onMessage_addListener}}
WA_CS = {'postMessage': True, # Checks which postMessage
'addEventListener': {'mess_type': 'B', 'fun': m.add_event_listener}, # Receives
'onmessage': {'mess_type': 'B', 'fun': m.onmessage}} # Receives
WA2BP = {'browser.runtime.sendMessage': {'mess_type': 'B1',
'fun': m.browser_runtime_sendMessage}, # Sends
'browser.runtime.connect': {'mess_type': 'B2',
'fun': m.browser_runtime_connect}} # Gets port + message
BP2WA = {'browser.runtime.onMessageExternal.addListener':
{'mess_type': 'B1', 'fun': m.browser_runtime_onMessageExternal_addListener}, # Receive
'browser.runtime.onConnectExternal.addListener':
{'mess_type': 'B2', 'fun': m.browser_runtime_onConnectExternal_addListener}, # Getport
'postMessage': False, # Checks which postMessage
'.runtime.onMessage.addListener': {'mess_type': 'Trash', 'fun': m.do_nothing},
# So that cannot be confounded with .onMessage.addListener
# port.onMessage.addListener to receive messages
'.onMessage.addListener': {'mess_type': 'B2', 'fun': m.onMessage_addListener}}
# B1: Browsers except for chrome one-time channel
# B2: Browsers except for chrome long-lived channel
# Trash: Empty, just to ensure that we are not matching wrong substrings