Created
July 5, 2013 04:12
-
-
Save shuujii/5931557 to your computer and use it in GitHub Desktop.
Chrome extension control command on Mac
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| # -*- coding: utf-8 -*- | |
| require 'optparse' | |
| EXTENSION_URL = '"chrome://extensions-frame/"' | |
| options = {enablements: []} | |
| optparser = OptionParser.new(nil, 17, " ") do |o| | |
| o.banner = "Usage: #{File.basename $0} [OPTIONS] EXTENSION-NAME" | |
| o.on("-e", "--enable", "拡張機能を有効にする (既に有効の場合は何もしない)") do | |
| options[:enablements] << true | |
| end | |
| o.on("-d", "--disable", "拡張機能を無効にする (既に無効の場合は何もしない)") do | |
| options[:enablements] << false | |
| end | |
| o.on("-C", "--no-close", "拡張機能タブを開いた場合に閉じない") do | |
| options[:no_close] = true | |
| end | |
| o.separator "" | |
| o.separator "-d -e のように指定すると無効にしてから有効にする。" | |
| end | |
| begin | |
| optparser.order!(ARGV) | |
| if ARGV.size != 1 | |
| raise OptionParser::ParseError, "require one EXTENSION-NAME" | |
| elsif options[:enablements].empty? | |
| raise OptionParser::ParseError, "require -e and/or -d" | |
| end | |
| rescue OptionParser::ParseError => e | |
| puts e | |
| exit 1 | |
| end | |
| ext_name = ARGV[0] | |
| javascript = '"' << <<EOS.gsub(/["\\]/,'"'=>'\\"','\\'=>'\\\\') << '"' | |
| !function() { | |
| var titles = | |
| document.querySelectorAll('#extension-settings-list .extension-title'); | |
| for (var i in titles) { | |
| if (titles[i].textContent !== '#{ext_name}') continue; | |
| var list_item = titles[i].parentNode.parentNode.parentNode; | |
| var checkbox = | |
| list_item.querySelector(".enable-controls input[type='checkbox']"); | |
| var enablements = #{options[:enablements].inspect}; | |
| for (var i in enablements) { | |
| if (enablements[i] !== checkbox.checked) { | |
| var evt = document.createEvent('MouseEvents'); | |
| evt.initEvent('click', true, true); | |
| checkbox.dispatchEvent(evt); | |
| checkbox.checked = !checkbox.checked; | |
| } | |
| } | |
| break; | |
| } | |
| }(); | |
| ''; | |
| EOS | |
| system "osascript", "-e", <<EOS | |
| tell window 1 of application "Google Chrome" | |
| set ext_tab to false | |
| repeat with t in tabs | |
| if URL of t is #{EXTENSION_URL} then | |
| set ext_tab to t | |
| exit | |
| end | |
| end | |
| if ext_tab is not false then | |
| ext_tab execute javascript #{javascript} | |
| else | |
| set active_tab_index to active tab index | |
| open location #{EXTENSION_URL} | |
| delay 1 | |
| active tab execute javascript #{javascript} | |
| #{"active tab close" unless options[:no_close]} | |
| set active tab index to active_tab_index | |
| end | |
| end | |
| EOS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment