1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 const Cc = Components.classes;
6 const Ci = Components.interfaces;
7 const Cu = Components.utils;
8 const Cr = Components.results;
10 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
11 Cu.import("resource://gre/modules/Services.jsm");
13 function MetroUIUtils() {
18 "applet": ["archive", "code", "codebase"],
22 "blockquote": ["cite"],
24 "button": "formaction",
29 "frame": ["longdesc", "src"],
30 "iframe": ["longdesc", "src"],
31 "img": ["longdesc", "src"],
32 "input": ["formaction", "src"],
35 "object": ["archive", "codebase", "data"],
41 MetroUIUtils.prototype = {
42 classID : Components.ID("e4626085-17f7-4068-a225-66c1acc0485c"),
43 QueryInterface : XPCOMUtils.generateQI([Ci.nsIMetroUIUtils]),
45 * Loads the specified panel in the browser.
46 * @ param aPanelId The identifier of the pane to load
48 showPanel: function(aPanelId) {
49 let browserWin = Services.wm.getMostRecentWindow("navigator:browser");
50 browserWin.PanelUI.show(aPanelId);
54 * Determines if the browser has selected content
56 get hasSelectedContent() {
58 let browserWin = Services.wm.getMostRecentWindow("navigator:browser");
59 let tabBrowser = browserWin.getBrowser();
60 if (!browserWin || !tabBrowser || !tabBrowser.contentWindow) {
64 let sel = tabBrowser.contentWindow.getSelection();
65 return sel && sel.toString();
72 * Obtains the current page title
74 get currentPageTitle() {
75 let browserWin = Services.wm.getMostRecentWindow("navigator:browser");
76 if (!browserWin || !browserWin.content || !browserWin.content.document) {
77 throw Cr.NS_ERROR_FAILURE;
79 return browserWin.content.document.title || "";
83 * Obtains the current page URI
85 get currentPageURI() {
86 let browserWin = Services.wm.getMostRecentWindow("navigator:browser");
87 if (!browserWin || !browserWin.content || !browserWin.content.document) {
88 throw Cr.NS_ERROR_FAILURE;
90 return browserWin.content.document.URL || "";
94 * Determines the text that should be shared
97 let browserWin = Services.wm.getMostRecentWindow("navigator:browser");
98 let tabBrowser = browserWin.getBrowser();
99 if (browserWin && tabBrowser && tabBrowser.contentWindow) {
100 let sel = tabBrowser.contentWindow.getSelection();
101 if (sel && sel.rangeCount)
105 throw Cr.NS_ERROR_FAILURE;
109 * Replaces the node's attribute value to be a fully qualified URL
111 _expandAttribute : function(ioService, doc, node, attrName) {
112 let attrValue = node.getAttribute(attrName);
117 let uri = ioService.newURI(attrValue, null, doc.baseURIObject);
118 node.setAttribute(attrName, uri.spec);
124 * Replaces all attribute values in 'n' which contain URLs recursiely
125 * to fully qualified URLs.
127 _expandURLs: function(doc, n) {
128 let ioService = Cc["@mozilla.org/network/io-service;1"].
129 getService(Ci.nsIIOService);
130 for (let i = 0; i < n.children.length; i++) {
131 let child = n.children[i];
132 let childTagName = child.tagName.toLowerCase();
134 // Iterate through all known tags which can contain URLs. A tag either
135 // contains a single attribute name or an array of attribute names.
136 for (let tagName in URLElements) {
137 if (tagName === childTagName) {
138 if (URLElements[tagName] instanceof Array) {
139 URLElements[tagName].forEach(function(attrName) {
140 this._expandAttribute(ioService ,doc, child, attrName);
143 this._expandAttribute(ioService ,doc, child, URLElements[tagName]);
148 this._expandURLs(doc, child);
153 * Determines the HTML that should be shared
156 let browserWin = Services.wm.getMostRecentWindow("navigator:browser");
157 let tabBrowser = browserWin.getBrowser();
159 if (browserWin && tabBrowser && tabBrowser.contentWindow &&
160 (sel = tabBrowser.contentWindow.getSelection()) && sel.rangeCount) {
161 let div = tabBrowser.contentWindow.document.createElement("DIV");
162 for (let i = 0; i < sel.rangeCount; i++) {
163 let contents = sel.getRangeAt(i).cloneContents(true);
164 div.appendChild(contents);
166 this._expandURLs(tabBrowser.contentWindow.document, div);
167 return div.outerHTML;
170 throw Cr.NS_ERROR_FAILURE;
174 var component = [MetroUIUtils];
175 this.NSGetFactory = XPCOMUtils.generateNSGetFactory(component);