nicocappa
nicocappa

Reputation: 116

Trying to mess around with a sample extension but my JS doesn't seem to work

I'm messing around with the Page Redder sample extension, however I can't figure out why my JS won't work. The original extension changes the background color to red when the extension is clicked. I'm trying to make it so it changes certain words found by their CSS selector to red instead, in my case the selector is "var.d" here is the JS: (The JS at the bottom that is commented in is the original code)

// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can    be
// found in the LICENSE file.

// Called when the user clicks on the browser action.
chrome.browserAction.onClicked.addListener(function(tab) {
// No tabs or host permissions needed!
console.log('Turning ' + tab.url + ' red!');
chrome.tabs.executeScript({
code: 'document.querySelectorAll("var.d");'

for (var i = 0; i < xx.length; i++) {
xx[i].style.color="red";
}

 });
});


// 'document.body.style.backgroundColor="red";

Upvotes: 0

Views: 106

Answers (1)

goblin
goblin

Reputation: 1553

Your code will not work because the for loop is not included in "code" attiribute and the xx variable is not declared.

Try the code below. Just make sure that "var.d" selectors really exist or else you will not get any result.

chrome.browserAction.onClicked.addListener(function(tab) {
  // No tabs or host permissions needed!
  console.log('Turning ' + tab.url + ' red!');
  chrome.tabs.executeScript({
    code: 'var xx = document.querySelectorAll("var.d"); for (var i = 0; i < xx.length; i++) {xx[i].style.color="red";}'});
  });

Upvotes: 1

Related Questions