Last active
April 26, 2018 22:55
-
-
Save omarstreak/72d9acf3f9e684067b7ec11353391fcf to your computer and use it in GitHub Desktop.
Check is Streak installed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var INSTALL_CHECK = 'chrome-extension://pnnfemgpilpdaojpnkjdgfgbnnjojfik/blank.png'; | |
function isExtensionInstalled() { | |
//we have different logic for Chrome vs Safari | |
if(BrowserDetect.browser === 'Chrome'){ | |
// the Chrome extension makes an image "web accessible", so we have the page try to load that image. | |
// if it loads successfully then the Streak extension is installed. If the image does not load successfully, then the image is not installed | |
var img = new Image(); | |
img.onload = function() { | |
clearInterval(installTimerId); | |
streakIsInstalled(); | |
}; | |
img.onerror = function(){ | |
}; | |
try { | |
img.src = INSTALL_CHECK; | |
} | |
catch (error) { | |
} | |
} | |
else{ | |
// for Safari, we have our Safari extension add an element to the page with id "streakExists" when going to a | |
// streak page. So we have our script look for that element. If we can find it then we're good | |
if( document.getElementById('streakExists')){ | |
clearInterval(installTimerId); | |
streakIsInstalled(); | |
} | |
} | |
} | |
// we poll because sometimes it takes time for the marker element to get added | |
function intervalCheckInstalled(max){ | |
var current = 0; | |
var delay = 500; | |
installTimerId = setInterval(function(){ | |
current += delay; | |
if(current > max){ | |
clearInterval(installTimerId); | |
streakIsNotInstalled(); | |
} | |
isExtensionInstalled(); | |
}, delay); | |
isExtensionInstalled(); | |
} | |
// our current homepage checks for 2 seconds | |
intervalCheckInstalled(2000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment