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

  • Home
  • SEARCH
  • 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 8454389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:01:01+00:00 2026-06-10T12:01:01+00:00

Ok, so what I want is to override a method that already exists inside

  • 0

Ok, so what I want is to override a method that already exists inside a tab, what I’m going to use is the default alert function.
Override it inside the JS function would be very easy. just add

window.alert = function(){ 
   //Do Something
}

but the problem is that when I try to use chrome.tabs.executeScript("window.alert = function() { };"); it doesn’t work. I tried to do this manually by using the Console from Chrome in the tab that I wanted to override the function, I typed that override function in the log and pressed enter, and done, the alert function was overridden, but I can’t do this via Chrome Extension.

When you add executeScript, it seems like it creates a Javascript apart from the one inside the tab DOM, because I can create functions with the name of a function that already exists inside the tab DOM.

Is there a way to make executeScript to write the script inside of the tab DOM, so it can actually override any function that was written by the .js file the page generated?

Thanks!

  • 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-06-10T12:01:02+00:00Added an answer on June 10, 2026 at 12:01 pm

    Functions don’t exist as part of the DOM; instead, they exist within an execution environment that includes the DOM. Content scripts (including scripts run with executeScript) and actual web pages share the same DOM, but have separate execution environments. So calling window.alert = function() {} only rewrites window.alert within your content script’s execution environment, not in the actual page’s one.

    The typical way to reach the execution environment of the actual page is to inject a <script> tag into the DOM. This can be done in several ways. One method is to white-list a script in web_accessible_resource, and insert the <script> element referring to this script in the document. The required absolute URL can be obtained via chrome.extension.getURL.

    var s = document.createElement("script");
    s.src = chrome.extension.getURL("script_in_extension.js");
    (document.head||document.documentElement).appendChild(s);
    

    Make sure that the script is configured to "run_at": "document_start", so that the overwrite takes place before any of the page’s functions are loaded.

    Note: Your action can easily be undone by the page:

    window.alert = function(){ /*...*/ }; // Your overwrite
    delete window.alert;                  // Run from the page/console/...
    window.alert('Test?');                // Displays alert box.
    

    If it’s critical that the overwritten function cannot be removed, use Object.defineProperty to define an immutable method. For more details, see Stop a function from execute with Chrome extension.

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

Sidebar

Related Questions

I'd like to use a map that would be equivalent to ConcurrentMap (I want
I want to override the SubmitChanges() method for my model. Whern I try to
I want to override the Tostring() method for changing some characters. Is it possible?
This is my code. I want to override the Draw method import wx.grid as
I want to override the validation_errors() method of the form helper in CodeIgniter for
If i want to override the Thread#start() method can I? and if yes will
How do I override a class special method? I want to be able to
I want to override the default integer constructors in Haskell so they produce strings
I have a UserControl that is override Paint() with my own drawings. I want
I want to put an gridview with images inside an expandable list...I've already make

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.