Use different icon for dev vs prod chrome extension - google-chrome-extension

I have a chrome extension, and I'd like to use a different icon for my dev version since right now both my dev and prod versions have the same icon so it can be confusing to tell which is which when they're both in my chrome extensions bar since only the icons show up there.
Is there a way in the extension manifest.json to specify different icons based on dev vs prod, or is there a way to use an entirely different manifest.json itself for dev vs prod?

That's not possible, the manifest.json has a fixed format. What I do right now is solving this issue through a build script, e.g. with gulp:
const replace = require('gulp-replace')
const clean = require('gulp-clean')
gulp.task('dev:copy', function () {
return gulp.src('build/**').pipe(gulp.dest('build-dev/'))
})
// Below the manifest is loaded and the name is replaced as well as the icon is replaced with "icon-dev"
gulp.task('dev:manifest', series('dev:copy', function () {
return gulp.src('build-dev/manifest.json')
.pipe(clean())
.pipe(replace(/"name": "([^"]*)"/g, '"name": "$1 (dev-channel)"'))
.pipe(replace(/"(default_icon|16|48|128)": "([^_]*)([^"]*)"/g, '"$1": "$2-dev$3"'))
.pipe(gulp.dest('build-dev/'))
}))
The result looks looks like this:
...
"icons": {
"16": "icons/icon-dev_16.png",
"48": "icons/icon-dev_48.png",
"128": "icons/icon-dev_128.png"
},
"browser_action": {
"default_icon": "icons/icon-dev_16.png",
...
},
...

Related

How do you trigger a chrome extension via a keyboard shortcut?

I wrote a short chrome extension that works as intended when I click on it on my extension list.
Ie. my code runs onClicked (chrome.action.onClicked.addListener(...))
I would like to add a keyboard shortcut (eg. Ctrl+Shift+Q) but not sure how.
Any ideas?
For context: I don't really know javascript, I'm hacking this together as a helper for myself in my day to day work.
You can use a command.
First step is to add the command to the manifest.json file. I added an example down below it should look very similar to your, but the part you really need is the "commands": {} section. This tells chrome to pay attention to when a user hits "Ctrl+Shift+Q". Note you can add multiple commands, like in the example below.
{
"name": "Getting Started Example",
"description": "Build an Extension!",
"version": "1.0",
"manifest_version": 3,
"background": {
"service_worker": "background.js"
},
"devtools_page": "devtools.html",
"commands": {
"name-of-command-passed-into-function": {
"suggested_key": "Ctrl+Shift+Q",
"description": "do somthing"
}
"shortcut2": {
"suggested_key": "Ctrl+Shift+A",
"description": "do somthing else"
}
},
"options_page": "options.html",
"permissions": ["storage", "activeTab", "scripting","tabs"],
}
Now when the user hits "Ctrl+Shift+Q" or "Ctrl+Shift+A", chrome will loos for some very specific code to run:
chrome.commands.onCommand.addListener((command,tab) => {
//do stuff here
});
Add this code and change the //do stuff here, to the code you want to run. The command property will simply be a string that matches what was types into the manifest, "name-of-command-passed-into-function" or "shortcut2". This is how you tell which command shortcut the user entered, but if you only need one, then delete the second one from the manifest file.

Migrating Extension to Manifest v3

I want to Migrate My Chrome extension from manifest version 2 to version 3 because in near future Google will remove MV2 extension from their store. For now My extension Manifest code is like this.
{
"browser_action": {
"default_icon": "img/icon_active.png",
"default_popup": "html/popup.html",
"default_title": "Title here"
},
"description": "description here",
"icons": {
"128": "img/icon_128.png",
"16": "img/icon_16.png"
},
"manifest_version": 2,
"name": "Title here",
"version": "1.0.1"
}
popup.js file look like this
$(document).on("click", ".copy-me", function(ev) {
var $body = document.getElementsByTagName('body')[0];
var rel = $(this).attr("rel");
var text = $("#"+rel).text();
var $tempInput = document.createElement("INPUT");
$body.appendChild($tempInput);
$tempInput.setAttribute("value", text)
$tempInput.select();
document.execCommand("copy");
$body.removeChild($tempInput);
});
Chrome Manifest v2 extensions deprecation timeline
On January 17, 2022: Chrome Web Store no longer accepts new Manifest V2 extensions, however, developers will be allowed to push updates to them.
In January 2023: Manifest V2 extensions will stop working and won’t run in Chrome, developers may not be able to push updates to them even with enterprise policy.
Offical Resource
I suggest you read the Google Chrome Offical Migration Article.
If you don't want to spend your time on this, I suggest Extension Manifest Converter which is a tool that open-source and developed by Google.
According to the README file, the tool has limitations:
This tool aims to simplify the MV3 conversion; it does not fully automate the process. Only search and replace changes are applied to .js files.
This tool does not:
update any service worker code that relies on a DOM
I tried the tool and it gives me the below output:
{
"description": "description here",
"icons": {
"128": "img/icon_128.png",
"16": "img/icon_16.png"
},
"manifest_version": 3,
"name": "Title here",
"version": "1.0.1",
"action": {
"default_icon": "img/icon_active.png",
"default_popup": "html/popup.html",
"default_title": "Title here"
},
"content_security_policy": {}
}
In your case manifest.json changed but nothing changed in popup.js.

