Skip to content

Instantly share code, notes, and snippets.

@azu
Created February 16, 2009 11:10
Show Gist options
  • Save azu/65126 to your computer and use it in GitHub Desktop.
Save azu/65126 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Launch Safari
// @include main
// @compatibility Firefox 2.0 3.0
// @Last Change: 2008-04-28.
// ==/UserScript==
/*
* The Original Code written by zeniko
* http://forums.mozillazine.org/viewtopic.php?t=397735
*/
/*
* ChangeLog
* 2008-04-28: Firefox 3.0b5 support
*
*/
var LaunchSafari = {
mSchemes: ["file", "ftp", "http", "https"],
init: function()
{
this.mItem = document.createElement("menuitem");
document.getElementById("contentAreaContextMenu").addEventListener("popupshowing", function() {
LaunchSafari.onPopupShowing(this);
}, false);
},
onPopupShowing: function(aPopup)
{
if (aPopup.id != "contentAreaContextMenu") return;
aPopup.insertBefore(this.mItem, document.getElementById("context-sep-" + ((gContextMenu.onLink)?"open":"stop")));
if (gContextMenu.onLink) {
this.mItem.setAttribute("oncommand", "LaunchSafari.launch(gContextMenu.linkURI);");
this.mItem.setAttribute("label", !this.isJa() ? "Search in ATOK" : "\u30EA\u30F3\u30AF\u3092 Safari \u3067\u958B\u304F");
} else {
this.mItem.setAttribute("oncommand", "LaunchSafari.launch(gBrowser.currentURI);");
this.mItem.setAttribute("label", !this.isJa() ? "Search in ATOK" : "Safari \u3067\u958B\u304F");
}
this.mItem.hidden = !gContextMenu.onLink && gContextMenu.onImage;
this.mItem.setAttribute("disabled", this.mItem.hidden || !this.isSupported((gContextMenu.onLink)?gContextMenu.linkURI:gBrowser.currentURI));
},
_getFocusedWindow: function(){ //現在のウインドウを得る
var focusedWindow = document.commandDispatcher.focusedWindow;
if (!focusedWindow || focusedWindow == window)
return window._content;
else
return focusedWindow;
},
_getselection: function() { //選択されている文字列を得る
var targetWindow = this._getFocusedWindow();
var sel = Components.lookupMethod(targetWindow, 'getSelection').call(targetWindow);
// for textfields
if (sel && !sel.toString()) {
var node = document.commandDispatcher.focusedElement;
if (node &&
(node.type == "text" || node.type == "textarea") &&
'selectionStart' in node &&
node.selectionStart != node.selectionEnd) {
var offsetStart = Math.min(node.selectionStart, node.selectionEnd);
var offsetEnd = Math.max(node.selectionStart, node.selectionEnd);
return node.value.substr(offsetStart, offsetEnd-offsetStart);
}
}
return sel ? sel.toString().replace(/\s/g,' ').replace(/^[\ ]+|[\ ]+$/g,'').replace(/[\ ]+/g,' ') : "";
},
launch: function(aURI, aApp)
{
if (!this.isSupported(aURI))
{
throw new Error("LaunchSafari: unsupported URI scheme '" + aURI.scheme + "'!");
}
var safari = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
//var win = document.commandDispatcher.focusedWindow;
var sel = this._getselection();
var UI = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"].
createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
//キャラクタコード変換
var platform = window.navigator.platform.toLowerCase();
if(platform.indexOf('win')>-1){
UI.charset = "Shift_JIS";
sel = UI.ConvertFromUnicode(sel);
}else{
UI.charset = "UTF-8";
sel = UI.ConvertFromUnicode(sel);
}
safari.initWithPath("D:\\Software\\GetTextOperaLClick\\GetTextOperaLClick.exe");
var ss;
if ('nsIShellService_MOZILLA_1_8_BRANCH' in Components.interfaces) {
// Firefox 2
ss = Components.classes["@mozilla.org/browser/shell-service;1"]
.getService(Components.interfaces.nsIShellService_MOZILLA_1_8_BRANCH);
} else {
// Firefox 3
ss = Components.classes["@mozilla.org/browser/shell-service;1"]
.getService(Components.interfaces.nsIShellService);
}
ss.openApplicationWithURI(safari, sel);
},
isSupported: function(aURI)
{
return this.mSchemes.indexOf(aURI.scheme) > -1;
},
isJa: function()
{
var prefBranch = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch);
return prefBranch.getCharPref("general.useragent.locale").indexOf("ja") > -1;
}
};
LaunchSafari.init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment