By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – Having a hard time using chrome.tabs.sendMessage
JavaScript

Javascript – Having a hard time using chrome.tabs.sendMessage

Admin
Last updated: 2023/12/20 at 4:37 PM
Admin
Share
2 Min Read
Having a hard time using chrome.tabs.sendMessage

Problem:

I’am sure this topic has already been discussed somewhere on this website but as a newbie I can’t really figure this out on my own..

Contents
Problem:Solution:

I’am trying to code a very “simple” chrome extension so that when a user clicks on a certain button, it triggers an alert.

As far as I understood, the click needs to be listened then the background has to communicate with my content in order to display the alert.

Here is my code :

Manifest

{
  "name": "Test alert",
  "version": "1.0",
  "manifest_version": 2,
  "description": "This is a test",
  "browser_action": {
      "default_icon": "icon.png"
    },
    "background": {
        "persistent": false,
        "scripts": ["background.js"]
  },
  "content_scripts" : [{
      "matches": ["<all_urls>"],
      "js" : ["content.js"]
  }]
}

Background

document.getElementById("edit-payment").addEventListener("click", function(tabs){
    chrome.tabs.sendMessage(tabs[0].id, {action: "saySomething"}); });

Content

chrome.runtime.onMessage.addListener(function (msg) {
    if (msg.action === 'saySomething') {
        alert('hello');
    }
});

I’am having trouble when it comes to message passing. Can anyone enlighten me pls ?
Thank you for your time !

I’ve been trying to use chrome.tabs but something is wrong.

Solution:

The background script doesn’t have access to the document and
document.getElementById("edit-payment") won’t work. It is the content script that has access to it!

The service worker (background script):

The extension service worker handles and listens for browser events. There are many types of events, such as navigating to a new page, removing a bookmark, or closing a tab. It can use all the Chrome APIs, but it cannot interact directly with the content of web pages; that’s the job of content scripts.

See: https://developer.chrome.com/docs/extensions/mv3/getstarted/extensions-101/#extension-files

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article How to make the text under my picture change when hovering over the picture (html, css, javascript) Javascript – How to make the text under my picture change when hovering over the picture (html, css, javascript)
Next Article How to prevent continuous axios API requests in useEffect()? Javascript – How to prevent continuous axios API requests in useEffect()?
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?