Giter Site home page Giter Site logo

Dictionary side panel example doesn't work with Uncaught (in promise) Error: Could not establish connection. Receiving end does not exist. about chrome-extensions-samples HOT 10 OPEN

AlexMikhalev avatar AlexMikhalev commented on June 4, 2024
Dictionary side panel example doesn't work with Uncaught (in promise) Error: Could not establish connection. Receiving end does not exist.

from chrome-extensions-samples.

Comments (10)

patrickkettner avatar patrickkettner commented on June 4, 2024 3

from chrome-extensions-samples.

AlexMikhalev avatar AlexMikhalev commented on June 4, 2024 1

@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.

AmySteam avatar AmySteam commented on June 4, 2024

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.

AlexMikhalev avatar AlexMikhalev commented on June 4, 2024

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.

AlexMikhalev avatar AlexMikhalev commented on June 4, 2024

The fix is to inject client-side script with chrome.runtime.sendMessage

from chrome-extensions-samples.

ChuckStrusz avatar ChuckStrusz commented on June 4, 2024

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.

ChuckStrusz avatar ChuckStrusz commented on June 4, 2024

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.
extension

from chrome-extensions-samples.

ChuckStrusz avatar ChuckStrusz commented on June 4, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.