chrome.tabs.query event not triggering - google-chrome-extension

I've been trying to work with chrome extensions lately and right now I'm just trying to get familiar with the chrome apis.
Specifically, I'm testing out chrome.tabs.query to create an alert with the URL of the currently active tab, but the event doesn't trigger. Here is the code for my background.js file.
chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
var activeTab = tabs[0];
var activeTabUrl = activeTab.url;
alert(activeTabUrl);
});
And here is my manifest.json. I'm pretty sure all the permissions are correct.
{
"manifest_version": 2,
"name": "Time Tracker",
"description": "Track how much time spent on ...",
"version": "1.0",
"permissions": ["history", "bookmarks", "activeTab", "tabs"],
"background":{
"service_worker": "background.js"
},
"content_scripts": [
{
"matches": ["http://*.www.youtube.com/*"],
"js": ["content.js"]
}
],
"browser_action": {
"default_popup": "popup.html",
"default_icon": "/images/clock.png"
}
}
I've also tried out other chrome apis such as chrome.history.onVisited to try and add an eventListener but nothing seems to be working.
I've added a link to the full repo here.
https://github.com/ismail-ahmed0149/Time-Tracker
Update #1 June 2. 2021 (Response to most recent comment)
Here is my most recent code my background script.
function handleUpdated(tabId, changeInfo, tabInfo) {
console.log("Updated tab: " + tabId);
console.log("Changed attributes: ");
console.log(changeInfo);
console.log("New tab Info: ");
console.log(tabInfo);
}
chrome.tabs.onUpdated.addListener(handleUpdated);
chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
var activeTab = tabs[0];
var activeTabUrl = activeTab.url;
alert(activeTabUrl);
});

You'll find docs for chrome.tabs events here:
LINK
(scroll down till the end)
You have to add a "onUpdated" event in your background script.

Related

save REQUEST body(payload) of POST REQUEST through chrome extension

we are looking to read/save a POST REQUEST's body/payload through chrome extension.
Scenario: a target webpage will submit a post request to server once in a while.
Requirement: chrome extension's popup.html has a button with id='startIntercept' and upon clicked we need to save the next firing post request's all details(headers, payload) to a session variable.
we only need to save what post request is been sent from current tab. we are not bothered what response we received for the post request.
through a source we came to know a way to achieve it by "Overwriting window.XMLHttpRequest.prototype.open method" by saving old open method to a variable and create a new open method to intercept any request and save it then pass parameters to old open method to get usual expected page functionality.
code used:
//popup.js
let startIntercept = document.getElementById("startIntercept");
startIntercept.addEventListener("click", async () => {
let [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ["startWork.js"],
});
});
//startWork.js
let oldXHROpen = window.XMLHttpRequest.prototype.open;
console.log('script injected');
window.XMLHttpRequest.prototype.open = function() {
this.addEventListener("load", function() {
const responseBody = this.responseText;
sessionStorage.setItem('payload', responseBody );
});
return oldXHROpen.apply(this, arguments);
};
//manifest.json
"name": "extTracker",
"description": "want to save post request details",
"version": "1.0",
"manifest_version": 3,
"background": {
"service_worker": "background.js"
},
"host_permissions": ["<all_urls>"],
"permissions": ["storage", "activeTab", "scripting"],
"web_accessible_resources": [{
"resources": ["/assets/media/blinkSd.mp3"],
"matches": ["<all_urls>"],
"use_dynamic_url": true
}],
"action": {
"default_popup": "popup.html",
},
*once we have a working model we will enhance manifest file/filter requests to intercept accordingly
the above code fails to save/intercept any request made from current tab even through script 'startWork.js' is successfully injected.
why it fails?? any better approach to achieve this functionality?
Your Expertise and Time are very much appreciated. Thank You

Chrome Extension: Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist

