How to Log to Console from Chrome Extension? - google-chrome-extension

Given a Chrome extension that sets up a browser_action based on a default_icon click, how can that extension print to the console? For example, say my manifest.json includes this:
{
"name": "Testing",
"version": "1.0",
"browser_action": {
"default_popup": "testing.html",
"default_icon": "icon.png"
},
"manifest_version": 2
}
and my testing.html loads a <script> with some Javascript:
console.log("WHY DON'T I SEE THIS??");
Why doesn't this print anything to the console? And how might I go about getting something printed to the console?

Related

Google extension not showing up when I try to load it

I tried to make an useless extension so I could map CTRL W to "open" this extension instead of closing the tab. When I click Load Unpacked the folder with the manifest.json it loads the extension without any errors but the extension doesnt show up at all... Did I do something wrong?
manifest.json:
{
"name": "CTRLW",
"manifest_version": 2,
"version": "1.0",
"icons": { "128": "128.png" }
}

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
});

To print console.log message in active window console instead of background..

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');"});

Chrome extension "version error"

So I was testing out chrome extensions and learning how to use them but I get the same error when I upload my code... It says
Failed to load extension from: ~\Desktop\chromeExtension
The 'manifest_version' key must be present and set to 2 (without quotes). See developer.chrome.com/extensions/manifestVersion.html for details.
I have a manifest.json file and icon.png file
manifest.json:
{
"name": "Hello World!",
"version": 2,
"description": "My first Chrome extension.",
"browser_action": {
"default_icon": "icon.png"
}
}
icon.png:
Can anyone tell me what I did wrong? I uploaded my chrome extension at the chrome://extension page, hit the developer mode button, pressed load unpacked extension, and then opened a file called chromeExtension containing the manifest.json and icon.png. Any ideas on how to fix the error?
You need to set the manifest_version, not just the version. The version key is simply the version of your extension:
{
"name": "Hello World!",
"version": "1.0",
"manifest_version": 2,
"description": "My first Chrome extension.",
"browser_action": {
"default_icon": "icon.png"
}
}

How to open my Chrome Extension in new Tab?

Hi I have found other similiar questions on stackoverflow but none of them solved the purpose.
I want my chrome extension/app to be opened in a full tab like how POSTMAN extension is opened.
My manifest.json
{
"name": "Sample App",
"manifest_version": 2,
"version": "0.0.1",
"app": {
"background": {
"scripts": ["main.js"]
}
},
"icons": { "128": "icon.png" },
"permissions" : ["tabs" ]
}
My main.js (alias for background.js)
chrome.app.runtime.onLaunched.addListener(function() {
chrome.tabs.create({'url': chrome.extension.getURL('index.html')}, function(tab) {
alert("Hi");
});
});
index.html is the file i want to load on opening the new tab.
My first time responding here on stackoverflow, please be gentle...
I discovered that it's much easier to add "launch" : { "local_path" : "index.html" } within the manifest.json file. See my sample manifest file below.
{
"manifest_version" : 2,
"name": "Hello World!",
"description": "My first Chrome App.",
"version": "0.1",
"app": {
"launch" : {
"local_path" : "index.html"
}
},
"icons": { "16": "icon.png" }
}
Keep in mind that this example is very basic, it has been stripped of some unnecessary information such as a background script but it should accomplish what you want.
http://developer.chrome.com/apps/first_app.html
chrome.app.runtime.onLaunched is only for Chrome apps, not extensions. The code for your background page will automatically run when the Chrome browser starts, so you can start directly with chrome.tabs.create(...).
Also, you need to include index.html and any resource included in your extension that the page will use in a web_accesible_resources section in your manifest.

Resources