| 1 |
|
| 2 |
require_relative 'model/web' |
| 3 |
|
| 4 |
Plugin.create(:web) do |
| 5 |
extend Memoist |
| 6 |
|
| 7 |
intent Plugin::Web::Web, label: _('外部ブラウザで開く') do |intent_token| |
| 8 |
openurl(intent_token.model.perma_link.to_s) |
| 9 |
end |
| 10 |
|
| 11 |
def openurl(url) |
| 12 |
if UserConfig[:url_open_specified_command] |
| 13 |
url_open_command = UserConfig[:url_open_command] |
| 14 |
bg_system(url_open_command, url) |
| 15 |
elsif(defined? Win32API) then |
| 16 |
shellExecuteA = Win32API.new('shell32.dll','ShellExecuteA',%w(p p p p p i),'i') |
| 17 |
shellExecuteA.call(0, 'open', url.melt, 0, 0, 1) |
| 18 |
else |
| 19 |
url_open_command = find_url_open_command |
| 20 |
if url_open_command |
| 21 |
bg_system(url_open_command, url) |
| 22 |
else |
| 23 |
activity :system, _("この環境で、URLを開くためのコマンドが判別できませんでした。設定の「表示→URLを開く方法」で、URLを開く方法を設定してください。") end end |
| 24 |
rescue => exception |
| 25 |
title = _('コマンド "%{command}" でURLを開こうとしましたが、開けませんでした。設定の「表示→URLを開く方法」で、URLを開く方法を設定してください。') % {command: url_open_command} |
| 26 |
description = { |
| 27 |
title: title, |
| 28 |
message: exception.to_s, |
| 29 |
backtrace: exception.backtrace.join("\n") } |
| 30 |
activity :system, title, |
| 31 |
error: exception, |
| 32 |
description: "%{title}\n\n%{message}\n\n%{backtrace}" % description |
| 33 |
end |
| 34 |
|
| 35 |
ができるコマンドを返す。 |
| 36 |
memoize def find_url_open_command |
| 37 |
openable_commands = %w{xdg-open open /etc/alternatives/x-www-browser} |
| 38 |
wellknown_browsers = %w{firefox chromium opera} |
| 39 |
command = nil |
| 40 |
catch(:urlopen) do |
| 41 |
openable_commands.each{ |o| |
| 42 |
if command_exist?(o) |
| 43 |
command = o |
| 44 |
throw :urlopen end } |
| 45 |
wellknown_browsers.each{ |o| |
| 46 |
if command_exist?(o) |
| 47 |
activity :system, _('この環境で、URLを開くためのコマンドが判別できなかったので、"%{command}"を使用します。設定の「表示→URLを開く方法」で、URLを開く方法を設定してください。') % {command: command} |
| 48 |
command = o |
| 49 |
throw :urlopen end } end |
| 50 |
command end |
| 51 |
end |