Javascript to change website favicon if specific text is present
I am trying to change the icon on the Nested tab of the Google Inbox tab. so I know which tab is at a glance.
I know there are probably extensions for this, but I'm learning Javascript and trying to go myself. I can change other icons using the "url.indexOf" command, but the Inbox page does not change the URL. So I figured I would write Javascript to find the word "Pinned" and then the Favicon change action if found. This is what I have:
favico = function() {
this.is_changed = 0;
this.retry = 1000;
this.timerFindButtons = null;
this.list = [
"foo",
"bar"
];
}
favico.prototype.init = function(){
setTimeout(this.looping.bind(this), this.retry);
}
favico.prototype.looping = function(){
var head = document.getElementsByTagName('head')[0];
var links = head.querySelectorAll('link');
var eles = document.querySelectorAll("div");
for(var ii=0;ii<eles.length;ii++){
var foo = eles[ii].getAttribute('jstcache');
if(foo != null && foo.indexOf('1683') != -1){
var bar = eles[ii].innerText;
if(bar.indexOf('Pinned') != -1){
links[0].setAttribute('href', 'data:image/x-icon;base64,...');
this.is_changed = 1;}
else {
this.is_changed = 0;
links[0].setAttribute('href', '//ssl.gstatic.com/bt/C3341AA7A1A076756462EE2E5CD71C11/ic_product_inbox_16dp_r2_2x.png');}
}
}
setTimeout(this.looping.bind(this), this.retry);
}
var oo = new favico()
oo.init();
What am I doing wrong? Many thanks to everyone who can help!
+3
source to share
1 answer
use this method to dynamically change favicon
(function() {
var link = document.createElement('link');
link.type = 'image/x-icon';
link.rel = 'shortcut icon';
link.href = 'http://www.stackoverflow.com/favicon.ico';
document.getElementsByTagName('head')[0].appendChild(link);
}());
Firefox should be cool.
+1
source to share