Skip to content

Instantly share code, notes, and snippets.

@kakaroto
Last active September 11, 2019 08:38
Show Gist options
  • Select an option

  • Save kakaroto/3b6776da4bdaaf37a2289fbbc35b2569 to your computer and use it in GitHub Desktop.

Select an option

Save kakaroto/3b6776da4bdaaf37a2289fbbc35b2569 to your computer and use it in GitHub Desktop.
Import the current compendium elements into your Roll 20 campaign
/*
* Paste into the chrome dev console of your Roll 20 campaign.
* Select the compendium category to import (you can do a search or filter results)
* Enter 'importCurrentCompendium()' in the dev console to start importing compendiums into your handouts
* Don't touch the compendium tab until it finishes
* If you want to import spells, reload the page to make sure there are no other hidden or closed character sheets
* then open a single character sheet, the one where you want to import all your spells, then in the dev console
* enter the command 'importCurrentcompendium(2, true)' and let it import all the spells into that character.
*/
async function importCurrentCompendium(timeout_per_item=10, spells=false) {
var triggerDragAndDrop = function (elemDrag, elemDrop) {
// function for triggering mouse events
var fireMouseEvent = function (type, elem, centerX, centerY) {
var evt = document.createEvent('MouseEvents');
evt.initMouseEvent(type, true, true, window, 1, 1, 1, centerX, centerY, false, false, false, false, 0, elem);
evt.dataTransfer = {files: [], types: ["text"]}
elem.dispatchEvent(evt);
};
// calculate positions
var pos = elemDrag.getBoundingClientRect();
var center1X = Math.floor((pos.left + pos.right) / 2);
var center1Y = Math.floor((pos.top + pos.bottom) / 2);
pos = elemDrop.getBoundingClientRect();
var center2X = Math.floor((pos.left + pos.right) / 2);
var center2Y = Math.floor((pos.top + pos.bottom) / 2);
// mouse over dragged element and mousedown
fireMouseEvent('mousedown', elemDrag, center1X, center1Y);
fireMouseEvent('mousemove', elemDrop, center2X, center2Y);
fireMouseEvent('mouseup', elemDrag, center2X, center2Y);
};
var target = $("#finalcanvas")[0]
if (spells)
target = $(".sheet-compendium-drop-target")[0]
compendium_items = $(".compendium-item")
for (let i = 0; i < compendium_items.length; i++) {
console.log("Importing Compendium item ", i, "/", compendium_items.length);
triggerDragAndDrop(compendium_items[i], target)
await new Promise(resolve => setTimeout(resolve, timeout_per_item * 1000));
if (!spells) {
$(".ui-dialog-titlebar-close").trigger("click")
$(".ui-dialog").remove()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment