-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathob-chromium.pl
executable file
·93 lines (84 loc) · 2.79 KB
/
ob-chromium.pl
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
#!/usr/bin/perl
# openbox-chromium 1.01-beta - Browser bookmarks pipe menu for Openbox
# Copyright (C) 2018 https://github.com/Spoiledbroth
use strict;
use warnings;
use JSON::Tiny qw(decode_json);
use IO::File;
use HTML::Entities qw(encode_entities);
# === CONFIGURATION: ===
# $bookmarks - Location of your chromium/google-chrome bookmarks.
# You can try "find ~/.config -name Bookmarks" if you're having finding it.
# $browser - browser command, either x-www-browser, chromium or google-chrome
# $maxlength - upper limit of characters for bookmark titles
my $bookmarks = "/home/MYUSER/.config/chromium/MYPROFILE/Bookmarks";
my $browser = "chromium";
my $maxlength = 35;
# Lets open up that bookmarks file
my $file = do {
local $/ = undef;
open my $fh, "<", $bookmarks
or die "could not open $bookmarks: $!";
<$fh>
};
# Now, get the bookmark JSON into a reference
my $data = decode_json $file;
# Remove sync transaction infos... causes the script to blow up.
delete $data->{'roots'}->{'sync_transaction_version'};
# Cast that reference to a hash of the 'roots' entries in the bookmark file.
my %json = %{$data->{'roots'}};
# ... so we only loop over what we need, essentially.
# Build the Openbox menu header
print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
. "<openbox_pipe_menu>\n";
# Main program loop...
foreach (keys %json)
{
traversal ($data->{'roots'}->{$_});
}
# End of menu
print "</openbox_pipe_menu>\n";
# Recursion function for bookmark "folders"
sub traversal
{
my ($obtained) = @_;
# If the entry is a folder with >0 children, build an entry and recurse
if (($obtained->{'type'} eq "folder") &&
(0+@{$obtained->{'children'}} != 0) &&
($obtained ne "sync_transaction_version")) {
build_entry($obtained);
my @children = @{$obtained->{'children'}};
foreach my $i (0 .. $#children)
{
#delete $children[$i]->{'sync_transaction_version'}
traversal($children[$i]);
}
print "</menu>\n"; # I'm not perfect...
# Else, we need to build an entry for a URL.
} elsif ($obtained->{'type'} eq "url") {
build_entry($obtained);
}
return;
}
# Function to build the menu (seperation of concerns)
sub build_entry
{
my ($entry) = @_;
if ($entry->{'type'} eq "folder") {
# print case for bookmark folder
print " <menu id=\""
.$entry->{'id'}."\" label=\"".encode_entities($entry->{'name'})."\">\n";
} elsif ($entry->{'type'} eq "url") {
# print case for bookmark link
my $truncated = scalar $entry->{'name'};
$truncated =~ s/^(.{1,$maxlength})(.+)?/$1 . (defined $2 ? "..." : "")/e;
print " <item label=\"".encode_entities($truncated)."\">\n"
. " <action name=\"Execute\">\n"
. " <execute>\n"
. " ".$browser." <![CDATA[".$entry->{'url'}."]]>\n"
. " </execute>\n"
. " </action>\n"
. " </item>\n";
}
return;
}