Skip to content

Instantly share code, notes, and snippets.

@inkhsutesou
Created Jan 21, 2021
Embed
What would you like to do?
// ==UserScript==
// @name Google Docs SHIFT+D Delete
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Use SHIFT+D to delete files fast
// @author Inkh Su Tesou
// @match https://docs.google.com/document/*
// @grant none
// ==/UserScript==
(function() {
console.log( ` - Delete files with SHIFT+D` )
const simulateMouseEvent = function ( element, eventName ) {
const bbox = element.getBoundingClientRect(),
clientX = bbox.left + ( bbox.right - bbox.left ) / 2,
clientY = bbox.top + ( bbox.bottom - bbox.top ) / 2
element.dispatchEvent( new MouseEvent( eventName, {
view : window,
bubbles : true,
cancelable : true,
clientX,
clientY,
button : 0
} ) )
}
let target
let onkeypress = e => {
if ( e.which !== 68 || !e.shiftKey ) { return }
while ( !target.classList.contains( `docs-homescreen-list-item` ) ) { target = target.parentNode }
const parent = target
const menuToggle = target.querySelector( `[aria-label="More actions. Popup button."]` )
menuToggle.click()
let i = 0
console.log( parent )
Array.from( parent.querySelectorAll( `div` ) ).forEach( node => {
if ( node.innerHTML === `Remove` ) {
const toClick = node.parentNode.parentNode.parentNode.parentNode
console.log( node, toClick )
simulateMouseEvent ( toClick, `mousedown` )
simulateMouseEvent ( toClick, `mouseup` )
simulateMouseEvent ( toClick, `click` )
document.querySelector('[name="moveToTrash"]').click()
}
} )
}
let onmousemove = e => {
target = e.target
}
document.body.addEventListener( `mousemove`, onmousemove, false )
document.body.addEventListener( `keypress`, onkeypress, false )
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment