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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:00:30+00:00 2026-06-03T02:00:30+00:00

I created an add-on using add-on builder. I attached a content-script to the pageMod

  • 0

I created an add-on using add-on builder. I attached a content-script to the pageMod in main.js

My content script counts the number of dynamic tags created using document.createElement(). This is done by creating a hook to document.createElement() and added this function to webpage by creating a script tag. My code is as follows.

contentscriptFile:

addJS_Node ("var count=0;");
function LogDocCreateElement ()
{
    var oldDocumentCreateElement = document.createElement;   
    document.createElement = function(tagName)
    {
       var elem = oldDocumentCreateElement.apply (document, arguments);     
       console.log("Dynamically created a(n)", tagName);
       count++;     
       return elem;
    } 
}
addJS_Node (null, null, LogDocCreateElement);
function addJS_Node (text, s_URL, funcToRun) {
     var D                                   = document;
     var scriptNode                          = D.createElement ('script');
     scriptNode.type                         = "text/javascript";
     if (text)       scriptNode.textContent  = text;
     if (s_URL)      scriptNode.src          = s_URL;
     if (funcToRun)  scriptNode.textContent  = '(' + funcToRun.toString() + ')()';
     var targ = D.getElementsByTagName ('head')[0] || D.body || D.documentElement;
     targ.appendChild (scriptNode);    
}
window.addEventListener("load", function() {alert(count) }, false);

Now I am getting uncaught exception: ReferenceError: count is not defined.

How can I access this count variable?

  • 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-03T02:00:31+00:00Added an answer on June 3, 2026 at 2:00 am

    Please see the documentation on how content scripts access web pages – they don’t see any custom properties and methods that the web page added to the DOM. In your case you would need to access the count variable via unsafeWindow object:

    unsafeWindow.count++;
    

    However, as the documentation notes you should avoid using unsafeWindow if somehow possible. Here the obvious course of action would be to avoid creating the count variable in the page altogether. So instead of this:

    addJS_Node ("var count=0;");
    

    You would just declare the variable:

    var count = 0;
    

    But you would need to replace unsafeWindow.document.createElement rather than document.createElement in order for this change to be visible to the web page. Avoiding unsafeWindow here will be way more complicated, maybe you can use mutation events instead?

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

Sidebar

Related Questions

I have GUI created using netbeans GUI builder. I want to add there an
I have created an add-on using firefox's add-on builder. I am able to create
I am using the Add-on Builder to create an icon in the addon toolbar
I've created a simple Outlook 2007 add-in using C# which loops through a selection
I created a rotating cube using the below code and add bitmaps as sides
I created a sample hello world application using Flash Builder 4.5.1 (with the update
I created an flv video player using Flash Builder 4. This BasicVideoPlayer project is
I am trying to create an add-in by using the article below http://blogs.microsoft.co.il/blogs/shair/archive/2008/07/28/how-to-create-sql-server-management-studio-addin.aspx I
I'm using Mime::Lite to create and send E-Mails. Now I need to add support
I have created a add input field function which is working fine. I would

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.