Recently, it is reported that the context menu of my app is vanished. If you remove the app and reinstall it, it works. But the vanishment happens again.
I found an error. I'm not sure if the error causes the vanishment of the context menu. But I'd like to fix this matter, because all I found is this.
This app shows texts you select in a page. When you select texts in an ordinaly page and click browser action button, it works without error. But if you try it on Google Docs, you will get error "Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist".
I'm afraid I don't know what to do with this. And I might have two problems. It'll be great help if you could give me some advice.
[manifest.js]
{
"manifest_version": 2,
"name": "Test Chrome Extension",
"short_name": "Test",
"version": "1.0",
"description": "This is a test.",
"icons": {
"128": "128.png"
},
"content_scripts": [{
"matches": ["<all_urls>"],
"js": ["googleDocsUtil.js", "content_scripts.js"]
}],
"background": {
"scripts": ["background.js"],
"persistent": true
},
"browser_action": {
"default_icon": {
"48": "48.png"
},
"default_title": "Test Chrome Extension"
},
"permissions": [
"contextMenus",
"tabs",
"background",
"http://*/*",
"https://*/*"
]
}
[background.js]
chrome.contextMenus.create({
type: 'normal',
id: 'testchromeextension',
title: 'Test Chrome Extension',
contexts:['selection']
});
chrome.contextMenus.onClicked.addListener(function(info,tab){
if( info.menuItemId == 'testchromeextension' ){
var selectedText = info.selectionText.replace(/ /g, "\n");
doSomething(selectedText);
}
});
chrome.browserAction.onClicked.addListener( function(tab) {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {method: "getSelection"}, function(response) {
doSomething(response.data);
});
});
});
function doSomething(selectedText) {
console.log(selectedText);
}
[content_scripts.js]
chrome.runtime.onMessage.addListener( function(request, sender, sendResponse) {
if (request.method == "getSelection") {
var post_val = window.getSelection().toString();
if ( !post_val ) {
var googleDocument = googleDocsUtil.getGoogleDocument();
post_val = googleDocument.selectedText;
}
sendResponse({data: post_val});
}
});
I believe this error is caused when you update the local version of an extension and then try to use the extension with its old/not-updated source code.
The fix: after you reload your local extension at chrome://extensions/, make sure you refresh the page you're using the extension on. You should no longer see the error.

In Chrome Extension development, how do I pass a variable from background.js to a content script that embedded as a script node?

Update: I am sorry that I wasn't clear about my case. The content script is not literally content script but a js file embedded into the page via the createElement method.
It's something like:
// this is "content.js"
var scriptElement = document.createElement('script');
scriptElement.src = 'chrome-extension://' + chrome.runtime.id + '/js/page-inject.js';
(document.head || document.documentElement).appendChild(scriptElement);
I want to use it in "page-inject.js", not the "content.js"
I thought it was very simple but failed after hours of retrying and searching.
I have a background.js like this:
chrome.tabs.query({active: true, currentWindow: true},
function(tabs) {
chrome.tabs.executeScript(tabs[0].id, {
code: 'window.my_var = 123'
}, function() {
chrome.tabs.executeScript(tabs[0].id, {file: 'js/content.js'});
});
});
After this, when I type "window.my_var" in the console of that page. It's not working. The wanted "123" is not showing up.
If I replace the code: 'window.my_var = 123' to alert(123), it works. But what I need is just to pass a variable from my background script (background.js) to the web page that my extension injected into.
Is there anything wrong with my try?
As #wOxxOm already pointed out you how to see the variable inject using the chrome.tabs.executeScript in the web page console by changing the Top in the console.
There is another way you can easily pass values from background to content script using messaging.
Se below a simple example:
manifest.json
{
"name": "test",
"version": "0.0.1",
"manifest_version": 2,
"description": "Test ",
"background": {
"scripts": [
"background.js"
]
},
"browser_action": {
"default_title": "Test "
},
"permissions": [
"*://*/*",
"tabs"
], "content_scripts" : [{
"matches" : ["*://*/*"],
"js" : ["content.js"]
}]
}
background.js
chrome.browserAction.onClicked.addListener(function(){
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {data: "some value"});
});
});
content.js
chrome.extension.onMessage.addListener(handleMessage);
function handleMessage(request) {
console.log(request.data);
}
chrome.extension.getBackgroundPage() will return the global window object of the background page. You can access scripts and any data in the background worker.
Example
const backgroundDOMWindow = chrome.extension.getBackgroundPage();
const myGlobalVariable = backgroundDOMWindow.window['myGlobalVariable'];
Adding script to global background object "background.js"
document.write(
'<script src="main.js"></script>'+
'<script src="main2.js"></script>'
);

Making sure Content Script is injected into current tab page without any Page-Refresh after reloading extension from chrome://extensions

