How to open my Chrome Extension in new Tab? - google-chrome-extension

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.

Related

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

How to Log to Console from 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?

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"
}
}

Chrome extension - Notification just work in apache

I already configured my chrome extension to send notification like this:
I have a button that has a event that call createNotification. But the button just send notification if I'm running the extension on apache.
What I'm doing wrong?
Is equal that example http://jsbin.com/ziwod/1/edit?html,js,output
And for me this example just work when I click in "Run with JS".
This is my manifest
{
"name": "EXTENSION EXAMPLE",
"version": "0.0.1",
"manifest_version": 2,
"description" : "This extension is...",
"icons": { "16": "icons/16x16.png", "48": "icons/48x48.png", "128": "icons/128x128.png" },
"omnibox": { "keyword" : "example" },
"browser_action": {
"default_icon": {
"19": "icons/19x19.png",
"38": "icons/38x38.png"
},
"default_title": "Launcher",
"default_popup": "browseraction/popup.html"
},
"background": {
"scripts": ["background.js"],
"persistent": false
},
"permissions" : ["notifications"],
"options_page" : "notification.html",
"chrome_url_overrides" : {
"newtab": "newtab/newtab.html"
},
"devtools_page": "devtools/devtools.html"
}
This is my notification.html http://codepen.io/anon/pen/VYWQwB
From your html file and the manifest file of "manifest_version": 2 is that Content Security Policy is enabled by default. And Chrome developers chose to be strict about it and always disallow inline JavaScript code - only code placed in an external JavaScript file is allowed to execute (to prevent Cross-Site Scripting vulnerabilities in extensions). So <button onclick="notifyMe()"> is not allowed in your html file. onclick attribute is an inline script. You should assign an ID attribute instead: <button id="button">.

Resources