Skip to content

Commit

Permalink
Replicate more messages to be sent to a content script
Browse files Browse the repository at this point in the history
  • Loading branch information
Stvad committed Dec 25, 2021
1 parent c32447f commit 5711c13
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
10 changes: 8 additions & 2 deletions src/background/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ function initTabState(sender) {
}
}

const getTabId =
async sender => sender?.tab?.id
|| (await Browser.getActiveTab()).id

browser.runtime.onMessage.addListener(async (data, sender) => {
console.log("background message", data, sender)

Expand Down Expand Up @@ -211,13 +215,15 @@ browser.runtime.onMessage.addListener(async (data, sender) => {
}

if (data.type === 'use_in_statement') {
await browser.runtime.sendMessage({
const message = {
type: 'use_in_statement',
dataype: data.dataype,
value: data.value ? data.value : null,
wdEntityId: data.entityId ? data.entityId : null,
reference: data.reference ? data.reference : null,
})
}
await browser.runtime.sendMessage(message)
await browser.tabs.sendMessage(await getTabId(sender), message)
}


Expand Down
15 changes: 10 additions & 5 deletions src/background/push-enitiy-to-sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ import browser from 'webextension-polyfill'
import {getInternalUrlForEntity} from "../core/navigation"
import {setSidebarUrl} from "./navigation"

function pushEnitiyToSidebar(id, tid, setPanel = true, nocache = false) {
async function pushEnitiyToSidebar(id, tid, setPanel = true, nocache = false) {
if (!sidebarLocked && setPanel) {
return setSidebarUrl(tid, getInternalUrlForEntity(id, nocache))
await setSidebarUrl(tid, getInternalUrlForEntity(id, nocache))
} else {
return browser.runtime.sendMessage({
console.log("pushing entity_add to", tid)
await browser.tabs.sendMessage(tid, {
type: 'entity_add',
id: id,
})
await browser.runtime.sendMessage({
type: 'entity_add',
id: id,
});
}
})
}
}

export { pushEnitiyToSidebar }
5 changes: 4 additions & 1 deletion src/sidebar/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ content.innerHTML = '';
]);
let entities = await wikidataGetEntity(currentEntity);
let e = entities[currentEntity];
let currentTab = await getCurrentTab();
// todo run-context incompatibility
// let currentTab = await getCurrentTab();
let currentTab = null

let description = getValueByLang(e, 'descriptions', false);
let hasDescription = description != false;
Expand Down Expand Up @@ -108,6 +110,7 @@ content.innerHTML = '';
};

browser.runtime.onMessage.addListener(async (data, sender) => {
console.log("add-content event", data)
if (data.type === 'entity_add') {
clearBouncer();
if (!receivedEntities.includes(data.id)) {
Expand Down
13 changes: 8 additions & 5 deletions src/update-status.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import browser from 'webextension-polyfill'
import {Browser} from "./core/browser"

function updateStatus(parts) {
browser.runtime.sendMessage({
type: 'status',
message: parts,
});
async function updateStatus(parts) {
const message = {
type: 'status',
message: parts,
}
await browser.runtime.sendMessage(message)
await Browser.sendMessageToActiveTab(message)
}

function updateStatusInternal(parts) {
Expand Down

0 comments on commit 5711c13

Please sign in to comment.