I'm trying to make an extension that scrapes email IDs from the current tab open in the Chrome browser.
After reloading the extension from chrome://extensions when I go to the tab where I want to test the extension I'm having to refresh the page in order to see the desired results in the console in spite of executing the Content Script in background.js using chrome.tabs.executeScript in order to reinject it into the current tab in the browser.
Content Script - Relevant portion
var jsonData = scrape();
console.log(jsonData); //THIS I SEE WITHOUT PAGE REFRESH
chrome.runtime.sendMessage(jsonData, function(response) {
console.log(response); //WITHOUT PAGE REFRESH shows undefined, AFTER PAGE REFRESH shows the desired response
});
background.js
var background = {
injectScript: function() {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.executeScript(tabs[0].id, {file: "myscript.js"});
});
}
};
background.injectScript();
manifest.json
{
"manifest_version": 2,
"name": "emailScraper",
"version": "1.0",
"description": "Email Scraping Chrome Extension",
"icons": {
"64": "tiki_1.3.png"
},
"browser_action": {
"default_icon": "tiki_1.3.png",
"default_popup": "popup.html"
},
"background": {
"scripts": ["js/app/background.js",
"js/lib/socket.io/socket.io.js"]
},
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*", "https://mail.google.com/*", "https://google.com/*", "file://*/*"],
"js": ["js/lib/jquery/jquery.min.js", "js/app/myscript.js"],
"all_frames": false
}
],
"permissions": [
"tabs",
"http://*/*",
"https://*/*"
],
"content_security_policy": "script-src 'self' http://localhost:1441/; object-src 'self'"
}
I'd also like to add that the Content Script is communicating with the popup.js of my extension. It sends the list of the email IDs scraped to popup.js.popup.js on receiving the list sends a response to Content Script. This exchange of information takes place only after the current page is refreshed but not otherwise. I wonder why so.
popup.js
var app = angular.module('emailScraper',[]);
app.controller('AppCtrl', ['$scope', '$http', function($scope, $http) {
//Fetch URL of current Tab open in Chrome
chrome.tabs.query({
active: true,
currentWindow: true
}, function(tabs) {
// and use that tab to fill in out title and url
var tab = tabs[0];
$scope.cpUrl = tab.url;
console.log($scope.cpUrl); //I SEE ONLY THIS LINE WHEN I INSPECT POPUP
});
$scope.appLoaded = false;
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
console.log("Message received: ", request); //I SEE ONLY AFTER PAGE REFRESH IN Inspect Popup BUT NOT IN popup.html view
$scope.emailList = request;
$scope.count = Object.keys($scope.emailList).length;
console.log("Emails found: " + $scope.count); //I SEE ONLY AFTER PAGE REFRESH IN Inspect Popup BUT NOT IN popup.html view
$scope.appLoaded = true;
sendResponse({status: "Received JSON data!"});
});
}]);
I appreciate your response.

Chrome Extension Send Message From Background.js to Content Script

I have read the documentation on how to do Send Message From background javascript file(main.js) to Content Script (content.js) but I cannot get the onMessage to open my alert.
Manifest.json
{
"name": "Example",
"version": "1.0.1",
"manifest_version" : 2,
"description": "Example Description",
"background" : {
"scripts" : ["main.js"]
},
"page_action" : {
"default_icon": {
"19": "icons/19.png",
"38": "icons/38.png"
},
"default_title" : "Example Title"
},
"content_scripts": [{
"matches": ["<all_urls>"],
"js": ["lib/jquery-1.8.3.min.js","scripts/content.js"],
"run_at": "document_idle",
"all_frames": false
}],
"permissions": [
"tabs",
"geolocation"
],
"icons": {
"16": "icons/16.png",
"48": "icons/48.png",
"128": "icons/48.png"
}
}
Background javascript file (main.js)
chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
chrome.tabs.sendMessage(tabs[0].id, {action: "SendIt"}, function(response) {});
});
Content javascript file (content.js)
chrome.extension.onMessage.addListener(function(msg, sender, sendResponse) {
if (msg.action == 'SendIt') {
alert("Message recieved!");
}
});
Thanks to the insight of #Teepeemm I have included a tab load complettion before sending message to content script.
WAIT FOR TAB TO BE FULLY LOADED
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (changeInfo.status == 'complete') {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
chrome.tabs.sendMessage(tabs[0].id, {action: "SendIt"}, function(response) {});
});
}
});
Sidenote: chrome.extension.onMessage is deprecated, you should be using chrome.runtime.onMessage - although I don't believe this will solve your problem.
I remember that I had an issue injecting a minified jquery file using content scripts. Try to use an un-minified version (ie jquery-1.8.3.js). Once you have done that, also add jquery-1.8.3.js to web_accessible_resources in your manifest file. (Read about that here)
If it still doesn't work, my last suggestion would be to add "<all_urls>" to the permissions array in your manifest.
If you have each script announce its presence (I prefer console.log to alert), you see that the background script runs once (on installation or startup), while the content script runs with each new page. This means that you'll want to have some external event trigger the message. Something like
chrome.pageAction.onClicked.addListener(function(tab) {
chrome.tabs.sendMessage(tab.id,{action:"SendIt"});
});
And don't forget to call chrome.pageAction.show(tabId); as appropriate.

Resources