Comments (10)
from chrome-extensions-samples.
@ChuckStrusz I was hoping Google Chrome's team would take ownership to ensure examples are working. As it stands now the example isn't going to work (by design):
service worker (background.js) in my case shall have:
chrome.contextMenus.onClicked.addListener(async (info, tab) => {
const { id, url } = tab;
const { menuItemId } = info
var query = info.selectionText;
switch (menuItemId) {
case "define-word":
await chrome.sidePanel.open({ tabId: id });
await chrome.sidePanel.setOptions({
tabId: id,
path: 'sidepanel.html',
enabled: true
});
await chrome.scripting.executeScript(
{
target: { tabId: id, allFrames: true },
files: ['clientside_concept.js']
}
)
}
});
than clientside_concepts.js:
// // content script
console.log("Running clientside_concepts.js");
(async () => {
let selection = document.getSelection().toString();
console.log("Selection", selection);
const response = await chrome.runtime.sendMessage({ type: 'concept', data:selection });
console.log("Response from background ", response);
console.log('concept complete')
}
)();
than again in background js I have a part:
} else if (message.type === "concept") {
let data = message.data;
console.log("Concept", data);
senderResponse({ data: data });
return true;
}
than in sidepanel.js
chrome.runtime.onMessage.addListener(async ({ type, data }) => {
console.log('Message received', type, data);
if (type === 'concept') {
// Hide instructions.
document.body.querySelector('#select-a-word').style.display = 'none';
// Show word and definition.
document.body.querySelector('#definition-word').innerText = data;
run('@cf/meta/m2m100-1.2b', {
text: data,
source_lang: "english", // defaults to english
target_lang: "spanish"
}).then((response) => {
console.log(JSON.stringify(response));
if (!response.success) {
console.log("Error", response);
}else{
console.log("Response", response);
// let translated_text=response.result.translated_text
document.body.querySelector('#definition-text').innerText = response.result.translated_text;
}
});
// document.body.querySelector('#definition-text').innerText =
// words[data.toLowerCase()];
}
});
my extension runs cloudflare AI worker to translate English into Spanish. Service worker will not be able to communicate directly with sidepanel.js without clientside message.
@patrickkettner please read https://github.com/GoogleChrome/chrome-extensions-samples/tree/main/functional-samples/sample.sidepanel-dictionary
from chrome-extensions-samples.
Hey @AlexMikhalev, thanks for flagging this. 😃
Out of curiosity, did you select the word "extensions", then right-click and choose the context menu Define?
This sample has a long list of steps, it can be easily missed 😟
from chrome-extensions-samples.
Yes. The point of a bug report is that we need working examples of how to pass data - even the string into the side panel with any reliability that our users will have it working in their environment. The case of nothing selected shall be handled too.
from chrome-extensions-samples.
The fix is to inject client-side script with chrome.runtime.sendMessage
from chrome-extensions-samples.
The fix is to inject client-side script with chrome.runtime.sendMessage
As someone who is floundering with the absolute paucity of good (ie, functioning) examples of V3 extensions, I'm hoping you can offer a bit more detail on this fix for this broken sample (I have the same issues) - is there any chance you could post the full updated source for the file in question, or indicate which file you mean by "client-side script" since I think there are two - both service-worker.js and sidepanel.js are "client-side", right?
from chrome-extensions-samples.
I am having this same issue, but I'll note that the instructions in the README, as written, do not work. To wit, Step 3 is "Open the side panel UI" - but you can't open the side panel UI, because the extension button is disabled (greyed out). I have tried this in a normal and an incognito window with the same results.
from chrome-extensions-samples.
Hi chuck You wouldn’t open the side panel with the extension action button. You open the side panel by clicking on the side panel button that’s built into Chrome. https://support.google.com/chrome/answer/13156494?hl=en#zippy=%2Cuse-side-panel
Oh my gosh! Thanks so much. I use the side panel so infrequently that I often forget it's there.
from chrome-extensions-samples.
Related Issues (20)
- sidepanel does not open even though it's trigger by a user gesture. HOT 2
- The stable version can't play audio normally, HOT 2
- How to obtain request headers for a request without using chrome.devtools.network.onRequestFinished? HOT 1
- Service Worker (无效) HOT 1
- Darman mallotta photos
- 在渲染大量数据数据类型为字符串(1M)的时候,切换浏览器tab页,页面数据显示为[object object] HOT 1
- 在渲染大量数据数据类型为字符串(1M)的时候,切换浏览器tab页,页面数据显示为[object object] HOT 1
- Setting sidePanel default width HOT 1
- Samples should indicate which are usable across browsers HOT 2
- R
- Andrewmichael042c
- code from function-sample-tutorial.quick-api-reference encounters authentication issue with API: Error Code 401 HOT 5
- addToClipboard support HOT 1
- JiwJiw73
- how to specify the content security policy for content script HOT 1
- <!-- Thank you for submitting a Pull Request. Please:
- New
- Using Chrome extension to access the accessibility tree of an element HOT 1
- Accessing and responding to the open/close state of a sidePanel
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from chrome-extensions-samples.