-
Notifications
You must be signed in to change notification settings - Fork 11
/
ChromeREPL.py
162 lines (113 loc) · 4.73 KB
/
ChromeREPL.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import sublime
import sublime_plugin
import subprocess
import tempfile
import ChromeREPL.ChromeREPLHelpers as ChromeREPLHelpers
from ChromeREPL.ChromeREPLConnection import ChromeREPLConnection
# Plugin teardown
# ------------------------------------------------------------------------------
def plugin_unloaded():
ChromeREPLConnection.close_all_instances()
ChromeREPLConnection.clear_statuses()
# Helper functions
# ------------------------------------------------------------------------------
def start_chrome(use_default_chrome_profile=False):
settings = sublime.load_settings('ChromeREPL.sublime-settings')
chrome_port = settings.get('port')
user_flags = settings.get('chrome_flags')
flags = [
'--remote-debugging-port={}'.format(chrome_port),
] + user_flags
if not use_default_chrome_profile:
data_dir = tempfile.TemporaryDirectory()
flags.append('--user-data-dir={}'.format(data_dir))
if settings.get('auto_open_devtools', True):
flags.append('--auto-open-devtools-for-tabs')
cmd = [ChromeREPLHelpers.get_chrome_path()] + flags
try:
subprocess.Popen(cmd)
except Exception as e:
sublime.error_message("Could not start Chrome, check the path in your settings")
return False
# Commands
# ------------------------------------------------------------------------------
class ChromeReplStartChromeCommand(sublime_plugin.WindowCommand):
def is_enabled(self):
return not ChromeREPLHelpers.is_chrome_running()
def start_chrome(self, use_default_chrome_profile):
start_chrome(use_default_chrome_profile)
def run(self):
use_default_chrome_profile = False
self.start_chrome(use_default_chrome_profile)
class ChromeReplStartChromeNormalProfileCommand(ChromeReplStartChromeCommand):
def run(self):
use_default_chrome_profile = True
self.start_chrome(use_default_chrome_profile)
class ChromeReplRestartChromeCommand(sublime_plugin.WindowCommand):
def is_enabled(self):
return ChromeREPLHelpers.is_chrome_running()
def restart_chrome(self, use_default_chrome_profile):
process = ChromeREPLHelpers.get_chrome_process()
if process is not None:
process.terminate()
process.wait()
start_chrome(use_default_chrome_profile)
def run(self):
use_default_chrome_profile = False
self.restart_chrome(use_default_chrome_profile)
class ChromeReplRestartChromeNormalProfileCommand(ChromeReplRestartChromeCommand):
def run(self):
use_default_chrome_profile = True
self.restart_chrome(use_default_chrome_profile)
class ChromeReplConnectToTabCommand(sublime_plugin.WindowCommand):
def is_enabled(self):
return ChromeREPLHelpers.is_chrome_running_with_remote_debugging()
def run(self):
connection = ChromeREPLConnection.get_instance(self.window.active_view())
connection.connect_to_tab()
class ChromeReplEvaluateCommand(sublime_plugin.TextCommand):
HIGHLIGHT_KEY = 'chromerepl-eval'
HIGHLIGHT_SCOPE = 'chromerepl-eval'
def is_enabled(self):
return ChromeREPLConnection.is_instance_connected(self.view)
def run(self, edit):
connection = ChromeREPLConnection.get_instance(self.view)
# store selection for later restoration
prev = []
for sel in self.view.sel():
prev.append(sel)
success = True
# evaluate selections in Chrome
for sel in self.view.sel():
if sel.a == sel.b: # the 'selection' is a single point
sel = self.view.line(sel)
self.view.sel().add(sel)
try:
expression = self.view.substr(sel)
connection.execute(expression)
except Exception as e:
success = False
if success:
# highlight
self.view.add_regions(self.HIGHLIGHT_KEY,
self.view.sel(),
self.HIGHLIGHT_SCOPE,
flags=sublime.DRAW_NO_OUTLINE)
# clear selection so highlighting will be visible
self.view.sel().clear()
# do highlighting
sublime.set_timeout(lambda: self.view.sel().add_all(prev), 10)
# remove highlight and restore original selection
sublime.set_timeout(lambda: self.view.erase_regions(self.HIGHLIGHT_KEY), 50)
class ChromeReplClearCommand(sublime_plugin.WindowCommand):
def is_enabled(self):
return ChromeREPLConnection.is_instance_connected(self.window.active_view())
def run(self):
connection = ChromeREPLConnection.get_instance(self.window.active_view())
connection.chrome_evaluate('console.clear()')
class ChromeReplReloadPageCommand(sublime_plugin.WindowCommand):
def is_enabled(self):
return ChromeREPLConnection.is_instance_connected(self.window.active_view())
def run(self, ignoreCache='False'):
connection = ChromeREPLConnection.get_instance(self.window.active_view())
connection.reload(ignoreCache == 'True')