Chrome extension simple script not being injected

I'm trying my first steps in writing a minimal Chrome extension, and I cannot figure out why it does not execute my clientScript.js.
This is my manifest.json:
{
"name": "Sit back, relax and enjoy",
"version": "0.1",
"description": "Finds and clicks the +extra channel points button when it is available",
"permissions": [ "activeTab" ],
"content_scripts": [
{
"matches": [ "https://twitch.tv/*" ],
"js": [ "contentScript.js" ],
"run_at": "document_idle"
}
],
"manifest_version": 2
}
And this is the the script that I want executed on pages that match https://twitch.tv/*:
let intervalTimer
function sibareaen() {
const btn = document.querySelector('.tw-button.tw-button--success.tw-interactive')
if (btn) {
btn.click()
console.log('At your service - clicked the button for you!')
}
}
function toggleSibareaen(on) {
switch (on) {
case true:
intervalTimer = setInterval(sibareaen, 750)
break
case false:
clearInterval(intervalTimer)
break
default:
clearInterval(intervalTimer)
}
}
console.log('At your service - ready to click for you!')
toggleSibareaen(true)
I have both files in the same folder:
Also, I have properly "installed" the extension:
The console shows no errors related to the extension.
What am I missing?
Assuming you did reload the tab (the content scripts are injected only on initial tab load, see this for more info and a workaround), you're probably a victim of the infamous Google's decision to hide www in the address bar: if you enter the edit mode and select/copy the entire text (or simply double-click the address) you will see the site's URL is actually https://www.twitch.tv/ so your manifest.json has an incorrect URL pattern that doesn't match the real site.
Simply use a generalized pattern like "https://*.twitch.tv/*" that will match both https://www.twitch.tv/ and https://twitch.tv/ and any other subdomain(s).
P.S. as a debugging step, you can check if the content script is even injected by looking at the context selector in devtools console toolbar or in devtools -> Sources -> Content scripts panel. And if you want to restore the classic address bar behavior, see https://superuser.com/a/1498561

How do I specify an Chrome Extension icon (popup icon) for Retina Mac's

I found my icon (and many other icons) very ugly on Retina Macs.
Is there any way that I can specify a well-designed retina image as the popup icon?
Thanks!
Just create double-sized icon (76x76 pixels) and provide path to it in manifest.json:
"browser_action": {
"default_icon": {
"19": "icon19x19.png",
"38": "icon76x76.png"
}
}
If you want to dynamically change icon via setIcon, then you must call it with following params:
chrome.browserAction.setIcon({
path: {
"19": "icon19x19.png",
"38": "icon76x76.png"
}
});

cant get simple chrome extension sample to work, the "makes page red" one

At
http://developer.chrome.com/extensions/samples.html 2nd sample titled:
"A browser action with no icon that makes the page red"
I downloaded the 3 files (manifest, js, icon) and installed the extension sample.
When I run it I get a puzzle icon on the bar with a badge that increments every second.
When I click on the icon - nothing
When I click on the page - nothing
I also cannot see the supplied icon being used anywhere.
I don't know if this is a code issue or a usage issue.
manifest.json:
{
"name": "A browser action with no icon that makes the page red",
"version": "1.0",
"background": { "scripts": ["background.js"] },
"permissions": [
"tabs", "http://*/*"
],
"browser_action": {
"name": "Make this page red",
"icons": ["icon.png"]
},
"manifest_version": 2
}
background.js file
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(
null, {code:"document.body.style.background='red !important'"});
});
chrome.browserAction.setBadgeBackgroundColor({color:[0, 200, 0, 100]});
var i = 0;
window.setInterval(function() {
chrome.browserAction.setBadgeText({text:String(i)});
i++;
}, 10);
This is a duplicate of How to make Google Chrome extension sample work?
Please see my answer below:
If you open up the sample zip... find backgrond.js... edit. Find the line that says:
null, {code:"document.body.style.background='red !important'"});
and remove the "!important". so it should read:
null, {code:"document.body.style.background='red'"});
That is it. just save and reload the extension, should work (unless the page has an !important flag set to the background).
I am afraid I don't know why the "!important" tag doesn't work but I have never been able to get it to work in an extension. Hopefully someone else here will be able to give an explanation and maybe a work around.

Resources