4
4
5
5
class AutoChrome ::ChromeExtension
6
6
attr_reader :path , :id , :key , :manifest , :version
7
+
7
8
def initialize ( crx_path )
8
9
@path = crx_path
9
10
10
- data = File . read ( @path , mode : 'rb' )
11
- key_file = File . dirname ( path ) + "/" + File . basename ( @path , ".crx" ) + ".pub"
12
- if !File . exists? ( key_file )
13
- raise "No key file found for extension #{ path } "
14
- end
15
- @key = File . read ( key_file )
16
- @id = Digest ::SHA256 . hexdigest ( key ) [ 0 ...32 ] . tr ( '0-9a-f' , 'a-p' )
17
-
18
11
# use open3 to suppress unzip warnings for unexpected crx headers
19
12
json , _ , _ = Open3 . capture3 ( 'unzip' , '-qc' , @path , 'manifest.json' )
20
13
21
14
@manifest = JSON . parse ( json , symbolize_names : true )
22
- @manifest [ :key ] = Base64 . encode64 ( @key ) . gsub ( /\s / , '' )
23
15
24
- @version = @manifest [ :version ]
16
+ key_file = File . dirname ( path ) + "/" + File . basename ( @path , ".crx" ) + ".pub"
17
+ unless File . exists? ( key_file )
18
+ if @manifest . dig ( :key ) != nil
19
+ puts "[---] Reading key from manifest, this might not work..."
20
+ @key = @manifest [ :key ]
21
+ @id = Digest ::SHA256 . hexdigest ( Base64 . decode64 ( @key ) ) [ 0 ...32 ] . tr ( '0-9a-f' , 'a-p' )
22
+ else
23
+ raise "No key file or key in manifest found for extension #{ path } "
24
+ end
25
+ else
26
+ @key = File . read ( key_file )
27
+ @id = Digest ::SHA256 . hexdigest ( key ) [ 0 ...32 ] . tr ( '0-9a-f' , 'a-p' )
28
+ @manifest [ :key ] = Base64 . encode64 ( @key ) . gsub ( /\s / , '' )
29
+ end
30
+
31
+ if @manifest . dig ( :id ) != nil
32
+ @id = @manifest [ :id ]
33
+ else
34
+ if :id == nil || id . to_s . strip . empty?
35
+ raise "No id found for extension #{ path } !"
36
+ end
37
+ end
38
+
39
+ if @manifest . dig ( :version ) != nil
40
+ @version = @manifest [ :version ]
41
+ else
42
+ if :version == nil
43
+ raise "No version found for extension #{ path } !"
44
+ end
45
+ end
25
46
end
26
- end
47
+ end
0 commit comments