home *** CD-ROM | disk | FTP | other *** search
Text File | 2010-04-16 | 43.8 KB | 1,301 lines |
- var Ya = {
- observerService: Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService),
- nsIYa: Cc["@yandex.ru/yasearch;1"].getService(Ci.nsIYaSearch).wrappedJSObject,
-
- sContextMenu: null,
- lastWord: null,
-
- yaContextSearchId: "yasearchMenuIdPrefix-yandex-search-menu",
- yaContextSearchServicesId: "yasearchMenuIdPrefix-yandex-menu",
-
- onLoad: function() {
- if (typeof(FeedHandler) === "object" && typeof(FeedHandler.updateFeeds) === "function") {
- var origUpdateFeeds = FeedHandler.updateFeeds;
-
- FeedHandler.updateFeeds = function() {
- Ya.updateFeedsAttribute();
- return origUpdateFeeds.apply(this, arguments);
- }
- }
-
- if (typeof(BrowserToolboxCustomizeDone) === "function") {
- var origBrowserToolboxCustomizeDone = BrowserToolboxCustomizeDone;
-
- BrowserToolboxCustomizeDone = function() {
- Ya.realizeLoginBehaviour();
- return origBrowserToolboxCustomizeDone.apply(this, arguments);
- }
- }
-
- this.stringBundleSet = document.getElementById("yasearch-string-bundle-global");
- this.sContextMenu = document.getElementById("contentAreaContextMenu");
-
- if (this.nsIYa.getBoolPref("yasearch.default.search")) {
- let menuElement = document.getElementById("context-searchselect");
- if (menuElement)
- menuElement.setAttribute("style", "display:none !important");
- }
-
- this.realizeLoginBehaviour();
-
- this.observerService.addObserver(this, "Ya-Refresh-Services", false);
- this.observerService.addObserver(this, "Ya-Refresh-Login-Status", false);
- this.observerService.addObserver(this, "Ya-Refresh-Data", false);
- this.observerService.addObserver(this, "Ya-Refresh-Busy-State", false);
-
- this.redrawContextMenu();
- this.sContextMenu.addEventListener("popupshowing", function(e) { Ya.showContextMenu(e) }, false);
- this.sContextMenu.addEventListener("popuphidden", function(e) { Ya.hideContextMenu(e) }, false);
-
- setTimeout(function(){
- Ya._mailIntegration.defaultOnClick =
- document.getElementById("content").onclick ||
- function() {
- return window.contentAreaClick(event, false);
- };
-
- Ya._mailIntegration.defaultWinObject = window.MailIntegration;
- Ya.mailIntegration = Ya.nsIYa.getBoolPref("yasearch.general.ui.mail.integration");
- }, 10);
-
- this.observerService.notifyObservers(null, "Ya-Browser-OpenClose", "open");
-
- // partner item
- let partnerPaletteNode =
- Array.slice.call(document.getElementById("navigator-toolbox").palette.childNodes)
- .filter(function(aNode){return aNode.id == "yasearch-partner"})[0];
-
- if (partnerPaletteNode) {
- var partnerExists = false;
-
- if (this.nsIYa.yaPartner) {
- let toolbaritemContent = this.nsIYa.yaPartner.toolbaritemContent(document);
- if (toolbaritemContent) {
- partnerExists = true;
-
- ["title", "label"].forEach(function(aAttrName) {
- if (toolbaritemContent.hasAttribute(aAttrName))
- partnerPaletteNode.setAttribute(aAttrName, toolbaritemContent.getAttribute(aAttrName));
- })
- }
- }
-
- if (!partnerExists)
- partnerPaletteNode.parentNode.removeChild(partnerPaletteNode);
- }
-
- // XB widgets custom actions helper
- this.log("Installing XB widgets custom actions helper");
- try {
- window.yasearchOverlayController.guiBuilder.registerExtActionsHelper(this);
- }
- catch (e) {
- this.log(e.message);
- this.log(e.stack);
- }
- },
-
- unLoad: function() {
- try {
- window.yasearchOverlayController.guiBuilder.unregisterExtActionsHelper(this);
- }
- catch (e) {}
-
- this.observerService.removeObserver(this, "Ya-Refresh-Services");
- this.observerService.removeObserver(this, "Ya-Refresh-Login-Status");
- this.observerService.removeObserver(this, "Ya-Refresh-Data");
- this.observerService.removeObserver(this, "Ya-Refresh-Busy-State");
-
- this.observerService.notifyObservers(null, "Ya-Browser-OpenClose", "close");
- },
-
- // called by XULBuilder
- handleXBAction: function yso_handleXBAction(aWidgetId, aAction) {
- let widget = window.yasearchOverlayController.windowEngine.getWidget(aWidgetId);
- let widgetProtoId = widget.getPrototype().getID();
-
- if (widgetProtoId == "A78155FC-41D5-4304-BD60-A01D8C1FB9FA" /* quotes widget */) {
- switch (aAction) {
- case "setup":
- var QIDSettingNode = widget.findSetting("quote-id");
- var QListNode = widget.findData("quotes list");
- var quoteID = {value: QIDSettingNode.getValue()};
- window.openDialog(YaUtils.CHROME_CONTENT + "custombar/quote-setup-dialog.xul",
- "",
- "centerscreen,chrome,dialog,modal,resizable,dependent",
- quoteID,
- QListNode);
- QIDSettingNode.setValue(quoteID.value);
- break;
-
- case "add":
- var defaultQuoteID = widget.findSetting("quote-id").getDefaultValue();
- var quoteID = {value: defaultQuoteID};
- var QListNode = widget.findData("quotes list");
- window.openDialog(YaUtils.CHROME_CONTENT + "custombar/quote-setup-dialog.xul",
- "",
- "centerscreen,chrome,dialog,modal,resizable,dependent",
- quoteID,
- QListNode);
- if (quoteID.value === undefined) break;
- window.yasearchOverlayController._barCore.application._overlayProvider
- .insertWidget(widgetProtoId, widget, true, undefined, {"quote-id": quoteID.value});
- break;
-
- case "delete":
- window.yasearchOverlayController._barCore.application._overlayProvider.removeWidget(widget, true);
- /*
- var toolbarItemId = "yasearch.cb-" + widgetProtoId + "-inst-" + widget.getID();
- var widgetToolbarItem = window.document.getElementById(toolbarItemId);
- widgetToolbarItem.parentNode.removeChild(widgetToolbarItem);
- var toolbar = widgetToolbarItem.parentNode;
- while ( toolbar && (toolbar.localName !== "toolbar") ) {
- toolbar = toolbar.parentNode;
- }
- if (toolbar) {
- toolbar.setAttribute("currentset", toolbar.currentSet);
- toolbar.ownerDocument.persist(toolbar.id, "currentset");
- }
- window.yasearchOverlayController.guiBuilder.onWidgetRemoved(widgetToolbarItem);
- */
- break;
-
- default:
- throw new Error("Unsupported action");
- }
- }
- },
-
- redrawSearchBoxPopup: function() {
- let searchTB = document.getElementById("yasearch-textbox");
- if (searchTB)
- searchTB.rebuildPopupServices();
- },
-
- //[2do] for mainKeyset
- focusSearch: function() {
- let searchTB = document.getElementById("yasearchbox");
- if (searchTB) {
- searchTB.focus();
- searchTB.select();
- }
- },
-
- get buttonsObject() {
- let buttons = {
- get navigElements() {
- return !!(this.bloggers || this.cy || this.spam);
- },
-
- get isAnyVisibleAuthElement() {
- //...["lenta", "mail", "money", "fotki", "yaru", "moikrug"]
- }
- };
-
- ["cy", "spam", "bloggers", "lenta", "mail", "money", "fotki", "yaru", "translate", "moikrug"]
- .forEach(function(aButtonName) {
- this[aButtonName] = !!document.getElementById("yasearch-" + aButtonName);
- }, buttons);
-
- return buttons;
- },
-
- buildFeedsMenu: function() {
- var XULNS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
-
- let addFeedsMenu = document.createElementNS(XULNS, "menulist");
- addFeedsMenu.appendChild(document.createElementNS(XULNS, "menupopup"));
-
- let feeds = ("feeds" in gBrowser.mCurrentBrowser) ? gBrowser.mCurrentBrowser.feeds : null;
-
- if (!feeds || feeds.length == 0)
- return false;
-
- for each (var feed in feeds) {
- let newFeed = document.createElementNS(XULNS, "menuitem");
- let baseTitle = feed.title || feed.href;
- newFeed.setAttribute("label", baseTitle);
- newFeed.setAttribute("tooltiptext", feed.href);
- addFeedsMenu.firstChild.appendChild(newFeed);
- }
-
- window.openDialog(YaUtils.CHROME_CONTENT + "feeds/add-dialog.xul", "",
- "centerscreen,chrome,dialog,resizable,dependent",
- addFeedsMenu, gBrowser.mCurrentBrowser.contentTitle);
-
- return true;
- },
-
- updateFeedsAttribute: function() {
- let lentaTBItem = document.getElementById("yasearch-lenta");
- if (lentaTBItem) {
- setTimeout(function(me) {
- let feeds = ("feeds" in gBrowser.mCurrentBrowser) ? gBrowser.mCurrentBrowser.feeds : null;
- lentaTBItem.setAttribute("hasFeeds", !(!feeds || feeds.length == 0));
-
- if (lentaTBItem.firstChild.open)
- me.onLentaPopupshowing();
- }, 0, this);
- }
- },
-
- onLentaPopupshowing: function() {
- let lentaTBItem = document.getElementById("yasearch-lenta");
- let lentaMenuPopup = document.getElementById("yasearch-lenta-menupopup");
-
- if (lentaTBItem && lentaMenuPopup)
- lentaMenuPopup.lastChild.previousSibling
- .setAttribute("disabled", !(lentaTBItem.getAttribute("hasFeeds") == "true"));
- },
-
- addBookmark: function(aFolderId, aExistId) {
- window.openDialog(YaUtils.CHROME_CONTENT + "bookmarks/add-dialog.xul", "",
- "centerscreen,chrome,dialog,resizable,dependent",
- gBrowser.mCurrentBrowser, aFolderId, aExistId);
- },
-
- loadCY: function(aEvent, aType) {
- let location = gBrowser.selectedBrowser.webNavigation.currentURI;
-
- try {
- if (location.scheme && (location.scheme == "http" || location.scheme == "https") && location.host.length >= 3) {
- switch (aType) {
- case "similar":
- this.loadURI("http://yandex.ru/yandsearch?ds=" + encodeURIComponent(location.spec), aEvent);
- break;
-
- case "all":
- this.loadURI("http://yandex.ru/yandsearch?serverurl=" + encodeURIComponent(location.host), aEvent);
- break;
-
- default:
- this.loadURI("http://www.yandex.ru/cy?host=" + encodeURIComponent(location.host) + "&base=0",
- aEvent, {action:3200});
- break;
- }
- }
- } catch(e) {}
- },
-
- setCY: function(aCYData) {
- let spamButton = document.getElementById("yasearch-spam-button");
- if (spamButton) {
- if (aCYData && aCYData.spam) {
- spamButton.disabled = false;
-
- let onCommandTxt = "Ya.confirmCYSpamPromt('" + aCYData.spam + "', event.target)";
- spamButton.setAttribute("oncommand", "if (event.target == this) " + onCommandTxt);
- document.getElementById("yasearch-spam-button-confirm-item").setAttribute("oncommand", onCommandTxt);
- } else {
- spamButton.disabled = true;
- }
- }
-
- let cyButton = document.getElementById("yasearch-cy-button");
- if (cyButton)
- cyButton.setCYData(aCYData);
- },
-
- confirmCYSpamPromt: function(aUrl, aElement) {
- let rv = this.nsIYa.promptService.confirm(window,
- this.stringBundleSet.getString("ConfirmCYSpamTitle"),
- this.stringBundleSet.getString("ConfirmCYSpamMessage"));
- if (rv)
- this.nsIYa.confirmCYSpam(aUrl, aElement);
- },
-
- get personalToolbarItems() {
- return YaUtils.evaluateXPath(document, "//xul:toolbaritem[starts-with(@class,'yasearch-login')]");
- },
-
- realizeLoginBehaviour: function() {
- let isLogin = this.nsIYa.isLogin;
-
- let reg = isLogin ? /(\s|^)yasearch\-login\-not(\s|$)/ : /(\s|^)yasearch\-login(\s|$)/;
- let repl = "$1yasearch-login" + (isLogin ? "" : "-not") + "$2";
-
- this.personalToolbarItems.forEach(function(item) {
- item.className = item.className.replace(reg, repl);
-
- let ch, i = 0;
-
- while ((ch = item.childNodes.item(i++))) {
- if (ch.hasAttribute("yaAuthDisabled"))
- ch.setAttribute("yaAuthDisabled", !isLogin);
- }
- });
-
- this.updateServicesWork();
- },
-
- refreshLoginButton: function() {
- let lButton = document.getElementById("yasearch-login-button");
- if (lButton) {
- let login = this.nsIYa.session.login;
- let tooltiptext = login ? this.stringBundleSet.getString("user") + ": " + login :
- lButton.getAttribute("_deftooltiptext");
-
- lButton.setAttribute("yaLabel", login || "0");
- lButton.setAttribute("tooltiptext", tooltiptext);
- }
- },
-
- checkOnCommandEvent: function(aEvent) {
- let target = aEvent.target;
- let currentTarget = aEvent.currentTarget;
-
- if (!(target == currentTarget || target.localName == "toolbarbutton"))
- return false;
-
- if (currentTarget.localName !== "toolbaritem" && currentTarget.localName !== "toolbarbutton") {
- target = null;
-
- let currentTargetId = currentTarget.getAttribute("id");
- if (currentTargetId && currentTargetId.indexOf("-yachevron-clone") > 0) {
- let chevronCloned = currentTargetId.split("-yachevron-clone")[0];
- target = document.getElementById(chevronCloned);
- }
-
- if (!target)
- return false;
- }
-
- if (target.disabled && !this.nsIYa.isLogin) {
- let parentNode = target.parentNode;
-
- while (parentNode && parentNode.localName != "toolbaritem")
- parentNode = parentNode.parentNode;
-
- if (parentNode && /yasearch\-login/.test(parentNode.getAttribute("class"))) {
- var [,serviceType] = parentNode.getAttribute("id").split("-");
- this.openWelcomeWindow(serviceType, target.getAttribute("id"));
- return false;
- }
- }
-
- return true;
- },
-
- updateServicesWork: function() {
- this.refreshLoginButton();
- this.redrawServicesButton();
- this.refreshMailData();
- this.refreshCounterData("fotki");
- this.refreshCounterData("yaru");
- this.refreshCounterData("moikrug");
- this.refreshLentaData();
- this.refreshMoneyData();
- },
-
- redrawServices: function() {
- this.redrawServicesButton();
- this.redrawContextMenu();
- this.redrawSearchBoxPopup();
- },
-
- redrawServicesButton: function() {
- let sButton = document.getElementById("yasearch-search");
- if (!sButton)
- return;
-
- if (sButton.firstChild)
- sButton.removeChild(sButton.firstChild);
-
- this.nsIYa.DOMUtils.appendNode(this.nsIYa.getDOMDocContent("bar-services"), sButton);
- },
-
- showQuickTranslateItems: function(aLabel) {
- if (!this.nsIYa.getBoolPref("yasearch.general.ui.quick.search"))
- aLabel = false;
-
- for each (var qtItemId in ["lingvo"]) {
- var mItem = document.getElementById("yasearchQTranslateItem-" + qtItemId);
- if (mItem) {
- mItem.hidden = aLabel ? false : true;
- if (aLabel)
- mItem.setAttribute("label", this.stringBundleSet.getFormattedString("search_" + qtItemId, [aLabel]));
- }
- }
- },
-
- redrawContextMenu: function() {
- let oldMenu = document.getElementById(this.yaContextSearchServicesId);
- if (oldMenu) {
- let newMenu = this.nsIYa.getDOMDocContent("menu-popup");
- this.nsIYa.DOMUtils.replaceNode(newMenu, oldMenu);
- }
- },
-
- getBrowserSelection: function() {
- let focusedWindow = document.commandDispatcher.focusedWindow;
- let selection = focusedWindow.getSelection().toString();
-
- return this._cropSelectionText(selection);
- },
-
- _cropSelectionText: function(aString) {
- const CHAR_LEN = 100;
-
- if (aString) {
- if (aString.length > CHAR_LEN) {
- var pattern = new RegExp("^(?:\\s*.){0," + CHAR_LEN + "}");
- pattern.test(aString);
- aString = RegExp.lastMatch;
- }
-
- aString = aString.replace(/^\s+/, "").replace(/\s+$/, "").replace(/\s+/g, " ");
-
- if (aString.length > CHAR_LEN)
- aString = aString.substr(0, CHAR_LEN);
- }
-
- return aString;
- },
-
- showContextMenu: function(evt) {
- if (evt.target.id != this.sContextMenu.id)
- return;
-
- let yaContextMenu = document.getElementById(this.yaContextSearchId);
- let yaContextServicesMenu = document.getElementById(this.yaContextSearchServicesId);
-
- let selectedText = "";
-
- let target = gContextMenu.target;
-
- if (gContextMenu.isContentSelected) {
- selectedText = this.getBrowserSelection();
- } else if (gContextMenu.onTextInput && !target.readOnly && target.type != "password") {
- if (target.value) {
- selectedText = target.value.toString().slice(target.selectionStart, target.selectionEnd);
- selectedText = this._cropSelectionText(selectedText);
- }
- }
-
- this.lastWord = selectedText;
-
- if (selectedText) {
- if (selectedText.length > 15)
- selectedText = selectedText.substr(0, 15) + "...";
-
- yaContextMenu.setAttribute("label", this.stringBundleSet.getFormattedString("search_yandex", [selectedText]));
- yaContextServicesMenu.setAttribute("label", this.stringBundleSet.getFormattedString("search_services", [selectedText]));
- }
-
- this.showQuickTranslateItems(selectedText || false);
-
- let hidden = !selectedText;
- yaContextMenu.hidden = hidden;
- yaContextServicesMenu.hidden = hidden;
-
- let sendToItemsType = "page";
- if (!!this.pageDataSender.getImageDataForContextMenu()) {
- sendToItemsType = "image";
- } else if (gContextMenu.isTextSelected) {
- sendToItemsType = "selected";
- }
-
- let hiddenSendService = /^(chrome|file|about):$/.test(target.ownerDocument.location.protocol);
-
- ["Mail", "Yaru"].forEach(function(quickSendService) {
- let item = document.getElementById("yasearchQSendTo" + quickSendService);
- if (item) {
- item.setAttribute("yaPageDataSendType", sendToItemsType);
- item.setAttribute("label", item.getAttribute("yaLabelSend_" + sendToItemsType));
- item.setAttribute("hidden", hiddenSendService || !document.getElementById("yasearch-" + quickSendService.toLowerCase()));
- }
- });
- },
-
- hideContextMenu: function(aEvent) {
- if (aEvent.target.id != this.sContextMenu.id)
- return;
-
- this.lastWord = null;
- },
-
- contentAreaClick: function(event, fieldNormalClicks) {
- if (event.isTrusted && event.button == 0 &&
- !event.ctrlKey && !event.shiftKey && !event.altKey && !event.metaKey) {
-
- var linkNode,
- target = event.target;
-
- if (target instanceof HTMLAnchorElement ||
- target instanceof HTMLAreaElement ||
- target instanceof HTMLLinkElement) {
- if (target.hasAttribute("href"))
- linkNode = target;
- } else {
- linkNode = event.originalTarget;
- while (linkNode && !(linkNode instanceof HTMLAnchorElement))
- linkNode = linkNode.parentNode;
- if (linkNode && !linkNode.hasAttribute("href"))
- linkNode = null;
- }
-
- if (linkNode && linkNode.href) {
- var hrefStr = linkNode.href;
- if (hrefStr.indexOf("mailto:") > -1 && hrefStr.indexOf("mailto:") < 7 &&
- this.composeEmail(hrefStr))
- return false;
- }
- }
-
- return window.contentAreaClick(event, fieldNormalClicks);
- },
-
- pageDataSender: {
- _storedDataToSend: null,
-
- sendToService: function(aDestination, aEvent, aStatData) {
- const LINE_BREAK = aDestination === "yaru" ? "<br/><br/>" : "\n\n";
-
- this._storedDataToSend = null;
-
- var sendType = "page";
-
- if (aEvent && aEvent.target && aEvent.target.hasAttribute("yaPageDataSendType"))
- sendType = aEvent.target.getAttribute("yaPageDataSendType");
-
- var dataToSend = { body: "", subject: "", title: "", URL: "" };
-
- switch (sendType) {
- case "page":
- case "page-link":
- dataToSend.subject = window.content.document.title;
-
- if (sendType == "page") {
- let metaNodes = window.content.document.getElementsByTagName("meta");
- for (let i = 0, len = metaNodes.length; i < len; i++) {
- let metaName = (metaNodes[i].name || "").toLowerCase();
- if (metaName == "description" && metaNodes[i].content) {
- dataToSend.body = metaNodes[i].content + LINE_BREAK;
- break;
- }
- }
- }
-
- dataToSend.body += window.content.location.href;
-
- break;
-
- case "selected":
- dataToSend.body = this.getSelectedText(aEvent);
- break;
-
- case "image":
- let imgData = this.getImageDataForContextMenu();
- if (imgData) {
- dataToSend.body = '<img src="' + imgData.imageURL +
- '" alt="' + (imgData.imageName || imgData.imageURL) +
- '" title="' + imgData.imageName + '" />';
- dataToSend.message_type = "html";
- }
-
- break;
-
- default:
- break;
- }
-
- if (!(dataToSend.body || dataToSend.subject))
- return false;
-
- if (!(sendType == "page" || sendType == "page-link")) {
- let wp = gBrowser.currentURI;
- if (wp && wp.spec && typeof wp.spec != "undefined" && !/^about/.test(wp.spec))
- dataToSend.body = "Источник: " + wp.spec + LINE_BREAK + dataToSend.body;
-
- } else if (/^about/.test(dataToSend.body)) {
- dataToSend.body = null;
- }
-
- if (aDestination == "yaru") {
- if (sendType == "page") {
- dataToSend.URL = window.content.location.href;
- delete dataToSend.body;
- }
-
- dataToSend.title = dataToSend.subject;
- delete dataToSend.subject;
- }
-
- var sendObject = {
- sendType: sendType,
- destination: aDestination,
- dataToSend: dataToSend,
- statData: aStatData,
- sessionRestarted: false
- };
-
- if (Ya.nsIYa.username) {
- this._sendToServiceHepler(sendObject);
- } else {
- sendObject.sessionRestarted = true;
- this._storedDataToSend = sendObject;
-
- var welcomeWindow = Ya.openWelcomeWindow(aDestination);
- if (welcomeWindow) {
- function addUnloadListener(aTarget) {
- aTarget.addEventListener("unload", function(aUnloadEvent) {
- aUnloadEvent.currentTarget.removeEventListener("unload", arguments.callee, false);
- if (Ya && Ya.pageDataSender)
- Ya.pageDataSender.checkNeedSendData();
- }, false);
- }
-
- if (welcomeWindow.document instanceof Ci.nsIDOMXULDocument) {
- addUnloadListener(welcomeWindow);
- } else {//window not ready
- welcomeWindow.addEventListener("load", function(aLoadEvent) {
- aLoadEvent.currentTarget.removeEventListener("load", arguments.callee, false);
- addUnloadListener(aLoadEvent.currentTarget);
- }, false);
- }
- }
- }
- },
-
- checkNeedSendData: function() {
- if (this._storedDataToSend) {
- this._sendToServiceHepler(this._storedDataToSend);
- this._storedDataToSend = null;
- }
- },
-
- _sendToServiceHepler: function(aSendObject) {
- var login = Ya.nsIYa.username;
-
- if (!login || typeof aSendObject !== "object" || !aSendObject.dataToSend)
- return false;
-
- var url = false;
-
- switch (aSendObject.destination) {
- case "mail":
- url = "http://" + Ya.nsIYa.getLocaleDependedUrl("MailHost") + "/compose?mailto=";
- break;
-
- case "yaru":
- url = "http://" + login + ".ya.ru/posts_add_";
- url += (aSendObject.sendType == "page" ? "link" : "text") + ".xml";
- break;
-
- default:
- break;
- }
-
- if (!url)
- return false;
-
- aSendObject.url = url;
-
- if (aSendObject.destination == "yaru") {
- if (aSendObject.sessionRestarted)
- aSendObject.url = "http://my.ya.ru/request_to_session.xml";
-
- aSendObject.dataToSend.YARU_retpath = url;
-
- Ya.nsIYa.xmlHttpRequest(
- "http://pass.yandex.ru/?retpath=" + encodeURIComponent("http://" + login + ".ya.ru/"),
- { callbackFunc: function() { if (Ya && Ya.pageDataSender) Ya.pageDataSender._sendData(aSendObject); } }
- );
-
- return true;
- }
-
- return this._sendData(aSendObject);
- },
-
- getSelectedText: function(aEvent) {
- var focusedWindow = document.commandDispatcher.focusedWindow;
- if (focusedWindow == window)
- focusedWindow = content;
-
- if (!focusedWindow)
- return false;
-
- var selection = focusedWindow.getSelection();
- if (selection.isCollapsed || !selection.rangeCount)
- return false;
-
- var range = selection.getRangeAt(0);
-
- var container = range && range.commonAncestorContainer ? range.commonAncestorContainer : null;
-
- var imgCollection = (container && container.getElementsByTagNameNS) ?
- Array.slice(container.getElementsByTagNameNS(container.namespaceURI, "img")) :
- [];
-
- const TMP_ALT_ATTR_NAME = "__yasearchTmpAlt";
-
- let i = 0;
-
- if (imgCollection.length) {
- while (imgCollection[i]) {
- let img = imgCollection[i];
- img.setAttribute(TMP_ALT_ATTR_NAME, img.getAttribute("alt"));
- img.setAttribute("alt", "[ изображение: " + img.src + " ]");
- i++;
- }
- }
-
- let selectedText = selection.toString();
-
- if (imgCollection.length) {
- i = 0;
- while (imgCollection[i]) {
- let img = imgCollection[i];
- img.setAttribute("alt", img.getAttribute(TMP_ALT_ATTR_NAME));
- img.removeAttribute(TMP_ALT_ATTR_NAME);
- i++;
- }
- }
-
- return selectedText ? selectedText : false;
- },
-
- getImageDataForContextMenu: function() {
- return this.getImageDataForNode(document.popupNode);
- },
-
- getImageDataForNode: function(aNode) {
- if (aNode && aNode.nodeType == 1 && (aNode instanceof Ci.nsIImageLoadingContent) && aNode.currentURI) {
- var imageURI = aNode.currentURI;
-
- if (/file|https?/.test(imageURI.scheme)) {
- var altText = (aNode.hasAttribute && aNode.hasAttribute("alt")) ? aNode.getAttribute("alt") : "";
- var titleText = (aNode.hasAttribute && aNode.hasAttribute("title")) ? aNode.getAttribute("title") : "";
-
- if (altText == imageURI.spec) altText = "";
- if (titleText == imageURI.spec) titleText = "";
-
- var imageName = (titleText.length > altText.length) ? titleText : altText;
-
- if (imageName.length > 1000)
- imageName = imageName.substr(0, 1000);
-
- return {imageURL: imageURI.spec, imageName: imageName};
- }
-
- return null;
- }
-
- return false;
- },
-
- _sendData: function(aSendObject) {
- if (!aSendObject)
- return false;
-
- var url = aSendObject.url;
-
- var dataToSend = [];
-
- for (var [paramName, paramValue] in Iterator(aSendObject.dataToSend)) {
- if (paramName && paramValue)
- dataToSend.push(paramName + "=" + encodeURIComponent(paramValue.substr(0, 1000000)));
- }
-
- var postData = null;
-
- if (dataToSend.length) {
- dataToSend = dataToSend.join("&");
-
- if (dataToSend.length > 8192 && /^https?:\/\/mail\.yandex\./.test(url)) {
- url = url.replace("/compose?", "/classic/compose?");
- }
-
- var stringStream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(Ci.nsIStringInputStream);
- stringStream.setData(dataToSend, dataToSend.length);
-
- postData = Cc["@mozilla.org/network/mime-input-stream;1"].createInstance(Ci.nsIMIMEInputStream);
- postData.addHeader("Content-Type", "application/x-www-form-urlencoded");
- postData.addContentLength = true;
- postData.setData(stringStream);
- }
-
- Ya.loadURI(url, "tab", aSendObject.statData, postData);
- }
- },
-
- _mailIntegration: {
- enabled: false,
-
- get mailUrl() {
- return "http://" + Ya.nsIYa.getLocaleDependedUrl("MailHost") + "/compose?mailto=";
- },
-
- defaultOnClick: function(aEvent) {
- return window.contentAreaClick(aEvent, false);
- },
-
- yaOnClick: function(aEvent) {
- return Ya.contentAreaClick(aEvent, false);
- },
-
- defaultWinObject: window.MailIntegration,
-
- _sendText: function(aBody, aSubject) {
- var dataToSend = [];
-
- if (aBody)
- dataToSend.push("body=" + encodeURIComponent(aBody.substr(0, 1000000)));
-
- if (aSubject)
- dataToSend.push("subject=" + encodeURIComponent(aSubject.substr(0, 100000)));
-
- var postData = null;
- if (dataToSend.length) {
- dataToSend = dataToSend.join("&");
-
- var stringStream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(Ci.nsIStringInputStream);
- stringStream.setData(dataToSend, dataToSend.length);
-
- postData = Cc["@mozilla.org/network/mime-input-stream;1"].createInstance(Ci.nsIMIMEInputStream);
- postData.addHeader("Content-Type", "application/x-www-form-urlencoded");
- postData.addContentLength = true;
- postData.setData(stringStream);
- }
-
- Ya.loadURI(this.mailUrl, "tab", null, postData);
- },
-
- yaWinObject: {
- sendLinkForWindow: function(aWindow) {
- this.sendMessage(aWindow.location.href,
- aWindow.document.title);
- },
-
- sendMessage: function(aBody, aSubject) {
- Ya._mailIntegration._sendText(aBody, aSubject);
- },
-
- _launchExternalUrl: function(aURL) {
- Ya._mailIntegration.defaultWinObject._launchExternalUrl(aURL);
- }
- }
- },
-
- composeEmail: function(aMailAddress) {
- if (this.nsIYa.getBoolPref("yasearch.general.ui.mail.integration.helper.show")) {
- this.nsIYa.setBoolPref("yasearch.general.ui.mail.integration.helper.show", false);
-
- setTimeout(function(me) {
- me.nsIYa.promptService.alert(window,
- me.stringBundleSet.getString("MailIntegrationHelperTitle"),
- me.stringBundleSet.getString("MailIntegrationHelperMessage")
- )
- }, 100, this);
- }
-
- let mailUrl = this._mailIntegration.mailUrl + encodeURIComponent(aMailAddress.replace(/^mailto\:/, ""));
- this.loadURI(mailUrl, "tab", {action:1021});
-
- return true;
- },
-
- get mailIntegration() {
- return this._mailIntegration.enabled;
- },
-
- set mailIntegration(val) {
- let type = val ? "ya" : "default";
- this._mailIntegration.enabled = !!val;
- document.getElementById("content").onclick = this._mailIntegration[type + "OnClick"];
- window.MailIntegration = this._mailIntegration[type + "WinObject"];
- },
-
- search: function(engineId, evt) {
- if (!this.lastWord || this.lastWord == "")
- return false;
-
- var searchUrl = this.nsIYa.getSearchEngineUrl(engineId, this.lastWord, null, {clid:10});
- if (!searchUrl)
- return false;
-
- if (this.nsIYa.prefs.searchHistoryEnabled) {
- Cc["@mozilla.org/satchel/form-history;1"]
- .getService(Ci.nsIFormHistory2)
- .addEntry("yasearch-history", this.lastWord);
- }
-
- this.loadURI(searchUrl.url, "tab", searchUrl.statData, null, true);
-
- return true;
- },
-
- loadService: function(aId, aURL, aEvent, aStatData) {
- this.loadURI(aURL, aEvent, aStatData);
- this.nsIYa.setServiceTimestamp(aId);
- },
-
- loadConditionalURI: function(aType, aEvent, aStatData) {
- var url;
-
- switch (aType) {
- case "feeds"://timed.alert.js
- case "lenta":
- url = "lenta.yandex.ru/" + (this.nsIYa.getBoolPref("yasearch.feeds.uri.open.new") ? "un" : "") + "read.xml";
- break;
-
- case "mail":
- url = this.nsIYa.getLocaleDependedUrl("MailHost") + "/messages" +
- (this.nsIYa.getBoolPref("yasearch.mail.uri.open.new") ? "?extra_cond=only_new" : "");
- break;
-
- case "fotki":
- var login = this.nsIYa.username;
- url = "fotki.yandex.ru" + (login ? ("/users/" + encodeURIComponent(login) + "/favorites") : "");
- break;
-
- case "yaru":
- var login = this.nsIYa.username;
- url = login ? ("http://" + encodeURIComponent(login) + ".ya.ru/replies_history_unread.xml") : "wow.ya.ru";
- break;
-
- default:
- return false;
- }
-
- return this.loadURI(url, aEvent, aStatData);
- },
-
- checkForClick: function(aEvent) {
- var node = aEvent.target;
- if (node.getAttribute("disabled") == "true" || !node.hasAttribute("oncommand"))
- return;
-
- var fn = new Function("event", node.getAttribute("oncommand"));
- fn.call(node, aEvent);
-
- this.closeMenus(node);
- },
-
- closeMenus: function(aNode) {
- if (aNode && "tagName" in aNode) {
- if (aNode.namespaceURI == "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
- && (aNode.tagName == "menupopup" || aNode.tagName == "popup"))
- if (aNode.hidePopup)
- aNode.hidePopup();
- this.closeMenus(aNode.parentNode);
- }
- },
-
- getTypeByEvent: function(evt) {
- if (evt) {
- if (evt == "tab" || evt == "window" || evt == "new-tab") return evt;
- if (evt.shiftKey) return "window";
- if (evt.ctrlKey || evt.metaKey || this.nsIYa.prefs.commandOpenTab) return "tab";
- if (evt.type == "click" && evt.button == 1) return "tab";
- }
- return "none";
- },
-
- canLoadUrlInCurrentTab: function(aTab, aUrl) {
- if (aTab.hasAttribute("busy"))
- return false;
-
- var linkedBrowser = aTab && aTab.linkedBrowser ? aTab.linkedBrowser : null;
-
- try {
- if (!linkedBrowser || linkedBrowser.currentURI.host == "yasearch")
- return true;
- } catch(e) {}
-
- try {
- var sh = linkedBrowser.sessionHistory;
- if (!sh || sh.index < 0 || (sh.count < 2 && (!linkedBrowser.currentURI || linkedBrowser.currentURI.spec == "about:blank")))
- return true;
- } catch(e) {}
-
- function getHostFromURL(url) {
- const IO_SERVICE = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
-
- try {
- return IO_SERVICE.newURI(url, null, null).host;
- } catch(e) {}
-
- return null;
- }
-
- var currentHost = getHostFromURL(linkedBrowser.currentURI.spec);
- var host2Load = getHostFromURL(aUrl);
-
- if (!(currentHost && (currentHost === host2Load)))
- return false;
-
- function modifTextareasInWindow(aWin) {
- var doc = aWin.document;
- if (doc && ("body" in doc)) {
- var i=0, textareas = doc.getElementsByTagName("textarea");
- while (textareas[i]) {
- var textarea = textareas[i++];
- if (textarea instanceof Ci.nsIDOMHTMLTextAreaElement && textarea.defaultValue != textarea.value)
- return true;
- }
- }
- return false;
- }
-
- return modifTextareasInWindow(linkedBrowser.contentWindow) ? false : true;
- },
-
- loadURI: function(url, evt, aStatData, postData, aRespectLoadTabPref) {
- this.nsIYa.checkNeedSendGuid();
-
- if (/^mailto\:/.test(url))
- return this.mailIntegration ? this.composeEmail(url) : false;
-
- var type = this.getTypeByEvent(evt);
-
- if (!/^[A-z]+\:\/\//.test(url))
- url = "http://" + url;
-
- if (aStatData)
- url = this.nsIYa.appendStatData2Url(url, aStatData);
-
- var currentTab;
-
- switch (type) {
- case "window":
- openNewWindowWith(url);
- break;
-
- case "new-tab":
- case "tab":
- currentTab = gBrowser.mCurrentTab;
-
- if (!(type == "new-tab" || evt.ctrlKey || evt.metaKey || (evt.type == "click" && evt.button == 1)) &&
- this.canLoadUrlInCurrentTab(currentTab, url))
- {
- currentTab.linkedBrowser.loadURIWithFlags(url, Ci.nsIWebNavigation.LOAD_FLAGS_NONE, null, null, postData);
- } else {
- currentTab = gBrowser.loadOneTab(url, null, null, postData, (aRespectLoadTabPref ? null : false));
- }
-
- break;
-
- default:
- currentTab = gBrowser.mCurrentTab;
- loadURI(url, null, postData);
- }
-
- if (aStatData && aStatData.action && currentTab && currentTab.linkedBrowser.yaSearchTHandler)
- currentTab.linkedBrowser.yaSearchTHandler.actionValue = aStatData.action;
-
- return true;
- },
-
- fixupWebURI: function(aString) {
- const nsIURIFixup = Ci.nsIURIFixup;
- let fixup = Cc["@mozilla.org/docshell/urifixup;1"].getService(nsIURIFixup);
- let uri = null;
- try {
- uri = fixup.createFixupURI(aString, nsIURIFixup.FIXUP_FLAG_NONE);
- uri = uri && uri.spec && /^(ftp|https?)/.test(uri.scheme) ? uri : null;
- } catch(e) {}
-
- return uri;
- },
-
- loadURIsInTabs: function(aURIsArray) {
- for each (let uriString in aURIsArray) {
- let uri = this.fixupWebURI(uriString);
- if (uri)
- gBrowser.addTab(uri.spec);
- }
- },
-
- refreshMailData: function() {
- var mb = document.getElementById("yasearch-mail-menupopup");
- if (!mb)
- return;
-
- while (mb.lastChild.nodeName != "menuseparator")
- mb.removeChild(mb.lastChild);
-
- var data = this.nsIYa.mailData;
-
- mb.lastChild.style.display = (data.count > 0 && data.nodes) ? "" : "none";
-
- if (data.nodes)
- while (data.nodes.hasChildNodes())
- this.nsIYa.DOMUtils.appendNode(data.nodes.firstChild, mb);
-
- mb.parentNode.setAttribute("yaLabel", data.count > 0 ? data.count : 0);
- },
-
- clickMail: function(aEvent) {
- let mailId = aEvent.target.id;
- this.nsIYa.decMailCounter(mailId);
- this.loadURI(mailId, aEvent, {action:1030});
- return false;
- },
-
- refreshCounterData: function(aType) {
- let menupopup = document.getElementById("yasearch-" + aType + "-menupopup");
-
- if (menupopup) {
- let tbb = this.nsIYa.Counters.getXULNode(aType);
- menupopup.parentNode.setAttribute("yaLabel", tbb.getAttribute("yaLabel"));
-
- while (menupopup.firstChild)
- menupopup.removeChild(menupopup.firstChild);
-
- let data = tbb.firstChild;
- while (data.hasChildNodes())
- this.nsIYa.DOMUtils.appendNode(data.firstChild, menupopup);
- }
- },
-
- refreshLentaData: function() {
- let fb = document.getElementById("yasearch-lenta-button");
- if (fb) {
- let data = this.nsIYa.Counters.getCount("lenta");
- fb.setAttribute("yaLabel", data > 0 ? data : 0);
- }
- },
-
- refreshMoneyData: function() {
- let mb = document.getElementById("yasearch-money-button");
- if (mb) {
- let data = this.nsIYa.Counters.getCount("money");
- mb.setAttribute("yaLabel", data > 0 ? data : 0);
- }
- },
-
- observe: function(aSubject, aTopic, aState) {
- switch (aTopic) {
- case "Ya-Refresh-Services":
- this.redrawServicesButton();
- break;
-
- case "Ya-Refresh-Login-Status":
- this.realizeLoginBehaviour();
- break;
-
- case "Ya-Refresh-Data":
- switch (aState) {
- //~ case "mail":
- case "mailList":
- this.refreshMailData();
- break;
-
- case "allServices":
- case "mailAndFeeds":
- this.refreshLentaData();
- this.refreshMoneyData();
- this.refreshCounterData("fotki");
- this.refreshCounterData("yaru");
- this.refreshCounterData("moikrug");
- break;
-
- case "fotki":
- case "yaru":
- case "moikrug":
- this.refreshCounterData(aState);
- break;
-
- case "lenta":
- this.refreshLentaData();
- break;
-
- case "money":
- this.refreshMoneyData();
- break;
-
- case "mail-integration":
- this.mailIntegration = this.nsIYa.getBoolPref("yasearch.general.ui.mail.integration");
- break;
-
- case "services":
- this.redrawServices();
- break;
-
- default:
- break;
- }
-
- break;
-
- case "Ya-Refresh-Busy-State":
- let data = aState.split(":");
- let state = !!(data[0] === "true");
- let types = [data[1]];
-
- for each (var type in types) {
- window.setTimeout(function() {
- let btn = YaUtils.evaluateXPath(document, '//xul:toolbaritem[starts-with(@id,"' + type + '")]')[0];
- if (btn)
- btn.setAttribute("busy", state);
- }, state ? 0 : 700);
- }
-
- break;
- }
- },
-
- openWelcomeWindow: function(aServiceType, aCallbackElementId) {
- const winType = "Yasearch:WelcomeDialog";
-
- let welcomeWindow = this.nsIYa.getWindow(winType);
- if (welcomeWindow) {
- welcomeWindow.focus();
- } else {
- let flags = "chrome,alwaysRaised,dependent=yes";
-
- if (!this.nsIYa.AppInfo.browser.isGreaterThenFx35 || this.nsIYa.AppInfo.OS.isMacOS)
- flags += ",titlebar=no";
-
- welcomeWindow = window.openDialog(YaUtils.CHROME_CONTENT + "dialogs/welcome.xul",
- winType, flags, aServiceType, aCallbackElementId);
- }
-
- return welcomeWindow;
- },
-
- openPrefWindow: function() {
- const winType = "Yasearch:PreferencesWindow";
-
- let prefWindow = this.nsIYa.getWindow(winType);
- if (prefWindow) {
- prefWindow.focus();
- } else {
- var instantApply = this.nsIYa.getBoolPref("browser.preferences.instantApply");
- var features = "chrome,titlebar,toolbar,centerscreen," + (instantApply ? "dialog=no" : "modal");
- prefWindow = window.openDialog(YaUtils.CHROME_CONTENT + "preferences/preferences.xul", winType, features);
- }
-
- return prefWindow;
- },
-
- openAboutWindow: function() {
- const winType = "Yasearch:AboutDialog";
-
- let aboutWindow = this.nsIYa.getWindow(winType);
- if (aboutWindow)
- aboutWindow.focus();
- else
- aboutWindow = window.openDialog(YaUtils.CHROME_CONTENT + "about/about.xul", winType,
- "chrome,resizable=no,dependent=yes,centerscreen");
-
- return aboutWindow;
- },
-
- log: function(msg) { this.nsIYa.log("Ya: " + msg); },
- dump: function(aObj) { this.nsIYa.dump(aObj); }
- };
-
- window.addEventListener("load", function(aLoadEvent) {
- aLoadEvent.currentTarget.removeEventListener("load", arguments.callee, false);
-
- aLoadEvent.currentTarget.addEventListener("unload", function(aUnloadEvent) {
- aUnloadEvent.currentTarget.removeEventListener("unload", arguments.callee, false);
- Ya.unLoad();
- }, false);
-
- Ya.onLoad();
- }, false);
-