This repository has been archived by the owner on Feb 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
220 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
const {utils: Cu} = Components; | ||
|
||
Cu.importGlobalProperties(["fetch", "URL"]); | ||
|
||
const TIPPYTOP_JSON_PATH = "resource://activity-stream/data/content/tippytop/top_sites.json"; | ||
const TIPPYTOP_URL_PREFIX = "resource://activity-stream/data/content/tippytop/images/"; | ||
|
||
function getDomain(url) { | ||
let domain = new URL(url).hostname; | ||
if (domain && domain.startsWith("www.")) { | ||
domain = domain.slice(4); | ||
} | ||
return domain; | ||
} | ||
|
||
function getPath(url) { | ||
return new URL(url).pathname; | ||
} | ||
|
||
this.TippyTopProvider = class TippyTopProvider { | ||
constructor() { | ||
this._sitesByDomain = {}; | ||
} | ||
async init() { | ||
// Load the Tippy Top sites from the json manifest. | ||
fetch(TIPPYTOP_JSON_PATH) | ||
.then(response => response.json()) | ||
.then(sites => { | ||
for (let site of sites) { | ||
if ("url" in site) { | ||
this._sitesByDomain[getDomain(site.url)] = site; | ||
} | ||
if ("urls" in site) { | ||
for (let url of site.urls) { | ||
this._sitesByDomain[getDomain(url)] = site; | ||
} | ||
} | ||
} | ||
}) | ||
.catch(error => Cu.reportError("Failed to load tippy top manifest.")); | ||
} | ||
processSite(site) { | ||
// Skip URLs with a path that isn't the root path / | ||
let path; | ||
try { | ||
path = getPath(site.url); | ||
} catch (e) {} | ||
if (path !== "/") { | ||
return site; | ||
} | ||
|
||
let key = getDomain(site.url); | ||
if (key && key in this._sitesByDomain) { | ||
let tippyTop = this._sitesByDomain[key]; | ||
site.tippyTopIcon = TIPPYTOP_URL_PREFIX + tippyTop.image_url; | ||
site.backgroundColor = tippyTop.background_color; | ||
} | ||
return site; | ||
} | ||
}; | ||
|
||
this.EXPORTED_SYMBOLS = ["TippyTopProvider"]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
"use strict"; | ||
const {TippyTopProvider} = require("lib/TippyTopProvider.jsm"); | ||
const {GlobalOverrider} = require("test/unit/utils"); | ||
|
||
describe("TippyTopProvider", () => { | ||
let instance; | ||
let globals; | ||
beforeEach(async () => { | ||
globals = new GlobalOverrider(); | ||
let fetchStub = globals.sandbox.stub(); | ||
globals.set("fetch", fetchStub); | ||
fetchStub.resolves({ | ||
ok: true, | ||
status: 200, | ||
json: () => Promise.resolve([{ | ||
"title": "facebook", | ||
"url": "https://www.facebook.com/", | ||
"image_url": "facebook-com.png", | ||
"background_color": "#3b5998", | ||
"domain": "facebook.com" | ||
}, { | ||
"title": "gmail", | ||
"urls": ["https://www.gmail.com/", "https://mail.google.com"], | ||
"image_url": "gmail-com.png", | ||
"background_color": "#000000", | ||
"domain": "gmail.com" | ||
}]) | ||
}); | ||
instance = new TippyTopProvider(); | ||
await instance.init(); | ||
}); | ||
it("should provide an icon for facebook.com", () => { | ||
const site = instance.processSite({url: "https://facebook.com"}); | ||
assert.equal(site.tippyTopIcon, "resource://activity-stream/data/content/tippytop/images/facebook-com.png"); | ||
assert.equal(site.backgroundColor, "#3b5998"); | ||
}); | ||
it("should provide an icon for www.facebook.com", () => { | ||
const site = instance.processSite({url: "https://www.facebook.com"}); | ||
assert.equal(site.tippyTopIcon, "resource://activity-stream/data/content/tippytop/images/facebook-com.png"); | ||
assert.equal(site.backgroundColor, "#3b5998"); | ||
}); | ||
it("should not provide an icon for facebook.com/foobar", () => { | ||
const site = instance.processSite({url: "https://facebook.com/foobar"}); | ||
assert.isUndefined(site.tippyTopIcon); | ||
assert.isUndefined(site.backgroundColor); | ||
}); | ||
it("should provide an icon for gmail.com", () => { | ||
const site = instance.processSite({url: "https://gmail.com"}); | ||
assert.equal(site.tippyTopIcon, "resource://activity-stream/data/content/tippytop/images/gmail-com.png"); | ||
assert.equal(site.backgroundColor, "#000000"); | ||
}); | ||
it("should provide an icon for mail.google.com", () => { | ||
const site = instance.processSite({url: "https://mail.google.com"}); | ||
assert.equal(site.tippyTopIcon, "resource://activity-stream/data/content/tippytop/images/gmail-com.png"); | ||
assert.equal(site.backgroundColor, "#000000"); | ||
}); | ||
it("should handle garbage URLs gracefully", () => { | ||
const site = instance.processSite({url: "garbagejlfkdsa"}); | ||
assert.isUndefined(site.tippyTopIcon); | ||
assert.isUndefined(site.backgroundColor); | ||
}); | ||
it("should handle error when fetching and parsing manifest", async () => { | ||
globals = new GlobalOverrider(); | ||
let fetchStub = globals.sandbox.stub(); | ||
globals.set("fetch", fetchStub); | ||
fetchStub.rejects("whaaaa"); | ||
instance = new TippyTopProvider(); | ||
await instance.init(); | ||
instance.processSite("https://facebook.com"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters