Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function (nativeMsBrowser) {
- var 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
- },
- "storage": {
- get: delayInitStorage,
- configurable: true,
- enumerable: true
- },
- "tabs": {
- get: delayInitTabs,
- configurable: true,
- enumerable: true
- },
- "browserAction": {
- get: delayInitBrowserAction,
- 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({}, {
- "getURL": {
- value: nativeMsBrowser.runtime.getURL.bind(nativeMsBrowser.runtime),
- configurable: true,
- enumerable: true
- },
- "sendMessage": {
- value: sendMessageIndirect.bind(nativeMsBrowser.runtime),
- configurable: true,
- enumerable: true
- },
- "onMessage": {
- get: delayInitOnMessage,
- configurable: true,
- enumerable: true
- },
- "onMessageExternal": {
- get: delayInitOnMessageExternal,
- configurable: true,
- enumerable: true
- }
- });
- function onMessageAddListenerIndirect(callback) {
- this.addListener(onMessageCallbackIndirect(callback));
- }
- function onMessageCallbackIndirect(callback) {
- return function () {
- var result = {};
- function sendResponse(response) {
- result.response = nativeJSON.stringify(response);
- }
- var msg = nativeJSON.parse(arguments[0].msg);
- result.async = callback(msg, arguments[0].sender, sendResponse.bind(window)) ? true : false;
- return result;
- // Need to handle sending the response async - http://osgvsowi/674353
- }
- }
- function responseCallbackIndirect(responseCallback) {
- return function () {
- var jsonResponse = arguments[0];
- var response; // This is initialized to undefined.
- if (jsonResponse !== 'undefined') {
- // JSON.stringify(undefined) === undefined is packed for courier messaging as 'undefined' but JSON.parse('undefined') throws javascript error.
- // A response of "undefined" (type string) is packed for courier messaging as '"undefined"', hence will not conflict with a response of undefined (type undefined).
- response = nativeJSON.parse(jsonResponse);
- }
- responseCallback(response);
- }
- }
- function delayInitOnMessage() {
- var wrapOnMessage = Object.defineProperties({}, {
- "addListener": {
- value: onMessageAddListenerIndirect.bind(nativeMsBrowser.runtime.onMessage),
- configurable: true,
- enumerable: true
- }
- });
- // Replace delayInit with the initialized object to avoid repeated calls to delayInit.
- Object.defineProperty(window.msBrowser.runtime, "onMessage", {
- value: wrapOnMessage,
- configurable: true,
- enumerable: true
- });
- return wrapOnMessage;
- }
- function delayInitOnMessageExternal() {
- var wrapOnMessageExternal = Object.defineProperties({}, {
- "addListener": {
- value: onMessageAddListenerIndirect.bind(nativeMsBrowser.runtime.onMessageExternal),
- configurable: true,
- enumerable: true
- }
- });
- // Replace delayInit with the initialized object to avoid repeated calls to delayInit.
- Object.defineProperty(window.msBrowser.runtime, "onMessageExternal", {
- value: wrapOnMessageExternal,
- configurable: true,
- enumerable: true
- });
- return wrapOnMessageExternal;
- }
- function sendMessageIndirect(/* optional string extensionId, any message, optional object options, optional function responseCallback */) {
- if (arguments.length < 1) {
- return null;
- }
- // The presence of multiple optional arguments in the order given here is resolved per the logic in chromium source code for alignSendMessageArguments.
- // Javascript allows a function to be called with extra parameters at end that are discarded.
- // Chrome parsed the arguments from the end so the behavior with extra args will not match "regular javascript expectation".
- 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) {
- // This is ambiguous state actually, e.g. developer may be passing a string for the request and options object and expecting default extension id.
- // But chrome will treat it as having a non-default-extension id and use the options object for the request.
- // 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;
- }
- function delayInitStorage() {
- var wrapStorage = Object.defineProperties({}, {
- "local": {
- get: delayInitLocal,
- configurable: true,
- enumerable: true
- },
- "onChanged": {
- get: delayInitOnChanged,
- configurable: true,
- enumerable: true
- }
- });
- function delayInitOnChanged() {
- var wrapOnChanged = Object.defineProperties({}, {
- "addListener": {
- value: nativeMsBrowser.storage.onChanged.addListener.bind(nativeMsBrowser.storage.onChanged),
- configurable: true,
- enumerable: true
- }
- });
- // Replace delayInit with the initialized object to avoid repeated calls to delayInit.
- Object.defineProperty(window.msBrowser.storage, "onChanged", {
- value: wrapOnChanged,
- configurable: true,
- enumerable: true
- });
- return wrapOnChanged;
- }
- function delayInitLocal() {
- var wrapLocal = Object.defineProperties({}, {
- "get": {
- value: nativeMsBrowser.storage.local.get.bind(nativeMsBrowser.storage.local),
- configurable: true,
- enumerable: true
- },
- "getBytesInUse": {
- value: nativeMsBrowser.storage.local.getBytesInUse.bind(nativeMsBrowser.storage.local),
- configurable: true,
- enumerable: true
- },
- "set": {
- value: nativeMsBrowser.storage.local.set.bind(nativeMsBrowser.storage.local),
- configurable: true,
- enumerable: true
- },
- "remove": {
- value: nativeMsBrowser.storage.local.remove.bind(nativeMsBrowser.storage.local),
- configurable: true,
- enumerable: true
- },
- "clear": {
- value: nativeMsBrowser.storage.local.clear.bind(nativeMsBrowser.storage.local),
- configurable: true,
- enumerable: true
- }
- });
- // Replace delayInit with the initialized object to avoid repeated calls to delayInit.
- Object.defineProperty(window.msBrowser.storage, "local", {
- value: wrapLocal,
- configurable: true,
- enumerable: true
- });
- return wrapLocal;
- }
- // Replace delayInit with the initialized object to avoid repeated calls to delayInit.
- Object.defineProperty(window.msBrowser, "storage", {
- value: wrapStorage,
- configurable: true,
- enumerable: true
- });
- return wrapStorage;
- }
- function delayInitTabs() {
- var wrapTabs = Object.defineProperties({}, {
- "create": {
- value: nativeMsBrowser.tabs.create.bind(nativeMsBrowser.tabs),
- configurable: true,
- enumerable: true
- },
- "query": {
- value: nativeMsBrowser.tabs.query.bind(nativeMsBrowser.tabs),
- configurable: true,
- enumerable: true
- }
- });
- // Replace delayInit with the initialized object to avoid repeated calls to delayInit.
- Object.defineProperty(window.msBrowser, "tabs", {
- value: wrapTabs,
- configurable: true,
- enumerable: true
- });
- return wrapTabs;
- }
- function delayInitBrowserAction()
- {
- var wrapBrowserAction = Object.defineProperties({}, {
- "onClicked": {
- get: delayInitOnClicked,
- configurable: true,
- enumerable: true
- }
- });
- function delayInitOnClicked() {
- var wrapOnClicked = Object.defineProperties({}, {
- "addListener": {
- value: nativeMsBrowser.browserAction.onClicked.addListener.bind(nativeMsBrowser.browserAction.onClicked),
- configurable: true,
- enumerable: true
- }
- });
- // Replace delayInit with the initialized object to avoid repeated calls to delayInit.
- Object.defineProperty(window.msBrowser.browserAction, "onClicked", {
- value: wrapOnClicked,
- configurable: true,
- enumerable: true
- });
- return wrapOnClicked;
- }
- // Replace delayInit with the initialized object to avoid repeated calls to delayInit.
- Object.defineProperty(window.msBrowser, "browserAction", {
- value: wrapBrowserAction,
- configurable: true,
- enumerable: true
- });
- return wrapBrowserAction;
- }
- })(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