UNPKG

4.6 kBMarkdownView Raw
1# Chromix-Too
2
3This project provides external (e.g. command-line or scripted) access to Chrome's internal (Javascript) API's.
4
5For example, the following command closes all tabs on stackoverflow:
6
7 chromix-too rm stackoverflow.com
8
9Chromix-too is a replacement for
10[chromix](https://github.com/smblott-github/chromix). Chromix-too is
11considerably simpler, uses a Unix-domain socket for communication between
12client and server (which is more secure), and is better packaged (it can be used as a module too).
13
14## Getting started
15
16There are three components: a chrome extension, a server and the `chromix-too` utility.
17
18Install [the extension](https://chrome.google.com/webstore/detail/chromix-too/ppapdfccnamacakfkpfmpfnefpeajboj) from the Chrome Store.
19
20To install the server and the `chromix-too` utility:
21
22```shell
23sudo npm install -g chromix-too
24```
25
26Next, you need to run the server:
27
28```shell
29chromix-too-server
30```
31
32And try out the client:
33
34```shell
35chromix-too ls
36```
37
38Of course, you need to keep the server running all the time. There are many ways to do this, but I use [daemontools](https://cr.yp.to/daemontools.html); my daemontools `run` file is just:
39
40```shell
41PATH="/usr/local/bin:$PATH"
42HOME='/home/blott' exec setuidgid blott chromix-too-server
43```
44
45(Or perhaps just leave the server running in a tmux session.)
46
47## Commands
48
49List tabs:
50
51```shell
52chromix-too ls
53```
54
55List just tab Ids:
56
57```shell
58chromix-too tid
59```
60
61Focus a tab:
62
63```shell
64chromix-too focus https://github.com/smblott-github/chromix
65```
66
67Select a tab (select the tab but don't `focus` Chrome window):
68
69```shell
70chromix-too select https://github.com/smblott-github/chromix
71```
72
73Remove a tab:
74
75```shell
76chromix-too rm https://github.com/smblott-github/chromix
77```
78
79Reload a tab:
80
81```shell
82chromix-too reload https://github.com/smblott-github/chromix
83```
84
85Open a tab:
86
87```shell
88chromix-too open https://github.com/smblott-github/chromix
89```
90
91View a file:
92
93```shell
94chromix-too file ./README.html
95```
96
97(The `file` command also focuses and reloads an existing tab if one exists.)
98
99Verify that everything is running correctly:
100
101```shell
102chromix-too ping
103```
104
105### Raw JSON
106
107Call any available Chrome function from the command line:
108
109```shell
110chromix-too raw chrome.storage.local.set '{"pi": 3.141}'
111
112chromix-too raw chrome.storage.local.get pi
113# {"pi":3.141}
114
115chromix-too raw chrome.storage.local.get pi | jq '.pi'
116# 3.141
117```
118
119## Filters
120
121For all of the commands above (except where it doesn't make sense), you can
122filter the list of tabs to which the command applies.
123
124There are three kinds of filter:
125
1261. If the filter is just a bare number, then it is interpreted as a tab Id.
127
1282. If the filter is one of the boolean options described
129 [here](https://developer.chrome.com/extensions/tabs#method-query), then the
130 corresponding flag is set. For example, you can use `pinned` to operate on all pinned tabs.
131
132 These boolean flags can be inverted: `-pinned` selects all unpinned tabs.
133
1343. Any remaining filter arguments are treated as queries. Tabs are removed
135 from consideration unless the query text is present in either the tab's URL
136 or the tab's title.
137
138Examples:
139
140```shell
141# Remove the tab with this tab Id.
142chromix-too rm 1234
143
144# Remove all audible tabs.
145chromix-too rm audible
146
147# Remove all unpinned tabs.
148chromix-too rm -pinned
149
150# List GMail tabs.
151chromix-too ls mail.google.com
152
153# Focus my Google Inbox tab.
154chromix-too focus Inbox smblott@gmail.com
155```
156
157All commands which accept filters fail (so, yield a non-zero exit status) if there are no matching tabs.
158
159## Usage as a module
160
161It is also possible to use `chromix-too` as a node module; here's an example:
162
163```Coffeescript
164chromix = require("chromix-too")().chromix
165
166chromix "chrome.storage.local.set", {}, {pi: 3.141}, ->
167 chromix "chrome.storage.local.get", {}, "pi", (response) ->
168 console.log response.pi
169```
170
171The second argument (`{}`, here) is a place holder for future extensions.
172
173The general form is:
174
175```Coffeescript
176chromix PATH, REQUEST, ARGS..., CALLBACK
177```
178
179The number of `ARGS...` provided must match the number of (non-callback) arguments expected by the relevant
180Chrome API call. When the call is actually made, chromix-too simply
181appends its own callback, and that callback must be in the correct argument position.
182
183## Known issues
184
185- There is currently no way to set the websocket port used between the Chrome extension and the server.
186- Only background-page API calls are possible. It is intended to add the ability to invoke functions in a content script at some point in the future.
187
188Contributions are welcome.