0

I coded a simple extension in to redirect my web page to a desired site with JavaScript disable through content settings for all URLS.

My code is

  1. I wait for page status to be "complete" though my background script
  2. When page is complete i inject a script through chrome.tabs.executescript()
  3. My script uses chrome.runtime.sendMessage() to send the desired stats back to background page of my extension where i print them.
  4. Then i redirect that page to a given URL (hard coded "google.com")
  5. Back to step 1

My aim is take multiple reading of "https://google.com" page load time with DOM count and Resources count etc Although it does not give any error but the timing it provides do not match with Dev-Tool Timings. Now i am unsure which is wrong Dev-Tool or Performance.Timings. Only first Time it matches the timings after my redirects it never matches with chrome DEV-TOOL.

If Someone wants to test it i have included all of the code below.

MY CHROME BUILD IS BELOW

Windows 10 (64 Bit) also tested on UBUNTU 16.04 with Chromium 70.0.3538.77
Google Chrome   70.0.3538.110 (Official Build) (64-bit) 
Revision    ca97ba107095b2a88cf04f9135463301e685cbb0-refs/branch-heads/3538@{#1094}
OS  Windows
JavaScript  V8 7.0.276.40
Flash   31.0.0.153 

My printing format is URL,DOM_Loaded,ON_LOAD,DOM_Count,Req_Count

Dom_loaded : performance.timing.domContentLoadedEventEnd-performance.timing.navigationStart,
on_load : performance.timing.loadEventEnd - performance.timing.navigationStart,
Dom_count : document.getElementsByTagName('*').length,              
req_count : window.performance.getEntriesByType("resource").length

enter image description here

Manifest

{
    "name" : "JS BLOCK settings",
    "version" : "1",
    "description" : "Block JS of Brower",
    "icons":{
        "128" : "js-logo.png",
        "48" : "js-logo.png",
        "16" : "js-logo.png"
    },
    "permissions": [
        "browsingData", 
        "contentSettings",
        "tabs",
        "<all_urls>" 
    ],
    "browser_action": {
        "default_icon": "js-logo.png"


    },
    "background": {
        "scripts": ["background.js"]

    },
    "externally_connectable": {
    "ids": [
      "*"
    ],
    "matches": ["https://www.google.com/"],
    "accepts_tls_channel_id": false
  },
    "manifest_version": 2
}

self_script.js

function send_stats(){

    chrome.runtime.sendMessage(

        {
            Dom_loaded : performance.timing.domContentLoadedEventEnd-performance.timing.navigationStart,
            on_load : performance.timing.loadEventEnd - performance.timing.navigationStart,
            Dom_count : document.getElementsByTagName('*').length,              
            req_count : window.performance.getEntriesByType("resource").length

        }, function(response) {
        }
    );

    console.log("IT WORKS");

}

send_stats();

background.js

document.addEventListener('DOMContentLoaded', blockjs);

var mytabid = 0

var b = "https://google.com" 
chrome.tabs.onUpdated.addListener(
    function (tabId , info) {
        if (info.status == 'complete') {
            mytabid = tabId
            console.log("Going to exec")
            chrome.tabs.executeScript(tabId , {
                file: "self_script.js"
            // code: new_redirect
            });

            setTimeout(function(){
                console.log("JUST BEFORE UPDATE")
                chrome.tabs.update(mytabid,{url: b})
            },5000)

      }
});


function blockjs(tab) {

    chrome.contentSettings['javascript'].set({
        primaryPattern:  "<all_urls>",
        setting: 'block'
    })

}

// URL,DOM_Loaded,ON_LOAD,DOM_Count,Req_Count
chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {


    console.log(sender.tab.url+","+request.Dom_loaded+","+request.on_load+","+request.Dom_count+","+request.req_count)

    // var millisecondperhour = 3600000;
    // var onehourago = (new Date()).getTime() - millisecondperhour;
    // chrome.browsingData.remove({
    // "since": onehourago
    // }, {
    //     "appcache": true,
    //     "cache": true,
    //     "cookies": true
    //  }
    // );


});
6
  • I can't reproduce it here in Chrome 70 or 72 in Windows 7 so the problem might be a platform-specific bug in Chrome. Or maybe you have another extension somehow interfering with the results - try in a new browser profile with just your extension.
    – woxxom
    Commented Dec 4, 2018 at 5:09
  • My tests were in window 10 Chrome 70. But will try it again on different Chrome with my extension only
    – drainzerrr
    Commented Dec 4, 2018 at 5:11
  • @wOxxOm can you kindly show me screen shots i just tested it again on a freshly installed Chrome but same results :(
    – drainzerrr
    Commented Dec 4, 2018 at 7:45
  • also tested on UBUNTU 16.04 with Chromium 70.0.3538.77 no change
    – drainzerrr
    Commented Dec 4, 2018 at 7:54
  • Its solved I was taking the time from navigation.start while chrome takes domainLookupStart
    – drainzerrr
    Commented Dec 4, 2018 at 8:22

1 Answer 1

1

enter image description here

The Issue is of chrome.tabs.update(mytabid,{url: b}) in (Background.js). Normally when we request the webpage it starts the event of navigation.start but when we redirect the web page the chrome takes time after the redirect.end which is DomainlookupStart.

One can also choose fetch start but for this case browser cache was disable so no point taking fetchStart.

For exact answer change background.js

Dom_loaded : performance.timing.domContentLoadedEventEnd-performance.timing.navigationStart,
on_load : performance.timing.loadEventEnd - performance.timing.navigationStart,
Dom_count : document.getElementsByTagName('*').length,              
req_count : window.performance.getEntriesByType("resource").length

TO

Dom_loaded : performance.timing.domContentLoadedEventEnd-performance.timing.domainLookupStart,
on_load : performance.timing.loadEventEnd - performance.timing.domainLookupStart,
Dom_count : document.getElementsByTagName('*').length,              
req_count : window.performance.getEntriesByType("resource").length

Not the answer you're looking for? Browse other questions tagged or ask your own question.