UNPKG

5 kBtext/coffeescriptView Raw
1`#!/usr/bin/env node
2`
3utils = require "./utils"
4utils.extend global, utils
5
6optimist = require "optimist"
7args = optimist.usage("Usage: $0 [--sock=PATH]")
8 .alias("h", "help")
9 .default("sock", config.sock)
10 .argv
11
12if args.help
13 optimist.showHelp()
14 process.exit(0)
15
16chromix = require("./chromix-too")(args.sock).chromix
17
18[ commandName, commandArgs ] =
19 if 2 < process.argv.length then [ process.argv[2], process.argv[3...] ] else [ "ping", [] ]
20
21# Extract the query flags (for chrome.tabs.query) from the arguments. Return the new arguments and the
22# query-flags object.
23getQueryFlags = (commandArgs) ->
24 validQueryFlags = {}
25 # These are the valid boolean flags listed here: https://developer.chrome.com/extensions/tabs#method-query.
26 for flag in "active pinned audible muted highlighted discarded autoDiscardable currentWindow lastFocusedWindow".split " "
27 validQueryFlags[flag] = true
28 queryFlags = {}
29 commandArgs =
30 for arg in commandArgs
31 if arg of validQueryFlags
32 queryFlags[arg] = true
33 continue
34 # Use a leading "-" or "!" to negate the test; e.g. "-audible" or "!active".
35 else if arg[0] in ["-", "!"] and arg[1..] of validQueryFlags
36 queryFlags[arg[1..]] = false
37 continue
38 # For symmetry, we also allow "+"; e.g. "+audible".
39 else if arg[0] in ["+"] and arg[1..] of validQueryFlags
40 queryFlags[arg[1..]] = true
41 continue
42 else
43 arg
44 [ commandArgs, queryFlags ]
45
46# Filter tabs by the remaining command-line arguements. We require a match in either the URL or the title.
47# If the argument is a bare number, then we require it to match the tab Id.
48filterTabs = do ->
49 integerRegex = /^\d+$/
50
51 (commandArgs, tabs) ->
52 for tab in tabs
53 continue unless do ->
54 for arg in commandArgs
55 if integerRegex.test(arg) and tab.id == parseInt arg
56 continue
57 else if integerRegex.test arg
58 return false
59 else if tab.url.indexOf(arg) == -1 and tab.title.indexOf(arg) == -1
60 return false
61 true
62 tab
63
64# Return an array of tabs matching the flags and other arguments on the command line.
65getMatchingTabs = (commandArgs, callback) ->
66 [ commandArgs, queryFlags ] = getQueryFlags commandArgs
67 chromix "chrome.tabs.query", {}, queryFlags, (tabs) ->
68 tabs = filterTabs commandArgs, tabs
69 process.exit 1 if tabs.length == 0
70 callback tabs
71
72focusWindow = (windowId) ->
73 chromix "chrome.windows.update", {}, windowId, {focused: true}, ->
74
75switch commandName
76 when "ls", "list", "tabs"
77 getMatchingTabs commandArgs, (tabs) ->
78 console.log "#{tab.id} #{tab.url} #{tab.title}" for tab in tabs
79
80 when "tid" # Like "ls", but outputs only the tab Id of the matching tabs.
81 getMatchingTabs commandArgs, (tabs) ->
82 console.log "#{tab.id}" for tab in tabs
83
84 when "focus", "activate"
85 getMatchingTabs commandArgs, (tabs) ->
86 for tab in tabs
87 chromix "chrome.tabs.update", {}, tab.id, {selected: true}
88 focusWindow tab.windowId
89
90 when "select"
91 getMatchingTabs commandArgs, (tabs) ->
92 for tab in tabs
93 chromix "chrome.tabs.update", {}, tab.id, {selected: true}
94
95 when "reload"
96 getMatchingTabs commandArgs, (tabs) ->
97 chromix "chrome.tabs.reload", {}, tab.id, {} for tab in tabs
98
99 when "url"
100 [url, commandArgs...] = commandArgs
101 getMatchingTabs commandArgs, (tabs) ->
102 chromix "chrome.tabs.update", {}, tab.id, {url} for tab in tabs
103
104 when "rm", "remove", "close"
105 getMatchingTabs commandArgs, (tabs) ->
106 chromix "chrome.tabs.remove", {}, tab.id for tab in tabs
107
108 when "open", "create"
109 for arg in commandArgs
110 do (arg) ->
111 chromix "chrome.tabs.create", {}, {url: arg}, (tab) ->
112 focusWindow tab.windowId
113 console.log "#{tab.id} #{tab.url}"
114
115 when "ping"
116 chromix "ping", {}, (response) ->
117 if response == "ok"
118 process.exit 0
119 else
120 process.exit 1
121
122 when "file"
123 for arg in commandArgs
124 url = if arg.indexOf("file://") == 0 then arg else "file://#{require("path").resolve arg}"
125
126 do (url) ->
127 getMatchingTabs [], (tabs) ->
128 tabs = (t for t in tabs when t.url.indexOf(url) == 0)
129 if tabs.length == 0
130 chromix "chrome.tabs.create", {}, {url: url}, (tab) ->
131 focusWindow tab.windowId
132 console.log "#{tab.id} #{tab.url}"
133 else
134 for tab in tabs
135 do (tab) ->
136 chromix "chrome.tabs.update", {}, tab.id, {selected: true}, ->
137 chromix "chrome.tabs.reload", {}, tab.id, {}, ->
138 focusWindow tab.windowId
139
140 when "raw", "josn"
141 args =
142 for arg in commandArgs[1..]
143 try
144 JSON.parse arg
145 catch
146 arg
147 chromix commandArgs[0], {}, args..., (response) ->
148 console.log JSON.stringify response
149
150 else
151 console.error "error: unknown command: #{commandName}"
152 process.exit 2