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 7757479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:06:21+00:00 2026-06-01T13:06:21+00:00

I have defined a global object in a .js file. For example file1.js contains

  • 0

I have defined a global object in a .js file. For example file1.js contains the global object SomeObject. This file gets loaded in background.html.

Since file1.js is included in background.html, I am able to access the global object in this page. So no issues here.

When an event like click on the extension button is performed, I am running a content script using the chrome.tabs.executeScript(null, {file: "contentscript.js"}); api.

How can I access SomeObject in the contentscript in this case?

  • 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-01T13:06:22+00:00Added an answer on June 1, 2026 at 1:06 pm

    There is no way to get direct access to the background page’s global object from a Content script or injected script.

    To use a background page’s method from an injected script , follow these steps:

    1. Content Script: Bind an event listener to the page example 1.

      Whenever you want to notify the method from the background page:
    2. Injected script: Create and fire this specific event example 1.

      → The event listener from 1) gets triggered.
    3. In this event listener, use chrome.runtime.sendMessage to request the functionality from the background example 2.
    4. In the background page, handle this request using chrome.runtime.onMessage.
    5. Optionally, inject code in the original tab using chrome.tabs.executeScript(tab.id, func).

    To use a background page’s method from a Content script, only steps 3 and 4 are needed.
    Here’s an example, in which the content script communicates with the background page:

    // Example background page
    function some_method(arg_name) {
        return localStorage.getItem(arg_name);
    }
    chrome.runtime.onMessage.addListener(function(request, sender, callback) {
        if (request.type == 'localStorage - step 4') {
            callback( some_method(request.name) );
        } else if (request.type == 'localStorage - step 5') {
            localStorage.setItem(request.name, request.value);
        }
    });
    
    // Example contentscript.js
    chrome.runtime.sendMessage({
        type: 'localStorage - step 4',
        name: 'preference'
    }, function(value) {
        if (value === null) {
            // Example: If no preference is set, set one:
            chrome.runtime.sendMessage({
                type: 'localStorage - step 5',
                name: 'preference',
                value: 'default'
            });
        }
    });
    

    See also

    • SO: What you should know about background scripts vs Content Scripts vs Injected scripts.
    • SO: Example code: Communicating between an Injected script and a Content script.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code in my Global.asax file: Protected Sub Application_PreRequestHandlerExecute(sender As Object,
I have the following structure and object of structure defined in the header file
I have 2 js files in my node.js app File a defines global object
I have defined a tag with a CSS attribute overflow set to scroll. This
I have defined a local mirror for all repositories in the settings.xml file: <mirror>
I have defined my Enums like this. public enum UserType { RESELLER(Reseller), SERVICE_MANAGER(Manager), HOST(Host);
I have defined a fieldset in HTML, and applied the following (simple) CSS rules
I have this code: class API(object): def __init__(self): self.baseuri = http://api.xxx.xxx self.cj = cookielib.CookieJar()
I already have a jQuery(document).ready fragment defined. Since I'm working on a large-scale modular
I have a WPF application with several windows. I would like to define GLOBAL

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.