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() { function fetchParentDomain() {
if (parentDomain) { return parentDomain ?
return Promise.resolve(); Promise.resolve() :
} API.getTabUrlPrefix()
return msg.send({ .then(newDomain => {
method: 'invokeAPI', parentDomain = newDomain;
name: 'getTabUrlPrefix', });
args: []
})
.then(newDomain => {
parentDomain = newDomain;
});
} }
function updateExposeIframes() { function updateExposeIframes() {
@ -168,14 +163,9 @@ self.INJECTED !== 1 && (() => {
} }
if (STYLE_VIA_API) { if (STYLE_VIA_API) {
API.styleViaAPI({method: 'updateCount'}).catch(msg.ignoreError); 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() { 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) => get: (target, name) =>
(...args) => Promise.resolve(self.msg.sendBg({ (...args) => Promise.resolve(self.msg[target[name] ? 'send' : 'sendBg']({
method: 'invokeAPI', method: 'invokeAPI',
name, name,
args args