Module: Dyndoc::Browser

Defined in:
lib/dyndoc-html-servers.rb

Constant Summary collapse

@@browser_cfg_file =
File.join(Dyndoc.home,"etc","browser.yml")
@@cfg =
nil
@@default_browser =
(RUBY_PLATFORM =~ /darwin/ ? "safari" : (RUBY_PLATFORM =~ /linux/ ? "midori" : "firefox"))
@@browser_reload_osa =
File.join(Dyndoc.home,"etc","browser_reload.osa")
@@browser_load_osa =
File.join(Dyndoc.home,"etc","browser_load.osa")

Class Method Summary collapse

Class Method Details

.cfgObject



32
33
34
35
36
37
# File 'lib/dyndoc-html-servers.rb', line 32

def Browser.cfg
  unless @@cfg
    @@cfg=(File.exist? @@browser_cfg_file) ? ::YAML::load_file(@@browser_cfg_file) : {}
  end
  @@cfg
end

.getObject



28
29
30
# File 'lib/dyndoc-html-servers.rb', line 28

def Browser.get
  Browser.cfg["browser"] || @@default_browser
end

.load(url) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/dyndoc-html-servers.rb', line 147

def Browser.load(url)
  if RUBY_PLATFORM =~ /darwin/
    mode=Browser.get.to_sym
    if mode==:safari
      code='tell application "'+Browser.name+'" to set URL of current tab of front window to "'+url+'"'
    elsif mode == :firefox
      code='tell application "Firefox" to open location "'+url+'"'
    elsif mode == :chrome
      code=%Q{
        tell application "Google Chrome"
          set frontIndex to active tab index of front window
          set URL of tab frontIndex of front window to "#{url}"
        end tell
      }
    end
    File.open(@@browser_load_osa,"w") do |f|
      f << code
    end
    `osascript #{@@browser_load_osa}`
  elsif RUBY_PLATFORM =~ /linux/
    system("xdg-open #{url} &")
  end
end

.nameObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dyndoc-html-servers.rb', line 45

def Browser.name
  mode=Browser.get.to_sym
  if RUBY_PLATFORM =~ /darwin/
    case mode
      when :chrome
        "Google Chrome"
      when :canary
        "Google Chrome Canary"
      when :firefox
        "Firefox"
      when :firefox_nightly
        "FirefoxNightly"
      when :firefox_developer
        "FirefoxDeveloperEdition"
      when :safari
        "Safari"
    end
  elsif RUBY_PLATFORM =~ /linux/
    mode.to_s
  end
end

.reloadObject



137
138
139
140
141
142
143
144
# File 'lib/dyndoc-html-servers.rb', line 137

def Browser.reload
  if RUBY_PLATFORM =~ /darwin/
    Browser.set_browser_reload unless File.exists? @@browser_reload_osa
    `osascript #{@@browser_reload_osa}`
  elsif RUBY_PLATFORM =~ /linux/ and File.exists? "/usr/bin/xdotool"
    `export DISPLAY=':0.0';/usr/bin/xdotool search --sync --onlyvisible  #{Browser.name} key F5 windowactivate`
  end
end

.save_cfgObject



39
40
41
42
43
# File 'lib/dyndoc-html-servers.rb', line 39

def Browser.save_cfg
  File.open(@@browser_cfg_file,"w") do |f|
    f << Browser.cfg.to_yaml
  end
end

.set(browser) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/dyndoc-html-servers.rb', line 19

def Browser.set(browser)
  Browser.cfg["browser"]=browser
  Browser.save_cfg
  FileUtils.rm(@@browser_load_osa) if File.exists? @@browser_load_osa
  FileUtils.rm(@@browser_reload_osa) if File.exists? @@browser_reload_osa
  Browser.set_browser_reload
  puts "Current browser is set to "+Browser.get+"!"
end

.set_browser_reloadObject



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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/dyndoc-html-servers.rb', line 68

def Browser.set_browser_reload
  activate=Browser.cfg["activate"] || false
  mode=Browser.get.to_sym
  code=case mode
    when :chrome
    %Q{
    tell application "Google Chrome"
      #{activate ? 'activate' : ''}
      "chrome"
      set winref to a reference to (first window whose title does not start with "Developer Tools - ")
      set winref's index to 1
      reload active tab of winref
    end tell
    }

    when :canary
    %Q{
    tell application "Google Chrome Canary"
      #{activate ? 'activate' : ''}
      "chrome canary"
      set winref to a reference to (first window whose title does not start with "Developer Tools - ")
      set winref's index to 1
      reload active tab of winref
    end tell
    }

    when :firefox
    %Q{
    tell application "Firefox"
    	activate
    	tell application "System Events" to keystroke "r" using command down
    end tell
    }

    when :firefox_nightly
    %Q{
    set a to path to frontmost application as text
    tell application "FirefoxNightly"
    	activate
    	tell application "System Events" to keystroke "r" using command down
    end tell
    #{activate ? '' : 'delay 0.2\nactivate application a'}
    }

    when :firefox_developer
    %Q{
    set a to path to frontmost application as text
    tell application "FirefoxDeveloperEdition"
    	activate
    	tell application "System Events" to keystroke "r" using command down
    end tell
    #{activate ? '' : 'delay 0.2\nactivate application a'}
    }

    when :safari
    %Q{
    tell application "Safari"
      #{activate ? 'activate' : ''}
      tell its first document
        set its URL to (get its URL)
      end tell
    end tell
    }
  end
  File.open(@@browser_reload_osa,"w") do |f|
    f << code.strip
  end
end