blib/lib/App/FirefoxUtils.pm

filter: Statementstmt 24.8% 36 / 145 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 35 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 35.2% 6 / 17 Podpod 100% 11 / 11 totaltotal 18.6% 53 / 285 CC 47 SCAR 74.3
CC
47
Cov
15%
open_firefox_tabs
2256.0
ps_firefox
2.0
pause_firefox
2.0
CRAP 1683.5
↑ Summary
1 package App::FirefoxUtils;
2  
3 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict 'subs', 'vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
11 1 our $DATE = '2026-03-29'; # DATE
12 1 our $DIST = 'App-FirefoxUtils'; # DIST
13 1 our $VERSION = '0.025'; # VERSION
14  
15 1 our @EXPORT_OK = qw(
16 ps_firefox
17 pause_firefox
18 unpause_firefox
19 pause_and_unpause_firefox
20 firefox_has_processes
21 firefox_is_paused
22 firefox_is_running
23 terminate_firefox
24 restart_firefox
25 start_firefox
26 open_firefox_tabs
27 );
28  
29 1 our %SPEC;
30  
31 1 ▶use App::BrowserUtils ();
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 1 $SPEC{':package'} = {
34 v => 1.1,
35 summary => 'Utilities related to Firefox',
36 };
37  
38 $SPEC{ps_firefox} = {
39 1 v => 1.1,
40 summary => "List Firefox processes",
41 args => {
42 %App::BrowserUtils::args_common,
43 },
44 };
45 0 ▶sub ps_firefox {
Subroutine ps_firefox: not called CC=1 SCAR=6.9
Pod documented
46 0 App::BrowserUtils::_do_browser('ps', 'firefox', @_);
47 }
48  
49 $SPEC{pause_firefox} = {
50 1 v => 1.1,
51 summary => "Pause (kill -STOP) Firefox",
52 description => $App::BrowserUtils::desc_pause,
53 args => {
54 %App::BrowserUtils::args_common,
55 },
56 };
57 0 ▶sub pause_firefox {
Subroutine pause_firefox: not called CC=1 SCAR=6.9
Pod documented
58 0 App::BrowserUtils::_do_browser('pause', 'firefox', @_);
59 }
60  
61 $SPEC{unpause_firefox} = {
62 1 v => 1.1,
63 summary => "Unpause (resume, continue, kill -CONT) Firefox",
64 args => {
65 %App::BrowserUtils::args_common,
66 },
67 };
68 0 ▶sub unpause_firefox {
Subroutine unpause_firefox: not called CC=1 SCAR=6.9
Pod documented
69 0 App::BrowserUtils::_do_browser('unpause', 'firefox', @_);
70 }
71  
72 $SPEC{pause_and_unpause_firefox} = {
73 1 v => 1.1,
74 summary => "Pause and unpause Firefox alternately",
75 description => $App::BrowserUtils::desc_pause_and_unpause,
76 args => {
77 %App::BrowserUtils::args_common,
78 %App::BrowserUtils::argopt_periods,
79 },
80 };
81 0 ▶sub pause_and_unpause_firefox {
Subroutine pause_and_unpause_firefox: not called CC=1 SCAR=6.9
Pod documented
82 0 App::BrowserUtils::_do_browser('pause_and_unpause', 'firefox', @_);
83 }
84  
85 $SPEC{firefox_has_processes} = {
86 1 v => 1.1,
87 summary => "Check whether Firefox has processes",
88 args => {
89 %App::BrowserUtils::args_common,
90 %App::BrowserUtils::argopt_quiet,
91 },
92 };
93 0 ▶sub firefox_has_processes {
Subroutine firefox_has_processes: not called CC=1 SCAR=6.9
Pod documented
94 0 App::BrowserUtils::_do_browser('has_processes', 'firefox', @_);
95 }
96  
97 $SPEC{firefox_is_paused} = {
98 1 v => 1.1,
99 summary => "Check whether Firefox is paused",
100 description => <<'_',
101  
102 Firefox is defined as paused if *all* of its processes are in 'stop' state.
103  
104 _
105 args => {
106 %App::BrowserUtils::args_common,
107 %App::BrowserUtils::argopt_quiet,
108 },
109 };
110 0 ▶sub firefox_is_paused {
Subroutine firefox_is_paused: not called CC=1 SCAR=6.9
Pod documented
111 0 App::BrowserUtils::_do_browser('is_paused', 'firefox', @_);
112 }
113  
114 $SPEC{firefox_is_running} = {
115 1 v => 1.1,
116 summary => "Check whether Firefox is running",
117 description => <<'_',
118  
119 Firefox is defined as running if there are some Firefox processes that are *not*
120 in 'stop' state. In other words, if Firefox has been started but is currently
121 paused, we do not say that it's running. If you want to check if Firefox process
122 exists, you can use `ps_firefox`.
123  
124 _
125 args => {
126 %App::BrowserUtils::args_common,
127 %App::BrowserUtils::argopt_quiet,
128 },
129 };
130 0 ▶sub firefox_is_running {
Subroutine firefox_is_running: not called CC=1 SCAR=6.9
Pod documented
131 0 App::BrowserUtils::_do_browser('is_running', 'firefox', @_);
132 }
133  
134 $SPEC{terminate_firefox} = {
135 1 v => 1.1,
136 summary => "Terminate Firefox (by default with -KILL signal)",
137 args => {
138 %App::BrowserUtils::args_common,
139 %App::BrowserUtils::argopt_signal,
140 },
141 };
142 0 ▶sub terminate_firefox {
Subroutine terminate_firefox: not called CC=1 SCAR=6.9
Pod documented
143 0 App::BrowserUtils::_do_browser('terminate', 'firefox', @_);
144 }
145  
146 $SPEC{restart_firefox} = {
147 1 v => 1.1,
148 summary => "Restart firefox",
149 args => {
150 %App::BrowserUtils::argopt_firefox_cmd,
151 %App::BrowserUtils::argopt_quiet,
152 },
153 features => {
154 dry_run => 1,
155 },
156 };
157 0 ▶sub restart_firefox {
Subroutine restart_firefox: not called CC=1 SCAR=6.9
Pod documented
158 0 App::BrowserUtils::restart_browsers(@_, restart_firefox=>1);
159 }
160  
161 $SPEC{start_firefox} = {
162 1 v => 1.1,
163 summary => "Start firefox if not already started",
164 args => {
165 %App::BrowserUtils::argopt_firefox_cmd,
166 %App::BrowserUtils::argopt_quiet,
167 },
168 features => {
169 dry_run => 1,
170 },
171 };
172 0 ▶sub start_firefox {
Subroutine start_firefox: not called CC=1 SCAR=6.9
Pod documented
173 0 App::BrowserUtils::start_browsers(@_, start_firefox=>1);
174 }
175  
176 $SPEC{open_firefox_tabs} = {
177 1 v => 1.1,
178 summary => 'Open a list of Firefox tabs, with options',
179 description => <<'MARKDOWN',
180  
181 This utility is best used via Perl or curried after you supply the list of
182 items. For example, in script `open-my-tiktok`:
183  
184 use App::FirefoxUtils ();
185 use Perinci::CmdLine::Any;
186 use Perinci::Sub::Util qw(gen_curried_sub);
187  
188 my @containers = (
189 'account1',
190 'account2',
191 'account3',
192 );
193  
194 my @containers_additional = (
195 'account4',
196 'account5',
197 );
198  
199 gen_curried_sub(
200 'App::FirefoxUtils::open_firefox_tabs',
201 {
202 items => [
203 (map { +{url=>'https://www.tiktok.com/', container=>$_} } @containers),
204 (map { +{url=>'https://www.tiktok.com/', container=>$_, include_by_default=>0} } @containers_additional),
205 ],
206 },
207 'open_my_tiktok',
208 );
209  
210 Perinci::CmdLine::Any->new(
211 url => '/main/open_my_tiktok',
212 log => 1,
213 )->run;
214  
215 Later on, you run your script:
216  
217 # open all items that are included by default
218 % open-my-tiktok
219  
220 # only open items that match the queries
221 % open-my-tiktok account2 account3
222  
223 # only open items that do not contain 'account4'
224 % open-my-tiktok --new-window --shuffle -- -account4
225  
226 MARKDOWN
227 args => {
228 items => {
229 schema => ['array*', {
230 min_len => 1,
231 of => ['hash*', {
232 keys => {
233 url => 'url*',
234 tags => ['array*', {of=>['str*', min_len=>1]}],
235 container => 'str*',
236 include_by_default => ['bool*', default=>1],
237 },
238 req_keys => ['url'],
239 }],
240 }],
241 req => 1,
242 },
243 new_window => {
244 schema => 'bool*',
245 cmdline_aliases => {
246 w => {},
247 # W = --no-new-window
248 },
249 },
250 kde_activity => {
251 summary => 'Switch to the specified KDE activity',
252 schema => 'str*',
253  
254 },
255 shuffle => {
256 schema => 'bool*',
257 },
258 include_any_tags => {
259 summary => 'Include all items that have any tag specified',
260 schema => ['array*', of=>'str*'],
261 },
262 include_all_tags => {
263 summary => 'Include all items that have ALL tags specified',
264 schema => ['array*', of=>'str*'],
265 },
266 exclude_any_tags => {
267 summary => 'Exclude all items that have any tags specified',
268 schema => ['array*', of=>'str*'],
269 },
270 exclude_all_tags => {
271 summary => 'Exclude all items that have ALL tags specified',
272 schema => ['array*', of=>'str*'],
273 },
274 query => {
275 schema => ['array*', of=>'str*'],
276 pos => 0,
277 slurpy => 1,
278 },
279 },
280 deps => {
281 all => [
282 {prog=>'firefox-container'},
283 ],
284 },
285 };
286 0 ▶sub open_firefox_tabs {
Subroutine open_firefox_tabs: not called CC=47 SCAR=77.2
Pod documented
287 0 require IPC::System::Options;
288 0 require List::Util;
289 0 require List::Util::Find;
290  
291 0 my %args = @_;
292  
293 0 ▶ my $items = $args{items} or return [400, "Please specify items"];
Branch: 1 branch
BranchTrueFalseTotal
my $items = $args{"items"} or return [400, "Please specify items"] 0 0 0
294 0 ▶ @$items or return [400, "Please specify at least one item in items"];
Branch: 1 branch
BranchTrueFalseTotal
@$items or return [400, "Please specify at least one item in items"] 0 0 0
295  
296 0 ▶ if (defined $args{kde_activity}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"kde_activity"}) 0 0 0
297 0 require App::KDEActivityUtils;
298 my $res_kde_activity = App::KDEActivityUtils::set_current_kde_activity(
299 name => $args{kde_activity},
300 0 );
301 0 ▶ return [500, "Can't set current KDE activity: $res_kde_activity->[0] - $res_kde_activity->[1]"]
Branch: 1 branch
BranchTrueFalseTotal
unless $res_kde_activity->[0] == 200 0 0 0
302 unless $res_kde_activity->[0] == 200;
303 }
304  
305 0 ▶ if ($args{shuffle}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"shuffle"}) 0 0 0
306 0 $items = [List::Util::shuffle(@$items)];
307 }
308  
309 0 my $j = 0;
310 ITEM:
311 0 for my $i (0 .. $#{$items}) {
312 0 my $item = $items->[$i];
313 0 my @ff_args;
314 0 my $env = {};
315  
316 # if not included by default, will be included only if specifically matching a filter
317 0 ▶ my $include_by_default = $item->{include_by_default} // 1;
Condition0%0/2
$item->{"include_by_default"} // 1
l!l
00
MC/DC0%0/1
$item->{"include_by_default"} // 1
$item->{"include_by_default"}
Truth table0%0/2
A
A= $item->{"include_by_default"}
Aresult
11
00
318  
319 0 my $match_a_filter = 0;
320  
321 FILTER: {
322 0 INCLUDE_ANY_TAGS: {
323 0 ▶ last unless $args{include_any_tags} && @{ $args{include_any_tags} };
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"include_any_tags"} and @{$args{"include_any_tags"};} 0 0 0
Condition0%0/3
$args{"include_any_tags"} and @{$args{"include_any_tags"};}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"include_any_tags"} and @{$args{"include_any_tags"};}
$args{"include_any_tags"} @{$args{"include_any_tags"};}
Truth table0%0/3
A and B
A= $args{"include_any_tags"}B= @{$args{"include_any_tags"};}
ABresult
0X0
100
111
324 0 do { log_debug "Skipping item %s: does not pass include_any_tags %s", $item, $args{include_any_tags}; next ITEM }
325 0 ▶ unless List::Util::Find::hasanystrs($args{include_any_tags}, @{ $item->{tags} // []});
Branch: 1 branch
BranchTrueFalseTotal
unless (List::Util::Find::hasanystrs($args{"include_any_tags"}, @{$item->{"tags"} // [];})) 0 0 0
Condition0%0/2
$item->{"tags"} // []
l!l
00
MC/DC0%0/1
$item->{"tags"} // []
$item->{"tags"}
Truth table0%0/2
A
A= $item->{"tags"}
Aresult
11
00
326 0 $match_a_filter++;
327 }
328 INCLUDE_ALL_TAGS: {
329 0 ▶ last unless $args{include_all_tags} && @{ $args{include_all_tags} };
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"include_all_tags"} and @{$args{"include_all_tags"};} 0 0 0
Condition0%0/3
$args{"include_all_tags"} and @{$args{"include_all_tags"};}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"include_all_tags"} and @{$args{"include_all_tags"};}
$args{"include_all_tags"} @{$args{"include_all_tags"};}
Truth table0%0/3
A and B
A= $args{"include_all_tags"}B= @{$args{"include_all_tags"};}
ABresult
0X0
100
111
330 0 do { log_debug "Skipping item %s: does not pass include_all_tags %s", $item, $args{include_all_tags}; next ITEM }
331 0 ▶ unless List::Util::Find::hasallstrs($args{include_all_tags}, @{ $item->{tags} // []});
Branch: 1 branch
BranchTrueFalseTotal
unless (List::Util::Find::hasallstrs($args{"include_all_tags"}, @{$item->{"tags"} // [];})) 0 0 0
Condition0%0/2
$item->{"tags"} // []
l!l
00
MC/DC0%0/1
$item->{"tags"} // []
$item->{"tags"}
Truth table0%0/2
A
A= $item->{"tags"}
Aresult
11
00
332 0 $match_a_filter++;
333 }
334 EXCLUDE_ANY_TAGS: {
335 0 ▶ last unless $args{exclude_any_tags} && @{ $args{exclude_any_tags} };
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"exclude_any_tags"} and @{$args{"exclude_any_tags"};} 0 0 0
Condition0%0/3
$args{"exclude_any_tags"} and @{$args{"exclude_any_tags"};}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"exclude_any_tags"} and @{$args{"exclude_any_tags"};}
$args{"exclude_any_tags"} @{$args{"exclude_any_tags"};}
Truth table0%0/3
A and B
A= $args{"exclude_any_tags"}B= @{$args{"exclude_any_tags"};}
ABresult
0X0
100
111
336 0 do { log_debug "Skipping item %s: does not pass exclude_any_tags %s", $item, $args{exclude_any_tags}; next ITEM }
337 0 ▶ if List::Util::Find::hasanystrs($args{exclude_any_tags}, @{ $item->{tags} // []});
Branch: 1 branch
BranchTrueFalseTotal
if (List::Util::Find::hasanystrs($args{"exclude_any_tags"}, @{$item->{"tags"} // [];})) 0 0 0
Condition0%0/2
$item->{"tags"} // []
l!l
00
MC/DC0%0/1
$item->{"tags"} // []
$item->{"tags"}
Truth table0%0/2
A
A= $item->{"tags"}
Aresult
11
00
338 0 $match_a_filter++;
339 }
340 EXCLUDE_ALL_TAGS: {
341 0 ▶ last unless $args{exclude_all_tags} && @{ $args{exclude_all_tags} };
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"exclude_all_tags"} and @{$args{"exclude_all_tags"};} 0 0 0
Condition0%0/3
$args{"exclude_all_tags"} and @{$args{"exclude_all_tags"};}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"exclude_all_tags"} and @{$args{"exclude_all_tags"};}
$args{"exclude_all_tags"} @{$args{"exclude_all_tags"};}
Truth table0%0/3
A and B
A= $args{"exclude_all_tags"}B= @{$args{"exclude_all_tags"};}
ABresult
0X0
100
111
342 0 do { log_debug "Skipping item %s: does not pass exclude_all_tags %s", $item, $args{exclude_all_tags}; next ITEM }
343 0 ▶ if List::Util::Find::hasallstrs($args{exclude_all_tags}, @{ $item->{tags} // []});
Branch: 1 branch
BranchTrueFalseTotal
if (List::Util::Find::hasallstrs($args{"exclude_all_tags"}, @{$item->{"tags"} // [];})) 0 0 0
Condition0%0/2
$item->{"tags"} // []
l!l
00
MC/DC0%0/1
$item->{"tags"} // []
$item->{"tags"}
Truth table0%0/2
A
A= $item->{"tags"}
Aresult
11
00
344 0 $match_a_filter++;
345 }
346 QUERY: {
347 0 ▶ last unless $args{query} && @{ $args{query} };
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"query"} and @{$args{"query"};} 0 0 0
Condition0%0/3
$args{"query"} and @{$args{"query"};}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"query"} and @{$args{"query"};}
$args{"query"} @{$args{"query"};}
Truth table0%0/3
A and B
A= $args{"query"}B= @{$args{"query"};}
ABresult
0X0
100
111
348 0 my $num_positive_queries = 0;
349 0 my $num_negative_queries = 0;
350 0 my $match = 0;
351 Q:
352 0 for my $query0 (@{ $args{query} }) {
353 0 my ($is_negative, $query) = $query0 =~ /\A(-?)(.*)/;
354 0 ▶ $num_positive_queries++ if !$is_negative;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_negative 0 0 0
355 0 ▶ $num_negative_queries++ if $is_negative;
Branch: 1 branch
BranchTrueFalseTotal
if $is_negative 0 0 0
356  
357 0 ▶ if ($item->{url} =~ /$query/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->{"url"} =~ /$query/i) 0 0 0
358 0 ▶ if ($is_negative) { goto L1 } else { $match = 1; last Q }
Branch: 1 branch
BranchTrueFalseTotal
if ($is_negative) { } 0 0 0
359 }
360 0 ▶ for my $tag (@{ $item->{tags} // [] }) {
Condition0%0/2
$item->{"tags"} // []
l!l
00
MC/DC0%0/1
$item->{"tags"} // []
$item->{"tags"}
Truth table0%0/2
A
A= $item->{"tags"}
Aresult
11
00
361 0 ▶ if ($tag =~ /$query/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag =~ /$query/i) 0 0 0
362 0 ▶ if ($is_negative) { goto L1 } else { $match = 1; last Q }
Branch: 1 branch
BranchTrueFalseTotal
if ($is_negative) { } 0 0 0
363 }
364 }
365 0 ▶ for my $container ($item->{container} // '') {
Condition0%0/2
$item->{"container"} // ""
l!l
00
MC/DC0%0/1
$item->{"container"} // ""
$item->{"container"}
Truth table0%0/2
A
A= $item->{"container"}
Aresult
11
00
366 0 ▶ if ($container =~ /$query/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($container =~ /$query/i) 0 0 0
367 0 ▶ if ($is_negative) { goto L1 } else { $match = 1; last Q }
Branch: 1 branch
BranchTrueFalseTotal
if ($is_negative) { } 0 0 0
368 }
369 }
370 } # for query
371 0 ▶ $match++ if $num_positive_queries == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $num_positive_queries == 0 0 0 0
372 L1:
373 0 ▶ do { log_debug "Skipping item %s: does not pass query %s", $item, $args{query}; next ITEM }
Branch: 1 branch
BranchTrueFalseTotal
unless ($match) 0 0 0
374 unless $match;
375 0 $match_a_filter++;
376 } # QUERY
377 } # FILTER
378  
379 0 ▶ if (!$include_by_default && !$match_a_filter) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($include_by_default or $match_a_filter) 0 0 0
Condition0%0/3
$include_by_default or $match_a_filter
l!l&&r!l&&!r
000
MC/DC0%0/2
$include_by_default or $match_a_filter
$include_by_default $match_a_filter
Truth table0%0/3
A or B
A= $include_by_defaultB= $match_a_filter
ABresult
1X1
011
000
380 0 log_debug "Skipping item %s: not included by default and does not match filter(s)", $item;
381 0 next ITEM;
382 }
383  
384 0 ▶ if ($j == 0 && $args{new_window}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j == 0 and $args{'new_window'}) { } 0 0 0
Condition0%0/3
$j == 0 and $args{"new_window"}
!ll&&!rl&&r
000
MC/DC0%0/2
$j == 0 and $args{"new_window"}
$j == 0 $args{"new_window"}
Truth table0%0/3
A and B
A= $j == 0B= $args{"new_window"}
ABresult
0X0
100
111
385 0 push @ff_args, "--new-window", $item->{url};
386 } else {
387 0 push @ff_args, $item->{url};
388 }
389 0 $j++;
390  
391 0 ▶ if (defined $item->{container}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $item->{"container"}) 0 0 0
392 0 $env->{FIREFOX_CONTAINER} = $item->{container};
393 }
394  
395 0 ▶ log_info "Opening tab %d: %s (%s) ...", $j, $item->{url}, (defined $item->{container} ? "container=$item->{container}" : "");
Branch: 1 branch
BranchTrueFalseTotal
defined $item->{'container'} ? : 0 0 0
396 0 IPC::System::Options::system({env=>$env, log=>1}, "firefox-container", @ff_args);
397 }
398  
399 0 [200];
400 }
401  
402 1 1;
403 # ABSTRACT: Utilities related to Firefox
404  
405 __END__
↑ Summary