How to keep Background Console on uninstall event Chrome extension - google-chrome-extension

As the title, when I remove extension on Development mode, the Background Console disapear immediately. It makes hard to debug.
I want to write an event, call to the server if user remove my extension.
Details my code:
manifest.json
"background": {
"scripts": ["bg.js"],
"persistent": false
},
"permissions": [ "management","storage","proxy", "*://*/*" ],
bg.js
chrome.management.onUninstalled.addListener(function (info) {
console.log('Uninstall event caught!');
// I write script here
});
Thanks for any help!

chrome.management.onUninstalled is only fired when other extension is uninstalled.
You could listen to that in another extension.
You can use chrome.runtime.setUninstallURL to do something in the server.

Related

Chrome extension create html page at the root

Currently my extension opens a web page at
chrome-extension://bpmcncockdffdifceihffkimpfbobjhn/dist/webpage.html
How do I make it so that the webpage.html is omitted, and that the following url:
chrome-extension://bpmcncockdffdifceihffkimpfbobjhn
Immediately points to the webpage?
Chrome extensions pages are treated as direct file URLs, not as server pages, so there is no default handler for directories like index.html.
Persistent background script
If your background script doesn't have "persistent": false in manifest.json you can use webRequest API to redirect such a fake URL into the real one:
manifest.json:
"permissions": ["webRequest"],
"background": {
"persistent": true,
"scripts": ["background.js"]
}
background.js:
chrome.webRequest.onBeforeRequest.addListener(e => {
return {redirectUrl: chrome.runtime.getURL('/dist/webpage.html')};
}, {
urls: [chrome.runtime.getURL('/')],
types: ['main_frame'],
}, ['blocking']);
Event page background script
If your background script has "persistent": false in manifest.json you can use webNavigation API to redirect an error page to the real page. The only nuisance is that there'll be a dummy item in the tab's history and the history back button will be active (but it won't work of course as it'll be redirected again).
manifest.json:
"permissions": ["webNavigation"],
"background": {
"persistent": false,
"scripts": ["background.js"]
}
background.js:
chrome.webNavigation.onErrorOccurred.addListener(({tabId}) => {
chrome.tabs.update(tabId, {url: '/dist/webpage.html'});
}, {
url: [{
urlEquals: chrome.runtime.getURL('/'),
}],
});
There is also declarativeWebRequest but only in Chrome beta/dev/Canary. And just for fun, it's possible to use webRequest API with nonpersistent event pages but it's hacky (you'll have to prevent unloading of the event page by opening a message connection from an iframe inside the background page) and defeats the purpose of event pages so I won't be showing it here.

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.

Google Chrome Extension Alert on Click

How do i display an Alert message instead of the pop up.
My goal is to launch a script when the user clicks the extensions icon. The pop up is unnecessary, in fact i would like to avoid letting the pop up to initiate all together. I want my extension to behave like the Gmail hyper link, but instead of opening a link, I want it to execute my script.
Take a look at the api.
You will want to remove the popup in your manifest and have a onClicked handler instead. It will look like this:
chrome.browserAction.onClicked.addListener(function(){
alert("stuff");
});
The important part is that you don't define a popup in your manifest as it prevents the event from firing.
Rather than a popup, I'm assuming you want to run a background script to listen for a browserAction event.
First, make sure you remove "default_popup": "popup.html" from your manifest.json. Then include the background script in "background".
Your manifest json should be something like this:
"browser_action": {
"default_icon": "image.png",
"default_title": "My Extension"
},
"background": {
"scripts": ["background.js"]
},

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

Resources