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.
|
|
|
|
*/
|
|
|
|
|
2012-08-02 07:31:25 +09:00
|
|
|
var FirefoxCom = (function FirefoxComClosure() {
|
2013-02-05 03:01:19 +09:00
|
|
|
'use strict';
|
|
|
|
|
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);
|
|
|
|
|
2013-02-06 07:29:36 +09:00
|
|
|
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);
|
2013-02-06 07:29:36 +09:00
|
|
|
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) {
|
|
|
|
var node = event.target,
|
2013-02-06 07:29:36 +09:00
|
|
|
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);
|
|
|
|
|
2013-02-06 07:29:36 +09:00
|
|
|
var sender = document.createEvent('CustomEvent');
|
|
|
|
sender.initCustomEvent('pdf.js.message', true, false,
|
|
|
|
{action: action, data: data, sync: false,
|
|
|
|
callback: callback});
|
2012-08-02 07:31:25 +09:00
|
|
|
return request.dispatchEvent(sender);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})();
|