I
I
Ivan Damaskin2016-06-15 15:31:53
JavaScript
Ivan Damaskin, 2016-06-15 15:31:53

Why doesn't setBadgeText change under the if condition?

Hello.
I'm writing a small extension.
There was a need to change the text near the icon ( setBadgeText ) provided that there is an element with the class .red_nor
Actually, what did I do here.
Here is my manifest object

{
  "manifest_version": 2,

  "name": "MyApp",
  "version": "1.0",
  "description": "Description text", 
  "background_page": "background.html",
  "browser_action": {
    "name": "Manipulate DOM",

  "default_icon": {                   
    "38": "lock-icon.png",         
    "19": "icon.png"           

  }

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


  "content_scripts": [ {
    "js": [ "jquery.min.js", "background.js" ],
    "matches": [ "http://*/*", "https://*/*"]
  }]
}

here is my background.js
var succlass = ".red_nor";
if ($(succlass)) {
    console.log("true");
    chrome.browserAction.setBadgeText({text: "yeah"});
} else {
    console.log("false");
}

The console screams
background.js:56 Uncaught TypeError: Cannot read property 'setBadgeText' of undefined
Actually that's understandable, I'm trying to change the browser element in a script that works with content elements.
So what's the correct way to change the browserAction element given content elements?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Ukolov, 2016-06-15
@alexey-m-ukolov

You need to check like this $(succlass).length:
Even if no object is found, a jquery object will be returned, which is always true.

P
pasha fudko, 2016-06-15
@darkwoolf10

I don’t know for sure, but if you have Jquery, then in background.js in if you need to write $("succlass"), not just $(succlass)

I
Ivan Damaskin, 2016-06-16
@Joannes

In general, it seems like I figured it out. The fact is that when writing an application, there is a certain hierarchy, i.e. there is a separate script that works with browser elements, and there is one that works with the content part of the site.
Respectively it is browser_action and content_scripts
Now there is a question. How to pull out the value value in the content form and insert this value into the url of the script that processes the browser element i.e. i have this code

xhr = new XMLHttpRequest();
    xhr.open("GET", "http://example.ru/1466088862.4504/", true);
    xhr.send(null);
    xhr.onreadystatechange = function() {

it is necessary that instead of a link (1466088862.4504/) there is a variable, and the value of the variable is the value of the value that we pulled out in the content?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question