Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var gd12 = {
- init : function(evt)
- {
- gd12debug.log("gd12.init starts");
- gd12.prefs.read();
- var appcontent = document.getElementById("appcontent"); // browser
- if (appcontent)
- {
- appcontent.addEventListener("load", gd12.contentLoadHandler, true);
- }
- var contextMenu = document.getElementById("contentAreaContextMenu");
- if (contextMenu)
- {
- contextMenu.addEventListener("popupshown",
- gd12.cmPopupShownHandler, false);
- }
- window.addEventListener("keydown", gd12.keydownHandler, true);
- gd12debug.log("gd12.init ends");
- },
- contentLoadHandler : function(evt)
- {
- gd12debug.log("gd12.contentLoadHandler starts");
- var doc = evt.originalTarget;
- if (doc instanceof HTMLDocument)
- {
- // register mouse handlers
- // we register them with content documents only because we
- // do not want the events fired when user click on menues, etc.
- doc.addEventListener("click", gd12.clickHandler, true);
- doc.addEventListener("mousemove", gd12.mousemoveHandler, true);
- // redundant. clean the inline translation flag.
- gd12.dicInline.isTranslating = false;
- }
- gd12debug.log("gd12.contentLoadHandler ends");
- },
- cmPopupShownHandler : function(evt)
- {
- // Google Dictionary menu
- var cmItem = document.getElementById("gd12gdicCM");
- if (cmItem)
- {
- if (gd12.prefs.isGDicInContextMenu && !gd12.isInlineDicOff)
- {
- if (gContextMenu.isTextSelected)
- cmItem.hidden = false;
- else
- {
- cmItem.hidden = true;
- if (gd12.prefs.isAggressive)
- {
- // Because it takes 0.5 second to capture a hovered word,
- // we need to delay a little bit (0.6 sec) to see whether
- // to show the context menu item for a hovered word.
- window.setTimeout(function() {
- if (gd12.hover.word && gd12.hover.word.length > 0)
- cmItem.hidden = false;
- },
- 600);
- }
- }
- }
- else
- {
- cmItem.hidden = true;
- }
- }
- // Translate Page menu
- var cmTItem = document.getElementById("gd12gtrnCM");
- if (cmTItem)
- {
- if (gd12.prefs.isGTransInContextMenu)
- cmTItem.hidden = false;
- else
- cmTItem.hidden = true;
- }
- },
- // Context menu oncommand handler
- lookupSelectedOrHovered : function()
- {
- if (gContextMenu.isTextSelected)
- {
- gd12.select.lookupSelected();
- }
- else if (gd12.prefs.isAggressive &&
- gd12.hover.word && gd12.hover.word.length > 0)
- {
- gd12.hover.translate();
- }
- },
- clickHandler : function(evt)
- {
- //gd12debug.log("gd12.clickHandler starts");
- if (2 == evt.detail)
- {
- // set for gd12.select.lookupSelected()
- gd12.select.mouseX = evt.screenX;
- gd12.select.mouseY = evt.screenY;
- if (gd12.prefs.isDoubleClick)
- {
- gd12.select.lookup(evt);
- }
- }
- else if (1 == evt.detail)
- {
- // set for gd12.select.lookupSelected()
- gd12.select.mouseX = evt.screenX;
- gd12.select.mouseY = evt.screenY;
- if (gd12.prefs.isSelect)
- {
- gd12.select.lookup(evt);
- }
- }
- //gd12debug.log("gd12.clickHandler ends");
- },
- mousemoveHandler : function(evt)
- {
- //gd12debug.log("gd12.mousemoveHandler starts");
- // feature: moving mouse away to close the popup.
- if (gd12.prefs.isMouseMoveClosePopup &&
- gd12.dicInline.isTranslating &&
- gd12.popup.popup && gd12.popup.popup.state == "open")
- {
- if (0 == gd12.popup.xWhenPopup && 0 == gd12.popup.yWhenPopup)
- {
- // first mouse movement after popup.
- gd12.popup.xWhenPopup = evt.screenX;
- gd12.popup.yWhenPopup = evt.screenY;
- }
- else
- {
- if (evt.screenX + 100 < gd12.popup.xWhenPopup
- || evt.screenX > gd12.popup.xWhenPopup + 100
- || evt.screenY + 100 < gd12.popup.yWhenPopup
- || evt.screenY > gd12.popup.yWhenPopup + 100)
- {
- // mouse has moved some distance.
- gd12.popup.close();
- }
- }
- }
- var isToCapture = false;
- var isToTranslate = false;
- if (gd12.prefs.isAggressive)
- {
- isToCapture = true;
- }
- if (gd12.prefs.isCtrlHover)
- {
- if (gd12.prefs.isCtrlShiftHover)
- {
- if (evt.ctrlKey && evt.shiftKey && !evt.altKey && !evt.metaKey)
- {
- isToCapture = true;
- isToTranslate = true;
- }
- }
- else if (gd12.prefs.isCtrlAltHover)
- {
- if (evt.ctrlKey && !evt.shiftKey && evt.altKey && !evt.metaKey)
- {
- isToCapture = true;
- isToTranslate = true;
- }
- }
- else
- {
- // Ctrl only + hover
- if (evt.ctrlKey && !evt.shiftKey && !evt.altKey && !evt.metaKey)
- {
- isToCapture = true;
- isToTranslate = true;
- }
- }
- }
- if (isToCapture)
- gd12.hover.capture(evt, isToTranslate);
- //gd12debug.log("gd12.mousemoveHandler ends");
- },
- keydownHandler : function(evt)
- {
- //gd12debug.log("gd12.keydownHandler starts");
- if (gd12.prefs.isCtrlHover && gd12.prefs.isAggressive)
- {
- var isToTranslate = false;
- if (gd12.prefs.isCtrlShiftHover)
- {
- if ((KeyEvent.DOM_VK_CONTROL == evt.keyCode &&
- evt.shiftKey && !evt.altKey && !evt.metaKey)
- || (KeyEvent.DOM_VK_SHIFT == evt.keyCode &&
- evt.ctrlKey && !evt.altKey && !evt.metaKey))
- {
- isToTranslate = true;
- }
- }
- else if (gd12.prefs.isCtrlAltHover)
- {
- if ((KeyEvent.DOM_VK_CONTROL == evt.keyCode &&
- !evt.shiftKey && evt.altKey && !evt.metaKey)
- || (KeyEvent.DOM_VK_ALT == evt.keyCode &&
- evt.ctrlKey && !evt.shiftKey && !evt.metaKey))
- {
- isToTranslate = true;
- }
- }
- else
- {
- if (KeyEvent.DOM_VK_CONTROL == evt.keyCode &&
- !evt.shiftKey && !evt.altKey && !evt.metaKey)
- {
- isToTranslate = true;
- }
- }
- if (isToTranslate)
- {
- if (gd12.dicInline.isTranslating)
- {
- gd12debug.log("gd12.keydownHandler isTranslating");
- }
- else
- {
- gd12.hover.keydownTimeoutId =
- window.setTimeout(gd12.hover.translate, 500);
- }
- }
- else
- {
- if (gd12.hover.keydownTimeoutId)
- {
- window.clearTimeout(gd12.hover.keydownTimeoutId);
- gd12.hover.keydownTimeoutId = null;
- }
- gd12.popup.close();
- }
- }
- //gd12debug.log("gd12.keydownHandler ends");
- },
- // decode HTML entity in string
- // e.g. "&s;" -> "&"
- decodeHtmlEntities: function(str)
- {
- if (str.indexOf("&") >= 0)
- {
- var div = getBrowser().contentDocument.createElement("div");
- div.innerHTML = str;
- return div.textContent;
- }
- return str;
- },
- select : {
- lookup : function(evt)
- {
- gd12debug.log("gd12.select.lookup starts");
- var myWin = evt.originalTarget.ownerDocument.defaultView;
- gd12.select.mouseX = evt.screenX;
- gd12.select.mouseY = evt.screenY;
- gd12.select.lookupInWin(myWin);
- gd12debug.log("gd12.select.lookup ends");
- },
- lookupSelected : function()
- {
- gd12debug.log("gd12.select.lookupSelected starts");
- var myWin = window.content;
- // gd12.select.mouseX and gd12.select.mouseY were set by
- // gd12.clickHandler()
- if (myWin)
- gd12.select.lookupInWin(myWin);
- gd12debug.log("gd12.select.lookupSelected ends");
- },
- lookupInWin : function(myWin)
- {
- gd12debug.log("gd12.select.lookupInWin starts");
- if (myWin.location.protocol == "https:")
- gd12.dicInline.protocol = "https:";
- else
- gd12.dicInline.protocol = "http:";
- var selected = myWin.getSelection();
- gd12debug.log("gd12.select.lookupInWin selected:" + selected);
- if (selected)
- {
- var word = selected.toString();
- if (word && word.length > 0)
- {
- gd12debug.log("gd12.select.lookupInWin word:" + word);
- gd12.dicInline.translate(word, gd12.select.showPopup);
- }
- }
- gd12debug.log("gd12.select.lookupInWin ends");
- },
- showPopup: function()
- {
- gd12.popup.open(gd12.select.mouseX, gd12.select.mouseY + 18);
- },
- mouseX : 0,
- mouseY : 0,
- }, // gd12.select
- hover : {
- capture : function(evt, isToTranslate)
- {
- //gd12debug.log("gd12.hover.capture starts");
- gd12.hover.word = null;
- if (gd12.dicInline.isTranslating)
- {
- return;
- }
- if (!evt.rangeParent || !evt.rangeParent.nodeType ||
- (evt.rangeParent.nodeType != Node.TEXT_NODE))
- {
- return;
- }
- var myWin = evt.originalTarget.ownerDocument.defaultView;
- if (myWin.location.protocol == "https:")
- gd12.dicInline.protocol = "https:";
- else
- gd12.dicInline.protocol = "http:";
- gd12.hover.mouseX = evt.screenX;
- gd12.hover.mouseY = evt.screenY;
- if (gd12.hover.mousemoveTimeoutId)
- window.clearTimeout(gd12.hover.mousemoveTimeoutId);
- gd12.hover.mousemoveTimeoutId = window.setTimeout(
- gd12.hover.getPointedWord,
- 500,
- evt.rangeParent,
- evt.rangeOffset,
- gd12.hover.mouseX,
- gd12.hover.mouseY,
- isToTranslate);
- //gd12debug.log("gd12.hover.capture ends");
- },
- getPointedWord : function(parnt, offset, x, y, isToTranslate)
- {
- gd12debug.log("gd12.hover.getPointedWord starts: offset:" + offset
- + ", x:" + x + ", y:" + y);
- gd12.hover.word = null;
- if (gd12.hover.mouseX != x || gd12.hover.mouseY != y)
- {
- gd12debug.log("gd12.hover.getPointedWord Mouse moved away");
- return;
- }
- var text = parnt.textContent;
- if (text && text.length > 0)
- {
- gd12.hover.word = gd12.hover.getWord(text, offset);
- if (isToTranslate)
- gd12.dicInline.translate(gd12.hover.word, gd12.hover.showPopup);
- }
- gd12debug.log("gd12.hover.getPointedWord ends");
- },
- getWord: function(text, offset)
- {
- gd12debug.log("gd12.hover.getWord starts: text:" + text + ", offset:" + offset);
- var ch = text.charAt(offset);
- gd12debug.log("gd12.hover.getWord charAt(offset):" + ch);
- if (!ch || ch.length == 0 || gd12.hover.isSymbol(ch))
- {
- gd12debug.log("gd12.hover.getWord: no word");
- return null;
- }
- var len = text.length;
- var s = offset;
- var e = offset + 1;
- while (s > 0)
- {
- s--;
- ch = text.charAt(s);
- if (gd12.hover.isSymbol(ch))
- {
- // separator
- s++;
- break;
- }
- }
- while (e < len)
- {
- ch = text.charAt(e);
- if (gd12.hover.isSymbol(ch))
- {
- // separator
- break;
- }
- e++;
- }
- var word = text.substring(s, e);
- gd12debug.log("gd12.hover.getWord returns: " + word);
- return word;
- },
- isSymbol: function(ch)
- {
- if (ch < 'A' ||
- (ch > 'Z' && ch <'a') ||
- (ch > 'z' && ch <= '\x7f') ||
- (ch >= '\u0080' && ch <= '\u00BF') ||
- (ch >= '\u3000' && ch <= '\u303F') ||
- (ch >= '\uFF01' && ch <= '\uFF20') ||
- (ch >= '\uFF3B' && ch <= '\uFF40') ||
- (ch >= '\uFF5B' && ch <= '\uFF5E'))
- {
- return true;
- }
- else
- return false;
- },
- showPopup: function()
- {
- // gd12.hover.showPopup
- gd12.popup.open(gd12.hover.mouseX, gd12.hover.mouseY + 18);
- },
- translate: function()
- {
- // gd12.hover.translate
- // after 500ms delay. we need to check the isTranslating flag again.
- if (!gd12.dicInline.isTranslating)
- {
- gd12.dicInline.translate(gd12.hover.word, gd12.hover.showPopup);
- }
- },
- mouseX : 0,
- mouseY : 0,
- mousemoveTimeoutId : null,
- keydownTimeoutId : null,
- word : null,
- }, // gd12.hover
- dicInline : {
- translate: function(word, callback)
- {
- gd12debug.log("gd12.dicInline.translate starts");
- if (gd12.isInlineDicOff)
- return;
- // reset
- gd12.resp.isTranslated = false;
- gd12.dicInline.word = null;
- gd12.dicInline.pendingReqCount = 0;
- if (word && word.length > 0)
- {
- word = word.replace(/^\s+|\s+$/g, ""); // trim spaces.
- gd12.dicInline.word = word;
- callback(); // ...showPopup()
- var ori = document.getElementById("gd12popupOrig");
- ori.textContent = gd12.dicInline.word;
- if (gd12.prefs.isDetectLangDisabled)
- {
- if (gd12.lang.isSupportedByGDic(gd12.prefs.srcLang, gd12.prefs.yourLang))
- {
- gd12.dicInline.lookupGDic(gd12.prefs.srcLang);
- gd12.dicInline.pendingReqCount++;
- if (gd12.prefs.srcLang != gd12.prefs.yourLang)
- {
- gd12.dicInline.lookupGTran(gd12.prefs.srcLang);
- gd12.dicInline.pendingReqCount++;
- }
- }
- else
- {
- gd12.dicInline.lookupGTran(gd12.prefs.srcLang);
- gd12.dicInline.pendingReqCount++;
- }
- }
- else
- {
- if (gd12.lang.isSupportedByGDic(gd12.prefs.yourLang, gd12.prefs.yourLang))
- {
- gd12.dicInline.lookupGDic(gd12.prefs.yourLang);
- gd12.dicInline.pendingReqCount++;
- }
- gd12.dicInline.lookupGTran(""); // use auto-detection.
- gd12.dicInline.pendingReqCount++;
- }
- }
- gd12debug.log("gd12.dicInline.translate ends");
- },
- lookupGTran : function(srcLang)
- {
- var url = gd12.dicInline.protocol
- + gd12.dicInline.urlGtrnPrefix
- + gd12.dicInline.word;
- url = url.replace("GD12_YOUR_LANG", gd12.prefs.yourLang);
- url = url.replace("GD12_SRC_LANG", srcLang);
- gd12debug.log("gd12.dicInline.lookupGTran url: " + url);
- var req = XMLHttpRequest();
- req.open("GET", url, true);
- req.onreadystatechange = function () {
- if (req.readyState == 4)
- {
- gd12.dicInline.processRespGTran(req.responseText);
- }
- };
- req.send(null);
- },
- processRespGTran : function(respText)
- {
- gd12.resp.parse(respText);
- gd12.popup.write();
- // Go query Google Dictionary for details.
- if (gd12.resp.src && gd12.resp.src.length > 0 &&
- gd12.resp.src != gd12.prefs.yourLang &&
- gd12.lang.isSupportedByGDic(gd12.resp.src, gd12.prefs.yourLang))
- {
- gd12.dicInline.lookupGDic(gd12.resp.src);
- }
- else
- {
- // this task is done.
- gd12.dicInline.pendingReqCount--;
- }
- gd12.dicInline.updatePopupHint();
- },
- lookupGDic : function(srcLang)
- {
- var url = gd12.dicInline.protocol
- + gd12.dicInline.urlGdicPrefix
- + gd12.dicInline.word;
- url = url.replace("GD12_YOUR_LANG", gd12.prefs.yourLang);
- url = url.replace("GD12_SRC_LANG", srcLang);
- gd12debug.log("gd12.dicInline.lookupGDic url: " + url);
- var gotResult = false;
- var req = XMLHttpRequest();
- req.open("GET", url, true);
- req.onreadystatechange = function () {
- if (gotResult)
- return;
- if (req.readyState == 3) // downloading
- {
- gotResult = gd12.dicInline.processRespGDic(req.responseText);
- if (gotResult)
- {
- req.abort();
- gd12.dicInline.pendingReqCount--;
- gd12.dicInline.updatePopupHint();
- }
- }
- else if (req.readyState == 4) // done
- {
- gotResult = gd12.dicInline.processRespGDic(req.responseText);
- gd12.dicInline.pendingReqCount--;
- gd12.dicInline.updatePopupHint();
- }
- };
- req.send(null);
- },
- processRespGDic : function(respText)
- {
- var hasResult = false;
- gd12debug.log("gd12.dicInline.processRespGDic resp len: " + respText.length);
- if (respText && respText.length > 0)
- {
- // extract lookup result from the response.
- // only when the response strictly matches the format:
- // meta name="description" content="<the meaning> - Google..."
- var ss = 'meta name="description" content="';
- var i1 = respText.indexOf(ss);
- if (i1 > 0)
- {
- i1 += ss.length;
- var i2 = respText.indexOf('"', i1);
- if (i2 > 0)
- {
- // found <meta name="description" content="........">
- // set the return value.
- hasResult = true;
- var subs = respText.substring(i1, i2);
- var i3 = subs.indexOf(" - Google");
- if (i3 > 0)
- {
- subs = subs.substring(0, i3);
- subs = subs.replace(/^\s+|\s+$/g, ""); // trim spaces.
- if (subs.length > 0)
- {
- // append to the details field in the popup.
- var det = document.getElementById("gd12popupDetails");
- var curText = det.textContent;
- if (curText && curText.length > 0)
- {
- det.textContent = curText + "\n"
- + gd12.decodeHtmlEntities(subs);
- }
- else
- det.textContent = gd12.decodeHtmlEntities(subs);
- gd12debug.log("gd12.dicInline.processRespGDic details:"
- + det.textContent);
- // clean the translate field if it is just
- // the duplicate of the original word,
- // or error("??")
- var tra = document.getElementById("gd12popupTrans");
- if (tra.textContent == gd12.dicInline.word ||
- tra.textContent == gd12.STR_ERROR)
- {
- tra.textContent = "";
- }
- }
- }
- }
- }
- }
- gd12debug.log("gd12.dicInline.processRespGDic ends: " + hasResult);
- return hasResult;
- },
- buildMoreUrlAfterLangDetect : function()
- {
- var url = null
- if (gd12.resp.src && gd12.resp.src.length > 0 &&
- gd12.lang.isSupportedByGDic(gd12.resp.src, gd12.prefs.yourLang))
- {
- url = gd12.dicInline.protocol
- + gd12.dicInline.urlGdicPrefix
- + gd12.dicInline.word;
- url = url.replace("GD12_YOUR_LANG", gd12.prefs.yourLang);
- url = url.replace("GD12_SRC_LANG", gd12.resp.src);
- }
- else if (gd12.dicInline.protocol == "http:")
- {
- url = gd12.dicInline.urlMoreGtrnPrefix + gd12.dicInline.word;
- url = url.replace("GD12_YOUR_LANG", gd12.prefs.yourLang);
- if (gd12.prefs.isDetectLangDisabled)
- url = url.replace("GD12_SRC_LANG", gd12.prefs.srcLang);
- else
- url = url.replace("GD12_SRC_LANG", "auto");
- }
- return url;
- },
- buildMoreUrlInit : function()
- {
- var url = null;
- if (gd12.dicInline.word &&
- gd12.prefs.isDetectLangDisabled &&
- gd12.lang.isSupportedByGDic(gd12.prefs.srcLang, gd12.prefs.yourLang))
- {
- url = gd12.dicInline.protocol
- + gd12.dicInline.urlGdicPrefix
- + gd12.dicInline.word;
- url = url.replace("GD12_YOUR_LANG", gd12.prefs.yourLang);
- url = url.replace("GD12_SRC_LANG", gd12.prefs.srcLang);
- }
- else
- {
- // translate.google.com does not support https yet.
- if (gd12.dicInline.protocol == "http:" && gd12.dicInline.word)
- {
- url = gd12.dicInline.urlMoreGtrnPrefix + gd12.dicInline.word;
- url = url.replace("GD12_YOUR_LANG", gd12.prefs.yourLang);
- if (gd12.prefs.isDetectLangDisabled)
- url = url.replace("GD12_SRC_LANG", gd12.prefs.srcLang);
- else
- url = url.replace("GD12_SRC_LANG", "auto");
- }
- }
- return url;
- },
- updatePopupHint : function()
- {
- if (gd12.dicInline.pendingReqCount <= 0)
- {
- // All queries are done.
- // Clean the string "Googling ..."
- var hnt = document.getElementById("gd12popupHint");
- hnt.textContent = "";
- }
- },
- urlGtrnPrefix : "//ajax.googleapis.com/ajax/services/language/translate?v=1.0&langpair=GD12_SRC_LANG%7CGD12_YOUR_LANG&q=",
- urlGdicPrefix : "//www.google.com/dictionary?aq=f&langpair=GD12_SRC_LANG%7CGD12_YOUR_LANG&q=",
- urlMoreGtrnPrefix : "http://translate.google.com/#GD12_SRC_LANG%7CGD12_YOUR_LANG%7C", // https not supported
- isTranslating : false,
- pendingReqCount : 0,
- protocol : "http:",
- word : null,
- }, // gd12.dicInline
- popup : {
- open : function(x, y)
- {
- gd12debug.log("gd12.popup.open starts");
- if (!gd12.popup.popup)
- gd12.popup.popup = document.getElementById("gd12popup");
- if (gd12.popup.popup)
- {
- // clean up.
- var ori = document.getElementById("gd12popupOrig");
- ori.textContent = "";
- var tra = document.getElementById("gd12popupTrans");
- tra.textContent = "";
- var puMore = document.getElementById("gd12popupMore");
- var url = gd12.dicInline.buildMoreUrlInit();
- if (url)
- {
- var onclk = "gd12.popup.close();" +
- "gBrowser.selectedTab = gBrowser.addTab('" + url + "');";
- gd12debug.log("gd12.popup.open onclk:" + onclk);
- puMore.setAttribute("class", "gDicClassPopupMore");
- puMore.setAttribute("onclick", onclk);
- }
- else
- {
- puMore.setAttribute("class", "gDicClassPopupMoreDisabled");
- if (puMore.hasAttribute("onclick"))
- puMore.removeAttribute("onclick");
- }
- var det = document.getElementById("gd12popupDetails");
- det.textContent = "";
- var hnt = document.getElementById("gd12popupHint");
- hnt.textContent = gd12.STR_SEARCH;
- gd12.popup.popup.openPopupAtScreen(x, y, false);
- }
- gd12debug.log("gd12.popup.open ends.");
- },
- close : function()
- {
- gd12debug.log("gd12.popup.close starts");
- if (gd12.popup.popup)
- {
- if (gd12.popup.popup.state == "open")
- gd12.popup.popup.hidePopup();
- }
- gd12debug.log("gd12.popup.close ends");
- },
- on : function()
- {
- gd12debug.log("gd12.popup.on starts");
- // if popup is open, stop new translation.
- // if we want to change this behavior, remember to change off() too
- gd12.dicInline.isTranslating = true;
- gd12.popup.xWhenPopup = 0;
- gd12.popup.yWhenPopup = 0;
- gd12debug.log("gd12.popup.on ends");
- },
- off : function()
- {
- gd12debug.log("gd12.popup.off starts");
- gd12.dicInline.isTranslating = false;
- gd12debug.log("gd12.popup.off ends");
- },
- write : function()
- {
- gd12debug.log("gd12.popup.write starts");
- if (gd12.resp.isTranslated)
- {
- var ori = document.getElementById("gd12popupOrig");
- ori.textContent = gd12.dicInline.word;
- var tra = document.getElementById("gd12popupTrans");
- tra.textContent = gd12.decodeHtmlEntities(gd12.resp.trans);
- if (gd12.resp.src)
- {
- // the link of "more"
- var url = gd12.dicInline.buildMoreUrlAfterLangDetect();
- if (url)
- {
- var onclk = "gd12.popup.close();" +
- "gBrowser.selectedTab = gBrowser.addTab('" + url + "');";
- gd12debug.log("gd12.popup.write onclk:" + onclk);
- var puMore = document.getElementById("gd12popupMore");
- if (puMore)
- {
- puMore.setAttribute("class", "gDicClassPopupMore");
- puMore.setAttribute("onclick", onclk);
- }
- }
- }
- }
- else
- {
- // JSON.parse error or any errors in inline translation respone.
- var ori = document.getElementById("gd12popupOrig");
- ori.textContent = gd12.dicInline.word;
- var tra = document.getElementById("gd12popupTrans");
- tra.textContent = gd12.STR_ERROR;
- }
- gd12debug.log("gd12.popup.write ends");
- },
- popup : null,
- xWhenPopup : 0,
- yWhenPopup : 0,
- }, // gd12.popup
- resp : {
- parse : function(jsonResp)
- {
- gd12debug.log("gd12.resp.parse starts: " + jsonResp);
- gd12.resp.src = null,
- gd12.resp.trans = null,
- gd12.resp.isTranslated = false;
- var jrsp;
- try
- {
- jrsp = JSON.parse(jsonResp);
- }
- catch (e)
- {
- gd12debug.log("gd12.resp.parse exception");
- return;
- }
- if (jrsp)
- {
- if (jrsp.responseStatus && jrsp.responseStatus == 200)
- {
- // good response.
- if (jrsp.responseData)
- {
- if (jrsp.responseData.translatedText)
- {
- gd12.resp.trans = jrsp.responseData.translatedText;
- gd12.resp.isTranslated = true;
- }
- if (jrsp.responseData.detectedSourceLanguage)
- {
- gd12.resp.src = jrsp.responseData.detectedSourceLanguage;
- }
- }
- }
- }
- gd12debug.log("gd12.resp.parse ends: isTranslated: " + gd12.resp.isTranslated);
- },
- src : null, // detected source language
- trans : null,
- isTranslated : false,
- }, // gd12.resp
- translatePage : {
- run : function()
- {
- gd12debug.log("gd12.translatePage.run starts");
- var doc = getBrowser().contentDocument;
- var s1 = doc.createElement("script");
- s1.type = "text/javascript";
- s1.innerHTML = gd12.translatePage.func.replace(
- "GD12_YOUR_LANG", gd12.prefs.yourLang);
- var s2 = doc.createElement("script");
- s2.type = "text/javascript";
- s2.src = gd12.translatePage.url.replace(
- "GD12_YOUR_LANG", gd12.prefs.yourLang);
- var elm = doc.getElementsByTagName("head")[0];
- if (elm)
- {
- elm.appendChild(s1);
- elm.appendChild(s2);
- }
- gd12debug.log("gd12.translatePage.run ends");
- },
- func : "function googleTranslateElementInit() { var tElem = new google.translate.TranslateElement({ autoDisplay: false, pageLanguage: 'auto', includedLanguages: 'GD12_YOUR_LANG' }); setTimeout(function() { tElem.showBanner(true); }, 100);}", // showBanner(autoTrans=true)
- url : "//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit&hl=GD12_YOUR_LANG",
- }, // gd12.translatePage
- prefs : {
- read : function()
- {
- gd12debug.log("gd12.prefs.read starts");
- var prefs = Components.classes["@mozilla.org/preferences-service;1"]
- .getService(Components.interfaces.nsIPrefService);
- var myprefs = prefs.getBranch("extensions.gdic_gtrans.");
- gd12.prefs.yourLang = myprefs.getCharPref("yourLanguage");
- gd12.prefs.isSelect = myprefs.getBoolPref("enableSelect");
- gd12.prefs.isDoubleClick = myprefs.getBoolPref("enableDoubleClick");
- gd12.prefs.isCtrlHover = myprefs.getBoolPref("enableCtrlHover");
- gd12.prefs.isCtrlShiftHover = gd12.prefs.isCtrlHover && myprefs.getBoolPref("isCtrlShiftHover");
- gd12.prefs.isCtrlAltHover = gd12.prefs.isCtrlHover && myprefs.getBoolPref("isCtrlAltHover");
- gd12.prefs.isAggressive = myprefs.getBoolPref("aggressiveMode");
- gd12.prefs.isMouseMoveClosePopup = myprefs.getBoolPref("mouseMoveClosePopup");
- gd12.prefs.isGDicInContextMenu = myprefs.getBoolPref("gdicInContextMenu");
- gd12.prefs.isGTransInContextMenu = myprefs.getBoolPref("gtransInContextMenu");
- gd12.prefs.isDetectLangDisabled = myprefs.getBoolPref("disableAutoDetectLanguage");
- gd12.prefs.srcLang = myprefs.getCharPref("srcLanguage");
- gd12.prefs.isMyStyle = myprefs.getBoolPref("enableMyStyle");
- gd12.prefs.myStyle = myprefs.getCharPref("myStyle");
- if (gd12.prefs.yourLang.length == 0)
- {
- // try to set as the browser's language.
- var gprefs = prefs.getBranch("general.useragent.");
- if (gprefs)
- {
- var browserLang = gprefs.getCharPref("locale");
- if (browserLang)
- {
- if (browserLang == "zh-CN" || browserLang == "zh-TW")
- {
- gd12.prefs.yourLang = browserLang;
- }
- else
- {
- if (browserLang.length > 2 && browserLang.charAt(2) == "-")
- {
- // google dictionary/translate does not
- // recognize sub-categories.
- gd12.prefs.yourLang = browserLang.substr(0, 2);
- }
- else
- {
- gd12.prefs.yourLang = browserLang;
- }
- }
- gd12debug.log("gd12.prefs.read browser lang:" +
- gd12.prefs.yourLang);
- }
- }
- // set language to english if we don't support the locale.
- if (gd12.prefs.yourLang.length < 2 ||
- !gd12.lang.isSupportedByGTran(gd12.prefs.yourLang))
- {
- gd12.prefs.yourLang = "en";
- }
- // write it to prefs so that we don't need to auto-set it again.
- myprefs.setCharPref("yourLanguage", gd12.prefs.yourLang);
- }
- gd12.prefs.applyUserStyle();
- gd12debug.log("gd12.prefs.read ends");
- },
- applyUserStyle : function()
- {
- gd12debug.log("gd12.prefs.applyUserStyle starts");
- var sss = Components.classes["@mozilla.org/content/style-sheet-service;1"]
- .getService(Components.interfaces.nsIStyleSheetService);
- var ios = Components.classes["@mozilla.org/network/io-service;1"]
- .getService(Components.interfaces.nsIIOService);
- if (gd12.prefs.isMyStyle)
- {
- // try apply user style
- if (gd12.prefs.myRegisteredStyle == gd12.prefs.myStyle)
- {
- gd12debug.log("gd12.prefs.applyUserStyle already regsitered");
- return;
- }
- // unregister the old user style
- if (gd12.prefs.myRegisteredStyle.length > 0)
- {
- var uri = ios.newURI(
- gd12.prefs.buildCssString(gd12.prefs.myRegisteredStyle),
- null, null);
- if(sss.sheetRegistered(uri, sss.USER_SHEET))
- sss.unregisterSheet(uri, sss.USER_SHEET);
- gd12.prefs.myRegisteredStyle = "";
- }
- // register the new user style
- if (gd12.prefs.myStyle.length > 0)
- {
- var uri = ios.newURI(
- gd12.prefs.buildCssString(gd12.prefs.myStyle),
- null, null);
- if(!sss.sheetRegistered(uri, sss.USER_SHEET))
- sss.loadAndRegisterSheet(uri, sss.USER_SHEET);
- gd12.prefs.myRegisteredStyle = gd12.prefs.myStyle;
- }
- }
- else
- {
- // remove user style
- if (gd12.prefs.myRegisteredStyle.length > 0)
- {
- var uri = ios.newURI(
- gd12.prefs.buildCssString(gd12.prefs.myRegisteredStyle),
- null, null);
- if(sss.sheetRegistered(uri, sss.USER_SHEET))
- sss.unregisterSheet(uri, sss.USER_SHEET);
- gd12.prefs.myRegisteredStyle = "";
- }
- }
- gd12debug.log("gd12.prefs.applyUserStyle ends");
- },
- buildCssString : function(styleStr)
- {
- var cssStr = "data:text/css," +
- "@-moz-document url-prefix(chrome://) {" +
- encodeURIComponent(styleStr) +
- "}";
- return cssStr;
- },
- yourLang : "",
- isSelect : false,
- isDoubleClick : true,
- isCtrlHover : true,
- isCtrlShiftHover : false,
- isCtrlAltHover : false,
- isAggressive : true,
- isMouseMoveClosePopup : false,
- isDetectLangDisabled : false,
- srcLang : "en",
- isMyStyle : false,
- myStyle : "",
- myRegisteredStyle : "",
- isGDicInContextMenu : true,
- isGTransInContextMenu : true,
- }, // gd12.prefs
- lang : {
- // Whether google dic supports the translation between lang1 and lang2
- isSupportedByGDic : function(lang1, lang2)
- {
- gd12debug.log("gd12.lang.isSupportedByGDic starts");
- var res = false;
- for (var i = 0; i < gd12.lang.gdicSupported.length; i++)
- {
- if ((lang1 == gd12.lang.gdicSupported[i][0] &&
- lang2 == gd12.lang.gdicSupported[i][1])
- ||
- (lang1 == gd12.lang.gdicSupported[i][1] &&
- lang2 == gd12.lang.gdicSupported[i][0]))
- {
- res = true;
- break;
- }
- }
- gd12debug.log("gd12.lang.isSupportedByGDic ends: " + res);
- return res;
- },
- isSupportedByGTran : function(lang)
- {
- gd12debug.log("gd12.lang.isSupportedByGTran starts");
- var res = false;
- for (var i = 0; i < gd12.lang.gtranSupported.length; i++)
- {
- if (lang == gd12.lang.gtranSupported[i])
- {
- res = true;
- break;
- }
- }
- gd12debug.log("gd12.lang.isSupportedByGTran ends: " + res);
- return res;
- },
- gdicSupported : [
- ["ar", "en"],
- ["bg", "en"],
- ["zh-CN", "en"],
- ["zh-CN", "zh-CN"],
- ["zh-TW", "en"],
- ["zh-TW", "zh-TW"],
- ["hr", "en"],
- ["cs", "en"],
- ["cs", "cs"],
- ["nl", "nl"],
- ["en", "en"],
- ["fi", "en"],
- ["fr", "en"],
- ["fr", "fr"],
- ["de", "en"],
- ["de", "de"],
- ["el", "en"],
- ["iw", "en"],
- ["hi", "en"],
- ["it", "en"],
- ["it", "it"],
- ["ko", "en"],
- ["ko", "ko"],
- ["pt", "en"],
- ["pt", "pt"],
- ["ru", "en"],
- ["ru", "ru"],
- ["sr", "en"],
- ["es", "en"],
- ["es", "es"],
- ["th", "en"]
- ],
- gtranSupported : [
- "ar",
- "bg",
- "zh-CN",
- "zh-TW",
- "hr",
- "cs",
- "nl",
- "en",
- "fi",
- "fr",
- "de",
- "el",
- "iw",
- "hi",
- "it",
- "ko",
- "pt",
- "ru",
- "sr",
- "es",
- "th",
- "af",
- "sq",
- "be",
- "ca",
- "da",
- "et",
- "tl",
- "gl",
- "ht",
- "hu",
- "is",
- "id",
- "ga",
- "ja",
- "lv",
- "lt",
- "mk",
- "ms",
- "mt",
- "no",
- "fa",
- "pl",
- "ro",
- "sk",
- "sl",
- "sw",
- "sv",
- "tr",
- "uk",
- "vi",
- "cy",
- "yi"
- ],
- },
- gdButton : {
- clickHandler: function()
- {
- if (gd12.gdButton.singleClickTimer)
- {
- // this is the second click of a double click.
- // cancel the handling of the first onclick event.
- clearTimeout(gd12.gdButton.singleClickTimer);
- gd12.gdButton.singleClickTimer = null;
- }
- else
- {
- // delay a little to see whether it is the first click of a double click.
- gd12.gdButton.singleClickTimer = setTimeout(function() {
- gd12.gdButton.singleClickTimer = null;
- gd12.select.lookupSelected();
- }, 500);
- }
- },
- doubleClickHandler: function()
- {
- // toggle inline dic on/off
- gd12.isInlineDicOff = !gd12.isInlineDicOff;
- var gdbutton = document.getElementById("gd12gdbutton");
- if (gdbutton)
- {
- if (gd12.isInlineDicOff)
- {
- gdbutton.setAttribute("class",
- "toolbarbutton-1 gd12gdbuttondisabled");
- }
- else
- {
- gdbutton.setAttribute("class",
- "toolbarbutton-1 gd12gdbuttonactive");
- }
- }
- },
- singleClickTimer : null,
- }, // gdButton
- STR_ERROR : "??",
- STR_SEARCH : "Googling ...",
- isInlineDicOff : false, // temporary off
- };
- window.addEventListener("load", gd12.init, false);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement