Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function (nativeMsBrowser) {
- nativeJSON = window.JSON;
- Object.defineProperty(window, "msBrowser", {
- get: delayInitMsBrowser,
- configurable: true,
- enumerable: true
- });
- function delayInitMsBrowser() {
- var wrapMsBrowser = Object.defineProperties({}, {
- "runtime": {
- get: delayInitRuntime,
- configurable: true,
- enumerable: true
- }
- });
- // Replace delayInit with the initialized object to avoid repeated calls to delayInit.
- Object.defineProperty(window, "msBrowser", {
- value: wrapMsBrowser,
- configurable: true,
- enumerable: true
- });
- return wrapMsBrowser;
- }
- function delayInitRuntime() {
- var wrapRuntime = {};
- Object.defineProperties(wrapRuntime, {
- "sendMessage": {
- value: sendMessageIndirect.bind(nativeMsBrowser.runtime),
- configurable: true,
- enumerable: true
- }
- });
- function sendMessageIndirect(/* optional string extensionId, any message, optional object options, optional function responseCallback */) {
- if (arguments.length < 1) {
- return null;
- }
- var index = arguments.length - 1;
- var responseCallback = null;
- if (typeof arguments[index] === "function") {
- responseCallback = responseCallbackIndirect(arguments[index]);
- --index;
- }
- var options = null;
- if (index >= 2) {
- // More than two arguments remain so options param must present.
- options = arguments[index];
- --index;
- }
- else if (index === 2) {
- // The logic: assume that the first param is the optional extension id if it is present and is a string, hence no options argument in this case.
- if (!(arguments[0] === null || typeof arguments[0] === "string")) {
- options = arguments[index];
- --index;
- }
- }
- var message = arguments[index]; // Must be present.
- --index;
- var extensionId = null;
- if (index >= 0) {
- extensionId = arguments[index];
- --index;
- }
- if (index !== -1) {
- // Requires exact number of arguments, no extra arguments allowed.
- return null;
- }
- else {
- // Always stringify on sendMessage side and always parse on the callback side.
- // Otherwise, callback cannot distinuish between a message sending an object and another message sending its string equivalent.
- return nativeMsBrowser.runtime.sendMessage(extensionId, nativeJSON.stringify(message), options, responseCallback);
- }
- };
- // Replace delayInit with the initialized object to avoid repeated calls to delayInit.
- Object.defineProperty(window.msBrowser, "runtime", {
- value: wrapRuntime,
- configurable: true,
- enumerable: true
- });
- return wrapRuntime;
- }
- })(window.msBrowser);
- // !!! End of File: be careful if you have to change the following line.
- // There is a null character at end of this line, enabling this resource to be used as a null terminated string:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement