mirror of
https://repo.dactyloidae.xyz/Dactyloidae/UXP.git
synced 2026-09-22 08:27:31 +09:00
Issue #303 Part 1: Move basilisk files from /browser to /application/basilisk
This commit is contained in:
parent
a951f19dfd
commit
177b28a898
1938 changed files with 0 additions and 0 deletions
49
application/basilisk/components/newtab/PreviewProvider.jsm
Normal file
49
application/basilisk/components/newtab/PreviewProvider.jsm
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
/* global XPCOMUtils, BackgroundPageThumbs, FileUtils, PageThumbsStorage, Task, MIMEService */
|
||||
/* exported PreviewProvider */
|
||||
|
||||
"use strict";
|
||||
|
||||
this.EXPORTED_SYMBOLS = ["PreviewProvider"];
|
||||
|
||||
const {utils: Cu} = Components;
|
||||
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
||||
Cu.import("resource://gre/modules/Task.jsm");
|
||||
Cu.import("resource://gre/modules/PageThumbs.jsm");
|
||||
Cu.import("resource://gre/modules/FileUtils.jsm");
|
||||
const {OS} = Cu.import("resource://gre/modules/osfile.jsm");
|
||||
|
||||
XPCOMUtils.defineLazyModuleGetter(this, "BackgroundPageThumbs",
|
||||
"resource://gre/modules/BackgroundPageThumbs.jsm");
|
||||
XPCOMUtils.defineLazyServiceGetter(this, "MIMEService",
|
||||
"@mozilla.org/mime;1", "nsIMIMEService");
|
||||
|
||||
let PreviewProvider = {
|
||||
/**
|
||||
* Returns a thumbnail as a data URI for a url, creating it if necessary
|
||||
*
|
||||
* @param {String} url
|
||||
* a url to obtain a thumbnail for
|
||||
* @return {Promise} A Promise that resolves with a base64 encoded thumbnail
|
||||
*/
|
||||
getThumbnail: Task.async(function* PreviewProvider_getThumbnail(url) {
|
||||
try {
|
||||
yield BackgroundPageThumbs.captureIfMissing(url);
|
||||
let imgPath = PageThumbsStorage.getFilePathForURL(url);
|
||||
|
||||
// OS.File object used to easily read off-thread
|
||||
let file = yield OS.File.open(imgPath, {read: true, existing: true});
|
||||
|
||||
// nsIFile object needed for MIMEService
|
||||
let nsFile = FileUtils.File(imgPath);
|
||||
|
||||
let contentType = MIMEService.getTypeFromFile(nsFile);
|
||||
let bytes = yield file.read();
|
||||
let encodedData = btoa(String.fromCharCode.apply(null, bytes));
|
||||
file.close();
|
||||
return `data:${contentType};base64,${encodedData}`;
|
||||
} catch (err) {
|
||||
Cu.reportError(`PreviewProvider_getThumbnail error: ${err}`);
|
||||
throw err;
|
||||
}
|
||||
})
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue