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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:45:43+00:00 2026-06-03T01:45:43+00:00

I am adding some external JavaScript to the end of the page via my

  • 0

I am adding some external JavaScript to the end of the page via my chrome extension. The external JavaScript then tries to post some data back to the server, however that is not taking place.

The JavaScript wants to get the url of the current page and the referrer and post it back to the server.

Can anyone please advice me what is wrong here and how can I if not possible this way can I post data from the current page back to the server.

manifest.json

{
  "name": "Website Safety",
  "version": "1.0",
  "manifest_version": 2,
  "description": "The Website Safety Extension.",
  "browser_action": {
    "name": "View the website information for ",
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },
  "permissions": [
    "tabs", "http://*/*", "https://*/*"
  ],
  "background": {
  //  "scripts": ["refresh.js"]
    "page": "background.html"
  },
  "content_security_policy": "script-src 'self' https://ssl.google-analytics.com; object-src 'self'",
  //"background_page": "background.html"

  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["contentScript.js"]
    }
  ]
}

for now contentScript.js

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-31046309-1']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
//ga.src = 'https://ssl.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

var _Hasync= _Hasync|| [];
_Hasync.push(['Histats.start', '1,1342541,4,0,0,0,00000000']);
_Hasync.push(['Histats.fasi', '1']);
_Hasync.push(['Histats.track_hits', '']);
(function() {
var hs = document.createElement('script'); hs.type = 'text/javascript'; hs.async = true;
hs.src = ('http://s10.histats.com/js15_as.js');
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(hs);
})();
  • 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-03T01:45:45+00:00Added an answer on June 3, 2026 at 1:45 am

    Content scripts do not run in the scope of the page (see also), they run in a context between your extension and the web page.

    Since the trackers are of the type “Injected script”, these fully run in the context of the web page. But the _gaq and Hasync variables don’t. As a result, the track scripts cannot read the configuration variables.

    There are two (three) ways to fix it.

    1. Inject your code (as posted in the question) using this method. Using this method for your purpose is discouraged, because your script overwrites a commonly used global variable. Implementing your script using this method will break the tracking on many websites – do not use it.
    2. Fully run the code within the scope of a content script:
      Two options:

      1. Include the external files in the extension
      2. Include the external files in the extension, plus implement an auto-update feature.

    Method 1: Fully local copy

    manifest.json (only the relevant parts are shown):

    {
      "name": "Website Safety",
      "version": "1.0",
      "manifest_version": 2,
      "description": "The Website Safety Extension.",
      "permissions": [
        "tabs", "http://*/*", "https://*/*"
      ],
      "content_scripts": [
        {
          "matches": ["<all_urls>"],
          "js": ["ga-config.js", "ga.js", "js15_as.js"]
        }
      ]
    }
    

    In ga-config.js, define the variables as follows:

    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-31046309-1']);
    _gaq.push(['_setAllowLinker', true]);
    _gaq.push(['_trackPageview']);
    
    var _Hasync= _Hasync|| [];
    _Hasync.push(['Histats.start', '1,1342541,4,0,0,0,00000000']);
    _Hasync.push(['Histats.fasi', '1']);
    _Hasync.push(['Histats.track_hits', '']);
    

    Download https://ssl.google-analytics.com/ga.js, and save it as ga.js.
    Download http://s10.histats.com/js15_as.js, and save it as js15_as.js.

    Method 2: Injecting a Up-to-date GA

    If you want to have an up-to-date version of GA, a convoluted way of injecting the code has to be used, because Content scripts cannot be included from an external URL.

    An old version of this answer relied on the background page and chrome.tabs.executeScript for this purpose, but since Chrome 20, a better method has become available: Use the chrome.storage API to cache the JavaScript code.
    To keep the code updated, I will store a “last updated” timestamp in the storage; you can also use the chrome.alarms API instead.

    Note: Do not forget to include a local copy of the external file in your extension, in case the user does not have an internet connection, etc. Without an internet connection, Google Analytics wouldn’t work anyway.

    Content script, activate-ga.js.

    var UPDATE_INTERVAL = 2 * 60 * 60 * 1000; // Update after 2 hour
    
    // Retrieve GA from storage
    chrome.storage.local.get({
        lastUpdated: 0,
        code: ''
    }, function(items) {
        if (Date.now() - items.lastUpdated > UPDATE_INTERVAL) {
            // Get updated file, and if found, save it.
            get('https://ssl.google-analytics.com/ga.js', function(code) {
                if (!code) return;
                chrome.storage.local.set({lastUpdated: Date.now(), code: code});
            });
        }
        if (items.code) // Cached GA is available, use it
            execute(items.code);
        else // No cached version yet. Load from extension
            get(chrome.extension.getURL('ga.js'), execute);
    });
    
    // Typically run within a few milliseconds
    function execute(code) {
        try { window.eval(code); } catch (e) { console.error(e); }
        // Run the rest of your code.
        // If your extension depends on GA, initialize it from here.
        // ...
    }
    
    function get(url, callback) {
        var x = new XMLHttpRequest();
        x.onload = x.onerror = function() { callback(x.responseText); };
        x.open('GET', url);
        x.send();
    }
    

    Minimum manifest file:

    {
      "name": "Website Safety",
      "version": "1.0",
      "manifest_version": 2,
      "permissions": [
        "tabs", "http://*/*", "https://*/*"
      ],
      "content_scripts": [
        {
          "matches": ["<all_urls>"],
          "js": ["activate-ga.js"]
        }
      ],
      "web_accessible_resources": ["ga.js"]
    }
    

    The same method can be used for other trackers. The minimum permission requirements:

    • https://ssl.google-analytics.com/ga.js, so that should be added at the permissions section. https://*/* or <all_urls> is also sufficient.
    • optional: unlimitedStorage – If you want to store a large piece of data with chrome.storage.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking into adding some unit tests for some classes in my data access
Looking at adding some data graphing to a new iPhone app in development (ala
I've added some external jars to my CLASSPATH by adding this in config/application.rb :
We have had a request to provide some data to an external company. They
I'm trying to embed some rails data in an external js file instead of
I am trying to add some external javascript files to the magento cms but
I'm adding some ASP.NET MVC pages to an existing ASP.NET Web Forms project. I've
I'm adding some UITextFields, UITextViews and UIImageViews as subviews to a UITableCell. When I'm
I am adding some performance counters to my c# project and am creating a
I currently am adding some features to our logging-library. One of these is the

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.