support all API methods in content scripts

This commit is contained in:
tophf 2020-02-11 15:26:53 +03:00
parent e9584b2cab
commit df883f2c72
2 changed files with 16 additions and 20 deletions

View File

@ -134,17 +134,12 @@ self.INJECTED !== 1 && (() => {
}
function fetchParentDomain() {
if (parentDomain) {
return Promise.resolve();
}
return msg.send({
method: 'invokeAPI',
name: 'getTabUrlPrefix',
args: []
})
.then(newDomain => {
parentDomain = newDomain;
});
return parentDomain ?
Promise.resolve() :
API.getTabUrlPrefix()
.then(newDomain => {
parentDomain = newDomain;
});
}
function updateExposeIframes() {
@ -168,14 +163,9 @@ self.INJECTED !== 1 && (() => {
}
if (STYLE_VIA_API) {
API.styleViaAPI({method: 'updateCount'}).catch(msg.ignoreError);
return;
} else {
API.updateIconBadge(styleInjector.list.length).catch(console.error);
}
// we have to send the tabId so we can't use `sendBg` that is used by `API`
msg.send({
method: 'invokeAPI',
name: 'updateIconBadge',
args: [styleInjector.list.length]
}).catch(console.error);
}
function orphanCheck() {

View File

@ -238,9 +238,15 @@ self.msg = self.INJECTED === 1 ? self.msg : (() => {
}
})();
self.API = self.INJECTED === 1 ? self.API : new Proxy({}, {
self.API = self.INJECTED === 1 ? self.API : new Proxy({
// Handlers for these methods need sender.tab.id which is set by `send` as it uses messaging,
// unlike `sendBg` which invokes the background page directly in our own extension tabs
getTabUrlPrefix: true,
updateIconBadge: true,
styleViaAPI: true,
}, {
get: (target, name) =>
(...args) => Promise.resolve(self.msg.sendBg({
(...args) => Promise.resolve(self.msg[target[name] ? 'send' : 'sendBg']({
method: 'invokeAPI',
name,
args