Skip to content

Instantly share code, notes, and snippets.

@sompylasar
Created January 19, 2018 20:52
Show Gist options
  • Save sompylasar/2ab9852a32fee6f1db775dec10c11d1c to your computer and use it in GitHub Desktop.
Save sompylasar/2ab9852a32fee6f1db775dec10c11d1c to your computer and use it in GitHub Desktop.
Sync JIRA favicon with the icon for the currently opened project.
// Custom JavaScript for websites: https://chrome.google.com/webstore/detail/custom-javascript-for-web/poakhlngfciodnhlhhgnaaelnpjljija?hl=en
// NOTE(@sompylasar): Doesn't work for the default project icons because they are in SVG, and link rel "shortcut icon" doesn't seem to accept SVGs.
(function () {
var iconEl = window.document.querySelector('link[rel="shortcut icon"]');
if (!iconEl) { return; }
var defaultSrc = iconEl.getAttribute('href');
var observer;
var timer;
var update = function () {
var imageEl = window.document.querySelector('[src^="' + window.location.origin + '/secure/projectavatar?"]');
var imageSrc = (imageEl && imageEl.getAttribute('src'));
if (imageSrc) {
iconEl.setAttribute('href', imageSrc);
}
else {
var imageSpanEls = window.document.querySelectorAll('[role="img"][style]');
var imageSpanEl = [].slice.call(imageSpanEls).filter(function (el) {
return (el.style.backgroundImage.indexOf(window.location.origin + '/secure/projectavatar?') >= 0);
})[0];
var imageSpanSrc = (imageSpanEl && imageSpanEl.style.backgroundImage || '').replace(/^\s*url\(\s*["']?/, '').replace(/["']?\s*\)\s*$/, '');
if (imageSpanSrc) {
iconEl.setAttribute('href', imageSpanSrc);
}
else if (defaultSrc) {
iconEl.setAttribute('href', defaultSrc);
}
}
};
observer = new MutationObserver(function (mutations) {
clearTimeout(timer);
timer = setTimeout(update, 100);
});
observer.observe(window.document.body, {
attributes: true,
childList: true,
subtree: true
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment