Skip to content

Instantly share code, notes, and snippets.

@dlopes7
Created January 9, 2020 13:09
Show Gist options
  • Save dlopes7/93b348d7306c5dff6994d45ba7967c20 to your computer and use it in GitHub Desktop.
Save dlopes7/93b348d7306c5dff6994d45ba7967c20 to your computer and use it in GitHub Desktop.
let changeColor = document.getElementById('changeColor');
const bkg = chrome.extension.getBackgroundPage();
function logger(message, ...other) {
bkg.console.log(message, other);
}
function findElement(selector, foundCallBack) {
attempts = 0;
let checkExists = setInterval(() => {
attempts += 1;
chrome.tabs.executeScript({
code: `(function findElement() {
return document.querySelector("${selector}");
})()`
}, element => {
logger("Looking for selector", selector);
if (element[0]) {
logger("Found the element!");
foundCallBack(true);
clearInterval(checkExists);
}
if (attempts >= 10) {
logger("Did not find the element after 10 attempts, giving up");
foundCallBack(false);
clearInterval(checkExists);
}
});
}, 500);
}
changeColor.onclick = element => {
chrome.tabs.query({
active: true,
currentWindow: true
}, tabs => {
chrome.tabs.update(tabs[0].id, {
url: "https://eaa50379.sprint.dynatracelabs.com/#newhosts;gf=all;gtf=l_2_HOURS"
}, tab => {
logger("Tab updated! ", tab);
findElement("[uitestid='gwt-debug-dateLabel']", found => {
if (found) {
logger("Found the element! taking screenshot");
} else {
logger("Could not find the element, not taking screenshot");
}
});
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment