Skip to content

Instantly share code, notes, and snippets.

@CVertex
Created May 14, 2019 04:21
Show Gist options
  • Save CVertex/ea81936fa8ff6e67f8b795835d62255c to your computer and use it in GitHub Desktop.
Save CVertex/ea81936fa8ff6e67f8b795835d62255c to your computer and use it in GitHub Desktop.
/**
* https://stackoverflow.com/questions/37098405/javascript-queryselector-find-div-by-innertext
* https://developers.google.com/gsuite/add-ons/concepts/menus
* https://stackoverflow.com/questions/51848258/google-docs-programmatically-send-mouse-click-to-an-item-in-outline-pane
* https://stackoverflow.com/a/43331339/209
*
* */
var simulateMouseEvent = function(element, eventName, coordX, coordY) {
element.dispatchEvent(new MouseEvent(eventName, {
view: window,
bubbles: true,
cancelable: true,
clientX: coordX,
clientY: coordY,
button: 0
}));
};
var simulateClick = function(element) {
var box = element.getBoundingClientRect(),
coordX = box.left + (box.right - box.left) / 2,
coordY = box.top + (box.bottom - box.top) / 2;
simulateMouseEvent (element, "mousedown", coordX, coordY);
simulateMouseEvent (element, "mouseup", coordX, coordY);
simulateMouseEvent (element, "click", coordX, coordY);
}
var findItemContaining = function(selector, text) {
var elements = document.querySelectorAll(selector);
return Array.prototype.filter.call(elements, function(element){
return RegExp(text).test(element.textContent);
});
}
// Open addon
var addonMenu = document.getElementById('docs-extensions-menu')
simulateClick(addonMenu);
// Get the Addons menu root item
var acAddonItem = null;
var addonMenuItems = document.querySelector('[aria-label="Get add-ons... g"').parentElement.parentElement.parentElement;
addonMenuItems.querySelectorAll('.goog-menuitem').forEach((entry) => {
if (entry.innerText.startsWith('Copy of AC ustwo'))
acAddonItem = entry;
});
simulateClick(acAddonItem);
// AC Connect Addon sub menu is now open
// We should load the addon now
// Now look for the "Open Audience Connect" item
var openAcItem = findItemContaining('.goog-menuitem-content','Open Audience Connect')[0]
simulateClick(openAcItem);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment