To print console.log message in active window console instead of background.. - google-chrome-extension

I started building chrome extensions. Initially I started with a simple extension which prints the hello in background console. How can I print that in the active window console.
This is my manifest.json file
{
"manifest_version": 2,
"name": "example",
"version": "0.1",
"description": "My Chrome Extension",
"icons": {
},
"background": {
"scripts": ["js/background.js"]
},
"browser_action": {
"default_title": "My test Environment"
},
"permissions": [
"background",
"storage",
"tabs",
"http://*/*",
"https://*/*"
]
}
This is the content of my background.js file
a simple
console.log("hello");

Take a look at Content Script, you could use manifest.json injection or Programming injection to ensure your code run in the context of current webpage.
The former requires Message Passing or Storage to ensure communications between content script with background page;
while as for the latter, Try the following code in background.js
chrome.tabs.executeScript({code: "console.log('hello');"});

Related

Chrome extension not loading content_scripts

I am creating a chrome extension. Problem I am facing is I am able to see the html content but I am not seeing my js files included in content_scripts. I am checking this by inspecting on my extension & checking sources tab.
Mainfest -
{
"manifest_version": 2,
"name": "My Plugin",
"description": "some description",
"version": "1.0",
"browser_action": {
"default_popup": "popup.html"
},
"content_scripts": [
{
"matches": [
"http://*/*",
"https://*/*"
],
"js": ["myPopup.js"]
}
],
"permissions": [
"activeTab",
"declarativeContent",
"storage"
]
}
myPopup.js -
console.log("hello world")
What could I be doing wrong?
Source tab will display the scripts that are actually injected into page DOM.
If you are trying to debug the content script, then add a debugger to your content script.
Open the developer tools and reload the page, you should be able to see content script in the source tab.
enter image description here

the click on chrome extension icon doesn't trigger chrome.browserAction.onClicked each time I clicked the icon

I'm developing a chrome extension and I want to detect every time a user clicks the extension icon.
I used chrome.browserAction.onClicked.addListener(function(){console.log('icon clicked')}) but I get the message one time only, when I click second time, third time, ... I get nothing. I don't know where is the problem
Below a portions of code of the extension:
manifest.json
{
"manifest_version": 2,
"name": "My ext",
"version": "0.1",
"browser_action": {
"default_title": "My ext"
},
"permissions": [
"tabs", "<all_urls>"
],
"background":
{
"scripts": ["background.js"]
} }
background.js
chrome.browserAction.onClicked.addListener(function(tab) {
console.log("icon clicked")
// do something
});

Script doesn't fully execute in Chrome Extension

Hopefully this is something simple. I'm testing a simple Chrome Extension script and it appears it'll execute part of the script, but won't complete it. For example, if I add an alert() to the beginning of a script, it will execute the alert. But if I place it after anything calling the chrome DOM object, it won't execute. Here's an example:
Will execute alert
alert("Test");
chrome.webRequest.onCompleted.addListener(function (request) { });
Will not execute alert
chrome.webRequest.onCompleted.addListener(function (request) { });
alert("Test");
Am I missing something?
Here is my manifest:
{
"background": {
"persistent": true,
"scripts": [
"scripts/libs/jquery.1.11.2.min.js",
"scripts/background.js"
]
},
"browser_action": {
"default_icon": "resources/icon.19.png"
},
"icons": {
"48": "resources/icon.48.png"
},
"manifest_version": 2,
"name": "Test",
"permissions": [
"<all_urls>",
"webNavigation",
"webRequest",
"webRequestBlocking"
],
"version": "1.0"
}
You are missing debugging it yourself.
Go to chrome://extensions/ and load the Dev Tools for your background page. You will see an uncaught exception that stops execution.
For webRequest events, you must include a filter argument to the addListener function.

Chrome Extensions - Script works only once, when I download a page the first time

The problem is that after it downloads the page the first time, it works, but if I click on "home" link, the script doesn't work anymore.
manifest:
{
"manifest_version": 2,
"name": "Facebook No Advs",
"description": "This extension TRY to avoid advertisement from Facebook Home.",
"version": "1.0",
"permissions": ["https://www.facebook.com/*"],
"content_scripts": [
{
"matches": ["https://www.facebook.com/*"],
"js": ["jquery-1.11.0.min.js", "noAdvsScript.js"],
"css": ["NoAdvsStyle.css"]
}
]
}

Chrome Extension - Console Log not firing

I'm starting to learn to make my own Chrome Extensions, and starting small.
At the moment, I'm switching from using the alert() function to console.log() for a cleaner development environment.
For some reason, console.log() is not displaying in my chrome console logs. However, the alert() function is working just fine.
Can someone review my code below and perhaps tell me why console.log() isn't firing as expected?
manifest.json
{
"manifest_version": 2,
"name": "Sandbox",
"version": "0.2",
"description": "My Chrome Extension Playground",
"icons": {
"16": "imgs/16x16.png",
"24": "imgs/24x24.png",
"32": "imgs/32x32.png",
"48": "imgs/48x48.png"
},
"background": {
"scripts": ["js/background.js"]
},
"browser_action": {
"default_title": "My Fun Sandbox Environment",
"default_icon": "imgs/16x16.png"
},
"permissions": [
"background",
"storage",
"tabs",
"http://*/*",
"https://*/*"
]
}
js/background.js
function click(e) {
alert("this alert certainly shows");
console.log("But this does not");
}
// Fire a function, when icon is clicked
chrome.browserAction.onClicked.addListener(click);
As you can see, I kept it very simple. Just the manifest.json and a background.js file with an event listener, if the icon in the toolbar is clicked.
As I mentioned, the alert() is popping up nicely, while the console.log() appears to be ignored.
Try to open the "_generated_background_page.html" in extension page (chrome://extensions/)

Resources