Index: chrome/common.js |
=================================================================== |
--- a/chrome/common.js |
+++ b/chrome/common.js |
@@ -51,33 +51,55 @@ |
this._listeners.splice(idx, 1); |
this._wrappedListeners.splice(idx, 1); |
} |
} |
}; |
var MessageEventTarget = function() |
{ |
- WrappedEventTarget.call(this, (chrome.runtime || {}).onMessage || chrome.extension.onRequest); |
+ var target; |
+ if ("runtime" in chrome && "onMessage" in chrome.runtime) |
+ target = chrome.runtime.onMessage; |
+ else if ("onMessage" in chrome.extension) |
+ target = chrome.extension.onMessage; |
+ else |
+ target = chrome.extension.onRequest; |
+ WrappedEventTarget.call(this, target); |
}; |
MessageEventTarget.prototype = { |
__proto__: WrappedEventTarget.prototype, |
_wrapListener: function(listener) { |
return function(message, sender, sendResponse) |
{ |
- return listener(message, {tab: sender.tab && new Tab(sender.tab)}, sendResponse); |
+ if (sender.tab && sender.tab.id >= 0) |
+ sender.tab = new Tab(sender.tab); |
+ return listener(message, sender, sendResponse); |
}; |
} |
}; |
/* API */ |
ext = { |
- backgroundPage: { |
- sendMessage: (chrome.runtime || {}).sendMessage || chrome.extension.sendRequest, |
- getWindow: chrome.extension.getBackgroundPage |
- }, |
+ backgroundPage: {}, |
getURL: chrome.extension.getURL, |
onMessage: new MessageEventTarget(), |
i18n: chrome.i18n |
}; |
+ |
+ if ("runtime" in chrome && "sendMessage" in chrome.runtime) |
+ ext.backgroundPage.sendMessage = chrome.runtime.sendMessage; |
+ else if ("sendMessage" in chrome.extension) |
+ ext.backgroundPage.sendMessage = chrome.extension.sendMessage; |
+ else |
+ ext.backgroundPage.sendMessage = chrome.extension.sendRequest; |
+ |
+ try |
Sebastian Noack
2013/12/19 10:38:59
I would prefer to wrap chrome.extension.getBackgro
Wladimir Palant
2013/12/19 10:47:17
True, that's a better solution - done.
|
+ { |
+ ext.backgroundPage.getWindow = chrome.extension.getBackgroundPage; |
+ } |
+ catch (e) |
+ { |
+ // This exception is expected in content scripts in older Chrome versions. |
+ } |
})(); |