From 1f1b7e06ec8761cbbb5d289435b1fd485c0e27e3 Mon Sep 17 00:00:00 2001 From: Kris Maglione Date: Sun, 17 Mar 2013 14:25:21 -0700 Subject: [PATCH] Add short URL to g. Also fix bugs. --- common/modules/buffer.jsm | 67 +++++++++++++++++++++++++++++++++--- common/modules/sanitizer.jsm | 7 ++++ 2 files changed, 69 insertions(+), 5 deletions(-) diff --git a/common/modules/buffer.jsm b/common/modules/buffer.jsm index 7bc53ebf..10e997cd 100644 --- a/common/modules/buffer.jsm +++ b/common/modules/buffer.jsm @@ -12,6 +12,7 @@ defineModule("buffer", { }); lazyRequire("bookmarkcache", ["bookmarkcache"]); +lazyRequire("contexts", ["Group"]); lazyRequire("io", ["io"]); lazyRequire("finder", ["RangeFind"]); lazyRequire("overlay", ["overlay"]); @@ -575,6 +576,30 @@ var Buffer = Module("Buffer", { */ get selectionController() util.selectionController(this.focusedFrame), + /** + * @property {string|null} The canonical short URL for the current + * document. + */ + get shortURL() { + let { uri, doc } = this; + + for each (let shortener in Buffer.uriShorteners) + try { + let shortened = shortener(uri, doc); + if (shortened) + return shortened.spec; + } + catch (e) { + util.reportError(e); + } + + let link = DOM("link[href]:-moz-any([rev=canonical], [rel=shortlink])", doc); + if (link) + return link.attr("href"); + + return null; + }, + /** * Opens the appropriate context menu for *elem*. * @@ -1176,12 +1201,20 @@ var Buffer = Module("Buffer", { let self = this; let uri = this.uri; - if (services.has("contentPrefs") && prefs.get("browser.zoom.siteSpecific")) - services.contentPrefs.getPref(uri, "dactyl.content.full-zoom", function (val) { + if (services.has("contentPrefs") && prefs.get("browser.zoom.siteSpecific")) { + let callback = function (val) { if (val != null && uri.equals(self.uri) && val != prefs.get("browser.zoom.full")) [self.contentViewer.textZoom, self.contentViewer.fullZoom] = [self.contentViewer.fullZoom, self.contentViewer.textZoom]; - }, sanitizer.getContext(this.win)); + } + // God damn it. + if (util.haveGecko("19.0a1")) + services.contentPrefs.getPref(uri, "dactyl.content.full-zoom", + sanitizer.getContext(this.win), callback); + else + services.contentPrefs.getPref(uri, "dactyl.content.full-zoom", + callback); + } }), /** @@ -1240,6 +1273,27 @@ var Buffer = Module("Buffer", { this.pageInfo[option] = Buffer.PageInfo(option, title, func); }, + uriShorteners: [], + + /** + * Adds a new URI shortener for documents matching the given filter. + * + * @param {string|function(URI, Document):boolean} filter A site filter + * string or a function which accepts a URI and a document and + * returns true if it can shorten the document's URI. + * @param {function(URI, Document):URI} shortener Returns a shortened + * URL for the given URI and document. + */ + addURIShortener: function addURIShortener(filter, shortener) { + if (isString(filter)) + filter = Group.compileFilter(filter); + + this.uriShorteners.push(function uriShortener(uri, doc) { + if (filter(uri, doc)) + return shortener(uri, doc); + }); + }, + Scrollable: function Scrollable(elem) { if (elem instanceof Ci.nsIDOMElement) return elem; @@ -1885,8 +1939,7 @@ var Buffer = Module("Buffer", { uri.query = uri.query.replace(/(?:^|&)utm_[^&]+/g, "") .replace(/^&/, ""); - let link = DOM("link[href][rev=canonical], link[href][rel=shortlink]", doc); - let url = link.length && options.get("yankshort").getKey(uri) ? link.attr("href") : uri.spec; + let url = options.get("yankshort").getKey(uri) && buffer.shortURL || uri.spec; dactyl.clipboardWrite(url, true); }); @@ -2509,6 +2562,10 @@ Buffer.addPageInfoSection("g", "General Info", function (verbose) { yield ["Title", doc.title]; yield ["URL", template.highlightURL(doc.location.href, true)]; + let { shortURL } = this; + if (shortURL) + yield ["Short URL", template.highlightURL(shortURL, true)]; + let ref = "referrer" in doc && doc.referrer; if (ref) yield ["Referrer", template.highlightURL(ref, true)]; diff --git a/common/modules/sanitizer.jsm b/common/modules/sanitizer.jsm index 7f10dba7..35220dda 100644 --- a/common/modules/sanitizer.jsm +++ b/common/modules/sanitizer.jsm @@ -290,6 +290,13 @@ var Sanitizer = Module("sanitizer", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakRef } }, + /** + * Returns a load context for the given thing, to be used with + * interfaces needing one for per-window private browsing support. + * + * @param {Window|Document|Node} thing The thing for which to return + * a load context. + */ getContext: function getContext(thing) { if (!Ci.nsILoadContext) return null;