Extension only works when Chrome starts - google-chrome-extension

I am testing my Chrome extension by using the following codes:
chrome.tabs.create({url: "login_popup.html"}, function(tab){
chrome.extension.sendRequest({oid: tab.openerTabId});
});
However, a new tab was created only when Chrome started, but no tabs were created when I opened open a new page. Shouldn't the chrome.tabs.create be executed whenever a new page is loaded?
The manifest.json is:
{
"name": "minus_test",
"version": "1.0",
"background_page": "minus_test.html",
"permissions": [ "*://*/", "tabs" ]
}
Thanks!

No. background_page gets executed once when Chrome starts at which point it creates a new tab. If you want to create a new tab ever time a new window is opened you need to use the chrome.windows.onCreated listener.

Related

Getting cookies on contextMenu tab instead of active window

So I explain my issue and lets see if someone knows what I'm doing wrong.
I am creating a chrome extension that adds an option in the context menu. As part of the action I need to fetch one cookie and with the value of the cookie, open a new tab.
My problem is seems to be with the Mac version (I cannot reproduce it on Windows, but I haven't tried other linux distros) that if the user has a normal window and an incognito window opened, if he has the focus on the incognito window, and does the right-click on the normal window to trigger the contextMenu action (MacOS doesn't change the focus), chrome tries to get the cookie from the incognito window and open the new tab in the incognito window, which is the one focused, instead of the window that received the right-click.
Any Idea on how to fix it?
(In order to reproduce, you need to Allow in incognito)
script.js
function openTab(info, tab) {
console.log(tab.url); // returns the clicked url, not the incognito
chrome.cookies.getAll({}, function(cookies){
console.log(cookies); // shows only the cookies of the incognito
// this will open the tab in the incognito window
chrome.tabs.create({
url: "https://www.example.com"
});
});
}
chrome.contextMenus.create({
"title":"Open tab",
"contexts":["link"],
"onclick":openTab
});
With the following manifest
{
"name": "Getting cookies context change",
"description": "Example of what goes wrong while getting cookie",
"version": "0.1",
"permissions": [
"contextMenus",
"cookies",
"activeTab",
"<all_urls>"
],
"background": {
"scripts": ["script.js"]
},
"manifest_version": 2
}

console.log - Chrome extension doesn't execute console.log, it just skips console.log [duplicate]

This question already has answers here:
Accessing console and devtools of extension's background.js
(9 answers)
Closed 6 years ago.
My Chrome extension doesn't seem to execute console.log in any way. It skips it.
console.log works: I can open the Console, type console.log('test'); and it works. It works from other scripts, i.e. a JS script loaded in a HTML page locally, but not from my extension.
My background.js that I run through manifest.json below. It executes alert(), but not console.log
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(null, { file: "jquery-3.0.0.min.js" }, function() {
console.log('----');
alert('Hello!);
});
});
I've tried:
delete console.log, delete window.console and delete window[console] as suggested by Restoring console.log() and JavaScript console log in Magento, but no success.
loaded the extension in Incognito mode, with no other extensions loaded, but no success.
updated background.js to consist of only a single line - console.log('----'); - still doesn't work.
making sure that the console reports "All", not just errors.
My manifest.json:
{
"manifest_version": 2,
"name": "My Chrome Extension",
"description": "This extension will you save time.",
"version": "1.0",
"permissions": [
"http://*/*",
"https://*/*",
"tabs"
],
"browser_action": {
"name": "Click to start the extension"
},
"background": {
"scripts": ["background.js"]
}
}
The console for your extension is in a separate window.You can inspect your console.log() messages by:
Go to chrome://extension
Enable developer mode
Click on the "background page" link at the "Inspect views" line
The developer console for your extension will open here.
Credits to: Where to read console messages from background.js in a Chrome extension?

Scanning Text through a chrome extension to auto launch links

I'm pretty new at chrome extensions and am trying to make a simple one that automatically launches links in my emails. I am going to modify it a bit later on, but for now, this is all I am trying to do. How do I have a chrome extension automatically read the text of the current tab that I am on, or when I open emails if I can get that specific? I have a manifest file set up and currently can make the extension button launch a link, but I'd rather have this happen automatically, as I don't want to hit a button to launch a link when I could just click the link itself.
manifest.json
{
"manifest_version": 2,
"name": "MT task launcher",
"description": "This extension launches Task Links in emails",
"version": "1.0",
"background": {
"scripts": ["task.js"]
},
"browser_action": {
"default_icon": "icon.png",
"default_title": "Email Task Launcher"
},
"permissions": [
"activeTab"
]
}
task.js
chrome.browserAction.onClicked.addListener(function(tab) {
var action_url = "http://www.reddit.com";
chrome.tabs.create({ url: action_url });
});
Take a look at Official Guide, for your purpose, I think you should use content scripts ( which are injected into current web page), then read the DOM and get all the links. To open the links, you can either call window.open() or by passing message then open them via chrome.tabs.create
There are two options to do that, it's either edit the local copy of the extension or to inject the call to the extension.
Inject code as a Content script, use the matching rules as defines in the manifest file
A background page file use the 'chrome.tabs.onUpdated' event. Also, use the 'chrome.tabs.executeScript' method to inject script.

I can't activate a custom chrome content script extension outside of developer mode

I am having problem activating my own custom made script. It's working in the developer mode, when I load it as a directory. But when I pack it using the method provided by chrome it does not work.
It creates a .pem and .crx file, and when I drag and drop the extension inside the chrome://extensions page it shows. When pressing f5 it is deactivated every time, and when I check "also allow in incognito mode", that checkbox disappears immediately and doesn't come back.
Below my manifest file contents:
{
"manifest_version": 2,
"name": "Test script",
"version": "0.1",
"incognito": "split",
"content_scripts": [
{
"matches": [
"http://mail.google.com/*",
"https://mail.google.com/*",
"https://accounts.google.com/*"
],
"js": [
"jquery-2.1.4.min.js",
"content.js"
]
}
]
}
Any clue what's happening?
Chrome blocks extension installs on Windows unless they come from Chrome Web Store.
Install Chrome from a developer channel or use another OS.

Chrome Extension: Event Page For New Bookmark Events

I'm completely new to Chrome Extensions. I want the creation of a bookmark to trigger a xmlhttprequest. Right now, I'm just trying to get a new bookmark event to do a console.log and can't see what I'm missing.
Here is my manifest.json:
{
"manifest_version": 2,
"name": "Booky Desktop Integration",
"description": "Sends New Chrome Bookmarks To Your Booky Desktop.",
"version": "1.0",
"background": {
"scripts": ["background.js"],
"persistent": false
},
"permissions": [
"bookmarks",
"http://bookydesktop.com/"
]
}
Here is my js:
chrome.bookmarks.onCreated.addListener(function(id, bookmark) {
console.log("bookmark created");
});
What am I missing?
Your code works perfectly as written. You're probably not viewing the console for your background page. You need to:
Open chrome://extensions/ (or click "Extensions" in Chrome's "Settings" menu)
Ensure "Developer mode" is ticked in the top right
Open the console by clicking "_generated_background_page.html (Inactive)" in the "Inspect views" list under your extension
Each page in Chrome has its own console instance. You were looking at the consoles of ordinary web pages, instead of looking at the console for your background page.

Resources