pdf.js/web/firefoxcom.js

156 lines
4.9 KiB
JavaScript
Raw Normal View History

2012-09-01 07:48:21 +09:00
/* Copyright 2012 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* globals pdfjsLib */
2012-09-01 07:48:21 +09:00
2013-07-13 03:14:13 +09:00
'use strict';
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define('pdfjs-web/firefoxcom', ['exports', 'pdfjs-web/preferences'],
factory);
} else if (typeof exports !== 'undefined') {
factory(exports, require('./preferences.js'));
} else {
factory((root.pdfjsWebFirefoxCom = {}), root.pdfjsWebPreferences);
}
}(this, function (exports, preferences) {
//#if FIREFOX || MOZCENTRAL
//#if !(FIREFOX || MOZCENTRAL)
if (true) { return; } // TODO ensure nothing depends on this module.
//#endif
var Preferences = preferences.Preferences;
2013-07-13 03:14:13 +09:00
var FirefoxCom = (function FirefoxComClosure() {
2012-08-02 07:31:25 +09:00
return {
/**
* Creates an event that the extension is listening for and will
* synchronously respond to.
* NOTE: It is reccomended to use request() instead since one day we may not
* be able to synchronously reply.
* @param {String} action The action to trigger.
* @param {String} data Optional data to send.
* @return {*} The response.
*/
requestSync: function(action, data) {
var request = document.createTextNode('');
document.documentElement.appendChild(request);
var sender = document.createEvent('CustomEvent');
sender.initCustomEvent('pdf.js.message', true, false,
{action: action, data: data, sync: true});
2012-08-02 07:31:25 +09:00
request.dispatchEvent(sender);
var response = sender.detail.response;
2012-08-02 07:31:25 +09:00
document.documentElement.removeChild(request);
return response;
},
/**
* Creates an event that the extension is listening for and will
* asynchronously respond by calling the callback.
* @param {String} action The action to trigger.
* @param {String} data Optional data to send.
* @param {Function} callback Optional response callback that will be called
* with one data argument.
*/
request: function(action, data, callback) {
var request = document.createTextNode('');
if (callback) {
document.addEventListener('pdf.js.response', function listener(event) {
2014-03-09 07:47:12 +09:00
var node = event.target;
var response = event.detail.response;
2012-08-02 07:31:25 +09:00
document.documentElement.removeChild(node);
document.removeEventListener('pdf.js.response', listener, false);
return callback(response);
}, false);
}
document.documentElement.appendChild(request);
var sender = document.createEvent('CustomEvent');
sender.initCustomEvent('pdf.js.message', true, false, {
action: action,
data: data,
sync: false,
responseExpected: !!callback
});
2012-08-02 07:31:25 +09:00
return request.dispatchEvent(sender);
}
};
})();
2013-07-13 03:14:13 +09:00
var DownloadManager = (function DownloadManagerClosure() {
function DownloadManager() {}
DownloadManager.prototype = {
2013-07-13 07:00:43 +09:00
downloadUrl: function DownloadManager_downloadUrl(url, filename) {
2013-07-13 03:14:13 +09:00
FirefoxCom.request('download', {
originalUrl: url,
2013-07-13 07:00:43 +09:00
filename: filename
2013-07-13 03:14:13 +09:00
});
},
downloadData: function DownloadManager_downloadData(data, filename,
contentType) {
var blobUrl = pdfjsLib.createObjectURL(data, contentType, false);
2015-02-03 00:12:52 +09:00
FirefoxCom.request('download', {
blobUrl: blobUrl,
originalUrl: blobUrl,
filename: filename,
isAttachment: true
});
},
2013-07-13 07:00:43 +09:00
download: function DownloadManager_download(blob, url, filename) {
2013-07-13 03:14:13 +09:00
var blobUrl = window.URL.createObjectURL(blob);
FirefoxCom.request('download', {
blobUrl: blobUrl,
originalUrl: url,
2013-07-13 07:00:43 +09:00
filename: filename
2013-07-13 03:14:13 +09:00
},
function response(err) {
if (err && this.onerror) {
this.onerror(err);
}
window.URL.revokeObjectURL(blobUrl);
}.bind(this)
);
}
};
return DownloadManager;
})();
2013-11-19 07:51:06 +09:00
2014-03-17 06:33:39 +09:00
Preferences._writeToStorage = function (prefObj) {
return new Promise(function (resolve) {
FirefoxCom.request('setPreferences', prefObj, resolve);
2014-03-17 06:33:39 +09:00
});
2013-11-19 07:51:06 +09:00
};
2014-03-17 06:33:39 +09:00
Preferences._readFromStorage = function (prefObj) {
return new Promise(function (resolve) {
FirefoxCom.request('getPreferences', prefObj, function (prefStr) {
var readPrefs = JSON.parse(prefStr);
resolve(readPrefs);
});
});
2013-11-19 07:51:06 +09:00
};
exports.DownloadManager = DownloadManager;
exports.FirefoxCom = FirefoxCom;
//#endif
}));