Skip to content

Instantly share code, notes, and snippets.

@greglittlefield-wf
Last active October 12, 2019 07:42
Show Gist options
  • Save greglittlefield-wf/4211cdd154850d16bad3 to your computer and use it in GitHub Desktop.
Save greglittlefield-wf/4211cdd154850d16bad3 to your computer and use it in GitHub Desktop.
Chrome-inspect pages on adb-connected devices from the terminal
if [[ "$1" == "--help" ]]; then
echo "usage: $0 [-u page_url_substring] [-t page_title_substring]"
exit
fi
if ! builtin type -p chrome-cli &>/dev/null; then
echo chrome-cli is required to run this script.
echo You can use Homebrew to install it: "'brew install chrome-cli'"
exit 1
fi
page_url=''
page_title=''
while getopts ":u:t:" opt; do
case $opt in
u)
page_url=$OPTARG
;;
t)
page_title=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
echo -n Loading inspect window...
inspect_tab_id=$(chrome-cli open 'chrome://inspect' | perl -n -e'/^Id\:\ (.+)$/ && print $1')
while chrome-cli info -t "$inspect_tab_id" | grep -q 'Loading: Yes'; do
echo -n .
done
echo
if [[ ! -z $page_url || ! -z $page_title ]]; then
echo Clicking the \"inspect\" link...
sleep 0.5
escape_js_string() {
printf -v escaped '%q' "$1"
if [[ "$escaped" == "''" ]]; then
echo -n ""
else
echo -n "$escaped"
fi
escaped=""
}
js_click_inspect="(function(config) {
function \$(selector, el) {
return (arguments.length > 1 ? el : document).querySelector(selector);
}
function \$\$(selector, el) {
return Array.prototype.slice.call((arguments.length > 1 ? el : document).querySelectorAll(selector), 0);
}
function closest(el, selector) {
var currentEl = el;
do {
if (currentEl.matches(selector)) {
return currentEl;
}
currentEl = currentEl.parentNode;
} while (currentEl !== document && currentEl);
return null;
}
var InspectablePage = function(baseElement) {
if (! (baseElement instanceof HTMLElement)) {
throw new Error('Invalid baseElement: ' + baseElement);
}
this._baseElement = baseElement;
};
InspectablePage.withEl = function(el) {
return new InspectablePage(closest(el, '.properties-box'));
};
InspectablePage.withUrl = function(url) {
if (! url) {
return null;
}
var urlEls = \$\$('#devices-list .list.pages .row .url');
for (var i=0; i<urlEls.length; i++) {
if (urlEls[i].textContent.indexOf(url) !== -1) {
return InspectablePage.withEl(urlEls[i]);
}
}
return null;
};
InspectablePage.withTitle = function(title) {
if (! title) {
return null;
}
var titleEls = \$\$('#devices-list .list.pages .row .name');
for (var i=0; i<titleEls.length; i++) {
if (titleEls[i].textContent.indexOf(title) !== -1) {
return InspectablePage.withEl(titleEls[i]);
}
}
return null;
};
InspectablePage.prototype = {
inspect: function() {
if (! this._baseElement) {
return false;
}
var actionButtonEls = \$\$(':scope .action', this._baseElement);
for (var i=0; i<actionButtonEls.length; i++) {
if (actionButtonEls[i].textContent === 'inspect') {
actionButtonEls[i].click();
return true;
}
}
return false;
}
}
var inspectablePage = InspectablePage.withUrl(config.url) || InspectablePage.withTitle(config.title);
return (inspectablePage && inspectablePage.inspect() && 'todo: return a page id') || '';
})({
url: '"$(escape_js_string "$page_url")"',
title: '"$(escape_js_string "$page_title")"'
})"
# echo $js_click_inspect # echo js for debugging
full_page_id=$(chrome-cli execute "$js_click_inspect" -t "$inspect_tab_id")
if [[ ! -z "$full_page_id" ]]; then
echo Found inspectable target\! #: $full_page_id
chrome-cli close -t "$inspect_tab_id"
else
echo Inspectable target not found.
# Give them a chance to read the error.
sleep 1
fi
fi
# Focus Chrome
open -a "Google Chrome"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment