Encountering an error where property 'onClicked' is not defined when attempting to utilize chrome.action or chrome.browserAction in Chrome

I am currently developing a Chrome extension that will automatically redirect me to a specific URL when I click on the browser action icon.

Here is the code snippet that I am trying to implement:

chrome.browserAction.onClicked.addListener

However, I am encountering the following error:

Uncaught TypeError: Cannot read property 'onClicked' of undefined

Below is my manifest file configuration:

{
    "name": "My First Extension",
    "version": "1.0",
    "description": "Redirects to specified link upon clicking icon",
    "browser_action": {
        "default_icon": "icon.png",
        "default_title": "Perform action"
    },
    "permissions": ["tabs", "http://*/*"],
    "content_scripts": [{
        "matches": ["http://*.example.com/*", "https://*.example.com/*"],
        "js": ["script.js"]
    }],
    "icons": {
        "16": "icon.png",
        "48": "icon.png",
        "128": "icon.png"
    }
}

This is the content of my JavaScript file:

chrome.browserAction.onClicked.addListener(function(tab) { alert("hi"); });

Answer №1

Transitioning to ManifestV3

manifest.json: Update by adding action instead of browser_action, refer to the migration guide.

  "action": {},
  "background": {"service_worker": "background.js"},

background.js: Make sure to use chrome.action and not chrome.browserAction.

For Classic ManifestV2 Users

If you have previously included

"background": {
    "scripts": ["background.js"]
}

and encounter an error like

Cannot read property 'onClicked' of undefined
, simply add

"browser_action": {}

to your manifest.json file.

Answer №2

It appears that the code resides in your twterland.js file, specifically within your content script. Please note that browserAction is only accessible on extension pages and cannot be utilized in content scripts.

For more information, refer to the documentation: https://developer.chrome.com/extensions/content_scripts

Nevertheless, it's important to acknowledge that content scripts come with certain restrictions. They are unable to:
- Utilize chrome.* APIs (with the exception of parts of chrome.extension)
- Access variables or functions defined by their extension's pages
- Access variables or functions defined by web pages or other content scripts

Consider placing the code on the background page instead.

Answer №3

If the "browser_action" property is not included in your manifest.json file, you may encounter this particular error. In addition to following @Kirill's solution, make sure to also include an empty icon.png file to prevent Chrome from reporting an error related to the missing image file.

To resolve this issue and prevent the error from appearing, add the following line to your manifest.json file:

"browser_action": {}

For more detailed instructions on using the "browser_action" setting, refer to the official Chrome Extension documentation.

Answer №4

If you encounter the same issue while using manifest_version 3, here are some key changes to make:

  • Replace "background.scripts" with "background.service_worker"
  • Replace "browser_action" with "action"
  • In your JavaScript code, update instances of chrome.browserAction with chrome.action

For more detailed information and guidance, refer to: Manifest version 3 migration documentation

Answer №5

After facing the same issue, I found that including

"persistent": true

in my background definition within manifest.json fixed the problem.

Answer №6

It is important to note that in your manifest.json file, you can only have one of the following options: app, browser_action, or page_actions.

For instance, if you want to utilize the chrome.browserAction.setBadgeText function, you must include the browser_action field in your manifest.json.

Answer №7

Ever since the introduction of manifest v3, the browser_action has been replaced with action. Click here for more information

To update your manifest file accordingly, make sure to include the following:

"action": {},

You can then manage the extension click event using the following code:

chrome.action.onClicked.addListener(function (tab) {...})

Answer №8

To avoid any issues, it is important to ensure that jquery.js does not come before background.js in the scripts array of background within manifest.json.

