I am developing a chrome extension, here are the main files:
background.js
getPageDimension = function (){ chrome.tabs.getSelected(null, function(tab) { chrome.tabs.sendMessage(tab.id, { message: "DIMENSION" }, function(response){ if (response != null) { console.log(response.x); console.log(response.y); console.log(response.w); console.log(response.h); }else{ console.log('Response is null'); } }); }); };
content.js
chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) { if (msg.message && (msg.message == "DIMENSION")) { var dimension = getPageDiemension(document.documentElement); console.log(dimension.x); console.log(dimension.y); console.log(dimension.w); console.log(dimension.h); sendResponse({x: dimension.x, y: dimension.y,w: dimension.w,h: dimension.h}); } }); getPageDiemension = function(currentDom){ var dimension = new Object(); dimension.x = 0; dimension.y = 0; dimension.w = currentDom.scrollWidth; dimension.h = currentDom.scrollHeight; return dimension; }
So, my goal is to get the full height and width of the page loaded in the current active tab. When I debug my script content, I get the correct answer in my background.js , but if you run the script without debugging, I get an undefined answer in my background.js .
Here is the declaration of my cotent.js in my manifest.json file:
"content_scripts": [{ "all_frames": true, "matches": [ "http://*/*", "https://*/*" ], "js": ["content.js"] }],
Please help me, where am I mistaken? Let me know if you need more details.
source share