Getting cookies on contextMenu tab instead of active window - google-chrome-extension

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
}

Related

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.

Click make popup disapear

I'm currently developping a chrome extension in order to watch youtube videos in a popup window while browsing an other website.
It works just fine, the problem is that if I click on a link or switch fullscreen app (Mac) the popup disapear, how can I make the popup bubble stay "for ever" ?
The manifest :
{
"manifest_version": 2,
"name": "One-Click Youtube",
"description": "Youtube on one click",
"version": "1.0",
"permissions": ["tabs" , "contextMenus"],
"browser_action": {
"default_icon": "icon.png",
"default_popup": "ytplayer.html"
}
}
There is an always on top window api for apps. A demo is here: https://chrome.google.com/webstore/detail/floating-youtube/jjphmlaoffndcnecccgemfdaaoighkel
You cannot. The popup will always close and destroy the page when it loses focus.
This behavior is by design; you need a different UI, maybe open a window with type: "popup".
If and when window type "panel" becomes available (probably never), your question can be solved better.

execute background scripts only once in my chrome extension

I am developing a chrome extension. My scripts are called everytime I reload a page or open a new tab. How I do make it to load the extension only once, i.e., when chrome starts and reset all values to default on browser close.
I did go through most of the links available here in stackoverflow but was unable to focus it down to my situation.
manifest.json
"background":{
"scripts":["common.js","example.js"],
"persistent":true
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["myscript.js"]
}
],
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
"permissions": ["http://*/*",
"https://*/*",
"contextMenus",
"tabs"]
And in one of my background javascripts, I am triggering an event:
example.js
var DOMContentLoaded_event = document.createEvent("Event");
DOMContentLoaded_event.initEvent("DOMContentLoaded", true, true);
window.document.dispatchEvent(DOMContentLoaded_event);
The above code is to trigger DONContent so that, the user need not click on the extension image everytime he boots the Chrome browser.
The trigger event is getting called each time my page loads, whether it is a reload of the
same page or open another tab, the event is getting called. I know I am missing something major here. I did setting things in localStorage. Did NOT work(I mean, the event gets called on refresh of a webpage). I did try the "persistent": true option but in vain.
Can I know what I am missing?
Nikhil
As per your comments, you are trying to register a listener for when Chrome starts and loads your extension. You can achieve this with the chrome.runtime.onStartup event:
Fired when a profile that has this extension installed first starts up. This event is not fired when an incognito profile is started, even if this extension is operating in 'split' incognito mode.
It is as simple as:
chrome.runtime.onStartup.addListener(function () {
/* Do some initialization */
});

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.

Extension only works when Chrome starts

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.

Resources