Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7653789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:06:46+00:00 2026-05-31T12:06:46+00:00

I’m trying to figure out how to write a simple Chrome extension which allows

  • 0

I’m trying to figure out how to write a simple Chrome extension which allows to transliterate web pages from one alphabet to another. Unfortunately Google’s documentation on Chrome extensions is pretty much confusing for a beginner. I’ve seen a lot of similar questions here, f.ex. Replace text in website with Chrome content script extension, but still can’t get it clear. In a trial run I’m trying to replace all “a”‘s in the page with “Z”‘s.

Here’s my Manifest.json:

{
  "name": "My Chrome extension",
  "version": "0.1",
  "browser_action": {
    "default_icon": "icon.png"
},

"permissions": [
    "tabs", "http://*/*", "https://*/*"
],

"content_scripts": [{
    "matches": ["http://*/*", "https://*/*"],
    "js": ["myscript.js"]
}]

}

Myscript.js:

chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(
  null, {code:"document.body.innerHTML = document.body.innerHTML.replace(new RegExp("a", "g"), "Z")"});
});

But this fails to work. If I include only one line into Myscript.js:

document.body.innerHTML = document.body.innerHTML.replace(new RegExp("a", "g"), "Z");

then all ‘a’ letters get replaced with ‘Z’ as soon as the page has loaded, but this is not my goal, as I want to get it working only after the extension button is pressed.

Any help will be much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:06:47+00:00Added an answer on May 31, 2026 at 12:06 pm

    You’ve set up your extension to inject your code using a content script. A content script will get injected into any page that matches your matches field. What you want is to only inject the script if they click on your Browser action. For this you can either have a background page that listens for the chrome.browserAction.onClicked event and reacts to it. Or you can have a default page for your Browser Action that injects the code into the page and if your put window.close as part of the script this will stop any popup from occuring and allow you to avoid using a background page (Im big on avoiding using a background page when ever possible).

    For an example of the first method, check out this sample….
    http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extensions/docs/examples/api/browserAction/make_page_red/

    And here’s an example of the second method (which is a modified version of the set_page_color sample)…
    manifest.json

    {
      "name": "A browser action that changes the page color.",
      "version": "1.0",
      "permissions": [
        "tabs", "http://*/*", "https://*/*"
      ],
      "browser_action": {
          "default_title": "Set this page's color.",
          "default_icon": "icon.png",
          "default_popup": "popup.html"
      },
      "manifest_version": 2
    } 
    

    popup.html

    <!doctype html>
    <html>
      <head>
        <script src="popup.js"></script>
      </head>
      <body>
      </body>
    </html>
    

    popup.js

    chrome.tabs.executeScript(null,{file:"injectedCode.js"});
    window.close();
    

    injectedCode.js

    // This was just a quick look at changing text on a page
    // there could be more tags you should ignore and there could be a better way of doing this all together
    var ignoreTags = ["NOSCRIPT","SCRIPT","STYLE"];
    var nodeIterator = document.createNodeIterator(
        document,
        NodeFilter.SHOW_TEXT,
        function (node){
          var parentTag = node.parentNode.tagName.toUpperCase();
          if ( ignoreTags.indexOf(parentTag)==-1 ) {return true} else {return false};
        },
        false
    );
    
    var node;
    while ( (node = nodeIterator.nextNode()) ) {
       node.data = node.data.replace(/a/g, 'z');
    }
    

    NOTE
    As Rob W points out in the comments chrome.tabs and chrome.browserAction are not usable in a content script which is why the chrome.browserAction.onClicked has to be used in a background page.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a text area in my form which accepts all possible characters from
I'm making a simple page using Google Maps API 3. My first. One marker
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.