2

I just started learning the basics of chrome extensions. Using a "Page Redder" extension, which changes the background color of the entire webpage, as a template I would like to make an extension that changes the background color of all text.

Page Redder from https://developer.chrome.com/extensions/samples: background.js:

// 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.body.style.backgroundColor="red"'
  });
});

manifest.json:

{
  "name": "Page Redder",
  "description": "Make the current page red",
  "version": "2.0",
  "permissions": [
    "activeTab"
  ],
  "background": {
    "scripts": ["background.js"],
    "persistent": false
  },
  "browser_action": {
    "default_title": "Make this page red"
  },
  "manifest_version": 2
}

Can I change the following lines of code to select all text instead?

chrome.tabs.executeScript({
        code: 'document.body.style.backgroundColor="red"'

I tried changing it to:

chrome.tabs.insertCSS("content.css")

with content.css:

input[type='text']
{
  background-color: red;
}

but with no success.

Brian
  • 21
  • 4
  • 2
    I don't know anything about Chrome extensions, but `input[type='text']` refers to `` fields, *not* all text on the page. To do that, you'd need to select `

    ` tags or whatever `

    `s may contain the text. There's not a CSS selector for all text of any sort that I am aware of.
    – thunderblaster Oct 13 '15 at 21:53
  • There will be a problem with specificity.. – Xan Oct 14 '15 at 00:42
  • Unfortunately, highlighting only text using CSS is nontrivial; see for example http://stackoverflow.com/q/18746649/689161 or http://stackoverflow.com/q/4899373/689161 – gengkev Oct 14 '15 at 01:33
  • Ah ok, thanks! I'm trying to eventually create a custom highlighter tool and I figured it would be best to take it one step at a time. Looks like I will look into word searching first. – Brian Oct 14 '15 at 03:48

0 Answers0