Class: VOODOO::Extension
- Inherits:
-
Object
- Object
- VOODOO::Extension
- Defined in:
- lib/voodoo/extension.rb
Instance Attribute Summary collapse
-
#folder ⇒ Object
readonly
Returns the value of attribute folder.
-
#manifest ⇒ Object
Returns the value of attribute manifest.
Instance Method Summary collapse
- #add_content_script(matches, js: [], css: []) ⇒ Object
- #add_service_worker(content: nil, file: nil) ⇒ Object
-
#initialize ⇒ Extension
constructor
A new instance of Extension.
- #save ⇒ Object
- #unlink ⇒ Object
Constructor Details
#initialize ⇒ Extension
Returns a new instance of Extension.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/voodoo/extension.rb', line 12 def initialize @id = 0 @folder = Dir.mktmpdir @manifest = { name: '~', author: '~', description: '', version: '0.0.1', manifest_version: 3, permissions: [], host_permissions: [], content_scripts: [], background: { service_worker: nil } } end |
Instance Attribute Details
#folder ⇒ Object (readonly)
Returns the value of attribute folder.
10 11 12 |
# File 'lib/voodoo/extension.rb', line 10 def folder @folder end |
#manifest ⇒ Object
Returns the value of attribute manifest.
9 10 11 |
# File 'lib/voodoo/extension.rb', line 9 def manifest @manifest end |
Instance Method Details
#add_content_script(matches, js: [], css: []) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/voodoo/extension.rb', line 41 def add_content_script(matches, js: [], css: []) matches = [matches] unless matches.is_a? Array js = js.map { |str| add_file(str) } css = css.map { |str| add_file(str, with_extension: '.css') } @manifest[:content_scripts] << { js: js, css: css, matches: matches } end |
#add_service_worker(content: nil, file: nil) ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/voodoo/extension.rb', line 30 def add_service_worker(content: nil, file: nil) if content == nil && file != nil content = File.read file end if content == nil raise StandardError.new(':content or :file argument are required') end path = add_file(content, with_extension: '.js') @manifest[:background][:service_worker] = path end |
#save ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/voodoo/extension.rb', line 54 def save @manifest[:permissions] = @manifest[:permissions].uniq service_worker = @manifest[:background][:service_worker] if service_worker == nil || service_worker == '' @manifest[:background].delete(:service_worker) end manifest_path = File.join(@folder, 'manifest.json') File.write(manifest_path, JSON.generate(@manifest)) return @folder end |
#unlink ⇒ Object
65 66 67 |
# File 'lib/voodoo/extension.rb', line 65 def unlink FileUtils.rm_r(@folder, :force=>true) end |