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
- I wait for page status to be "complete" though my background script
- When page is complete i inject a script through chrome.tabs.executescript()
- My script uses chrome.runtime.sendMessage() to send the desired stats back to background page of my extension where i print them.
- Then i redirect that page to a given URL (hard coded "google.com")
- 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
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
// }
// );
});