"background": {
    "scripts": ["background.js","jquery.js"]
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilize the power of XMLHttpRequest to fetch and load numerous audio files, seamlessly integrating them for playback through the Web Audio

I am looking to create a web application that loads three different audio files, each one second long, in a specific order, and then merges them into a single Audio Buffer consecutively. To illustrate my goal, here is a sample code snippet: var AudioCo ...

Using jQuery to create dynamic elements that fade out with timers

My website has a simple message system that displays messages in a floating div at the top of the page. Each message is supposed to fade out after a certain amount of time, but I want users to be able to pause the fading process by hovering over the messag ...

Guidelines for establishing authentic headers on a SignalR connection

Can headers be set on the SignalR connection directly? I am aware of setting query string parameters but it is not secure enough for my specific scenario. var conn = ($ as any).hubConnection(); conn.url = URL; conn.qs = { "token": SECRET_KEY }; conn ...

Is it possible to launch an Electron application by clicking a run button in the VSCode

I'm new to using VSCode and am currently working on an HTML5 app with Electron. I'm finding it cumbersome to switch between windows and enter a command each time I want to test my application. Is there a way to configure VSCode to run my Electron ...

Magical Stylist - Eradicate Indicators while Preserving Labeling

Recently, I've been experimenting with the Google styling wizard in an effort to remove markers while retaining labels for businesses. My objective is to eliminate the marker icons but still display the text labels such as "Jimmy Johns," "Boone Saloon ...

What is the best way to transfer an id from JavaScript to Rails while utilizing ajax?

Is there a way to call a rail route and pass in an ID from javascript? I have recently started using rails routes within my application, even in js. The current code I am using is: # app/assets/javascript/verifying_link.js $.ajax({ url: "/verify_link/ ...

Even with React.memo(), functional components continue to trigger re-renders

I am facing an issue with my button component that contains a button inside it. The button has a state isActive and a click function passed to it. Upon clicking the button, the isActive flag changes, triggering the app to fetch data accordingly. However, t ...

Angular Reacts to Pre-Flight Inquiry

I'm facing an issue with my interceptor that manages requests on my controllers. The back-end web API has a refresh token implementation, but when I try to refresh my token and proceed with the request, I encounter the error message "Response to prefl ...

Issues with the functionality of the ZeroClipboard Angular plugin

"> I'm fairly new to Angular and currently experimenting with a module called ZeroClipboard. I've made adjustments to my application in order to incorporate the module and configured it as per the demonstration. var app = angular.module ...

"Triggering an AJAX POST request with a click event, unexpectedly receiving a GET response for

Hello there! I am currently attempting to send an AJAX POST request when a button is clicked. Below is the form and button that I am referring to: <form class="form-horizontal" > <fieldset> <!-- Form Name --> <legend> ...

Having trouble getting the Bootstrap tooltip to work on a Select option?

Is there a way to have a tooltip displayed for each option in the select box? <select ng-model="rightList" class="form-control" size="13" multiple> <option ng-repeat="item in selectedList" value="{{$ ...

When the form is submitted, the values of the items are being reverted back

I'm currently working on a single web page using nodejs, expressjs, and mongoDB. On the page, I have two text boxes that define the filter clause for the collection in mongoDB. Additionally, there is an HTML table that displays the documents from the ...

Exploring Angular 2 Beta 8: An Introduction to @Query Usage

My attempt to utilize @Query to fetch data regarding an element in my template has not been successful. I made an effort using the following approach: Referenced here. Here is a snippet of my code, import {Component, Query, QueryList, ElementRef} from &a ...

In React, ensure a component's state is preserved when the browser history changes

I recently developed a React application that features a classic layout with a left-side menu, title, footer, and main content section. The side menu includes navigation links structured using components such as <List>, <ListItem>, etc. from th ...

Moving from one page to another

I am attempting to create a transition effect between sections within a single-page application. All the sections are contained on the same page, with only one section displayed at a time while the rest are set to display none. When a specific event is tri ...

Correctly define the vm function within the Controller As scope

I am a newcomer to javascript and AngularJS. So... Maybe it's a simple question, but I've noticed two ways to define functions in javascript. In the following controllers, pay attention to "grupoCancha" and "grupoVisible" (I included the entire ...

Having trouble triggering a click event on Ant Design menu button using jest and enzyme

Troubleshooting the simulation of a click event on the Menu component using Antd v4.3.1 Component: import React from 'react' import PropTypes from 'prop-types' import { Menu } from 'antd' import { SMALL_ICONS, PATHS } fro ...

Exploring Javascript bugs in Visual Studio (or any other JS debugger)

I am currently working with a .js file that is executed using cscript.exe and not in a browser environment. I am aware that I can use the //X parameter with cscript.exe to trigger a debugger selection prompt. This works well when choosing "Visual Studio 2 ...

The promise is only guaranteed to resolve correctly upon the refreshing of the page

Exploring an API that retrieves a list of Pokemon and related data, the code snippet below demonstrates how to achieve this. export function SomePage() { const [arr, setArray] = useState([]); useEffect(() => { fetchSomePokemon(); }, []); f ...

When attempting to use Ajax, the operation fails; however, if I directly access the URL,

When using firebug, I attempted to make the following AJAX call: var rootUrl = 'http://172.24.105.22:8080/geoserver/Chennai_Sub/ows'; var defaultParameters = { service: 'WFS', version: '1.0.0', request: 'Get ...