Class: VOODOO::Extension

Inherits:
Object
  • Object
show all
Defined in:
lib/voodoo/extension.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeExtension

Returns a new instance of Extension.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/voodoo/extension.rb', line 12

def initialize
    @id = 0
    @folder = Dir.mktmpdir
    @manifest = {
        name: '~',
        author: '~',
        description: '',
        version: '0.0.1',
        manifest_version: 2,
        background: {
            scripts: []
        },
        permissions: [],
        content_scripts: []
    }
end

Instance Attribute Details

#folderObject (readonly)

Returns the value of attribute folder.



10
11
12
# File 'lib/voodoo/extension.rb', line 10

def folder
  @folder
end

#manifestObject

Returns the value of attribute manifest.



9
10
11
# File 'lib/voodoo/extension.rb', line 9

def manifest
  @manifest
end

Instance Method Details

#add_background_script(content: nil, file: nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/voodoo/extension.rb', line 29

def add_background_script(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][:scripts] << path
end

#add_content_script(matches, js: [], css: []) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/voodoo/extension.rb', line 40

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

#saveObject



53
54
55
56
57
58
# File 'lib/voodoo/extension.rb', line 53

def save
    @manifest[:permissions] = @manifest[:permissions].uniq
    manifest_path = File.join(@folder, 'manifest.json')
    File.write(manifest_path, JSON.generate(@manifest))
    return @folder
end


60
61
62
# File 'lib/voodoo/extension.rb', line 60

def unlink
    FileUtils.rm_r(@folder, :force=>true)
end