Skip to content

Instantly share code, notes, and snippets.

@shaqman
Created December 12, 2018 11:03
Show Gist options
  • Select an option

  • Save shaqman/8acd84f067e3c709768a5f513e42fc2b to your computer and use it in GitHub Desktop.

Select an option

Save shaqman/8acd84f067e3c709768a5f513e42fc2b to your computer and use it in GitHub Desktop.
UserScripts
// ==UserScript==
// @name Google Photo Plus
// @version 1.0
// @description Add various functionalities to google photo web interface
// @include http*://*photos.google*
// @namespace eto
// @run-at document-idle
// @grant none
// @require https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js
// ==/UserScript==
MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
var addToAlbumNode;
var addToAlbumParentContainer;
var albumName;
var step = 0;
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function simulateClick(elem) {
var rect = elem.getBoundingClientRect(), // holds all position- and size-properties of element
topEnter = rect.top,
leftEnter = rect.left, // coordinates of elements topLeft corner
topMid = topEnter + rect.height / 2,
leftMid = topEnter + rect.width / 2, // coordinates of elements center
ddelay = (rect.height + rect.width) * 2, // delay depends on elements size
ducInit = {bubbles: true, clientX: leftMid, clientY: topMid}, // create init object
// set up the four events, the first with enter-coordinates,
mover = new MouseEvent('mouseover', {bubbles: true, clientX: leftEnter, clientY: topEnter}),
// the other with center-coordinates
mdown = new MouseEvent('mousedown', ducInit),
mup = new MouseEvent('mouseup', ducInit),
mclick = new MouseEvent('click', ducInit);
// trigger mouseover = enter element at toLeft corner
elem.dispatchEvent(mover);
// trigger mousedown with delay to simulate move-time to center
window.setTimeout(function() {elem.dispatchEvent(mdown)}, ddelay);
// trigger mouseup and click with a bit longer delay
// to simulate time between pressing/releasing the button
window.setTimeout(function() {
elem.dispatchEvent(mup); elem.dispatchEvent(mclick);
}, ddelay * 1.2);
}
var observer = new MutationObserver(function(mutations, observer) {
// fired when a mutation occurs
//console.log(mutations, observer);
mutations.forEach(function(mutation) {
[].slice.call(mutation.addedNodes).forEach(async (addedNode) => {
if(step == 0) {
var result = $(addedNode).find("span:contains('Add to album')");
if(result.length > 0 && addToAlbumNode == null) {
addToAlbumNode = result;
addToAlbumParentContainer=result.parent().parent().parent();
$('<div><input id="eto-album-name" placeholder="Name" style="width: 89%;border: none;height: 3em;padding: 0.5em;margin-left: 0.5em;"></div>')
.insertBefore(addToAlbumParentContainer);
$('#eto-album-name').change(function(){
if($('#eto-album-name').val().length > 0) {
albumName = $('#eto-album-name').val();
console.log('Selected album name: ' + albumName);
step=1;
}
});
}
}
if(step == 1 && albumName != null) {
var result = $(addedNode).find("div:contains('New album')");
if(result.length > 0) {
console.log('Found new album button. Clicking...');
//result = $("div:contains('New album')");
simulateClick(result.closest('li')[0]);
step=2;
}
}
if(albumName != null) {
var result = $(addedNode).find("textarea[placeholder='Add a title']");
if(result.length > 0) {
result.html(albumName);
await sleep(3000);
simulateClick($("button[title='Done']")[0]);
step=0;
addToAlbumNode = null;
addToAlbumParentContainer = null;
albumName = null;
}
}
});
});
});
// define what element should be observed by the observer
// and what types of mutations trigger the callback
observer.observe(document, {
subtree: true,
childList: true,
});
@mikeymckay
Copy link

Hi @shaqman - I want a script that gives me a shortcut for adding a photo to an album. This looks like it almost does that... But what is the trigger to make this happen?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment