Skip to content

Instantly share code, notes, and snippets.

@robin-hartmann
Last active April 13, 2024 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robin-hartmann/135e43eeca63d3c8fa759681af8045ca to your computer and use it in GitHub Desktop.
Save robin-hartmann/135e43eeca63d3c8fa759681af8045ca to your computer and use it in GitHub Desktop.
UserScript to paste a transaction activity into Parqet
// ==UserScript==
// @name Paste Activity
// @namespace https://robin-hartmann.com/
// @version 1.0.3
// @description Paste Activity
// @author robin-hartmann
// @match https://app.parqet.com/*
// @grant none
// @noframes
// ==/UserScript==
'use strict';
(function () {
window.addEventListener('paste', (e) => {
let activity;
try {
const text = e.clipboardData.getData('text');
activity = JSON.parse(text);
} catch (error) {
return;
}
try {
e.preventDefault();
insertActivity(activity);
} catch (error) {
alert('error while inserting activity:\n' + error);
}
});
})();
async function insertActivity(activity) {
const clearSearchButton = document.querySelectorAll(
'div.justify-between > div > button'
)[0];
if (clearSearchButton) {
clearSearchButton.click();
await new Promise((resolve) => setTimeout(resolve, 200));
}
const searchInput = document.querySelector('#search_field');
searchInput.value = activity.shareName;
searchInput.focus();
searchInput.dispatchEvent(new Event('input'));
const searchResults = await waitForSearchResults();
searchResults[0].click();
const timeInput = document.querySelector('#timefield');
if (timeInput.parentElement.classList.contains('hidden')) {
const showTimeButton = document.querySelectorAll(
'button.base-toggle-wrapper'
)[0];
showTimeButton.click();
}
let feesInput = document.querySelector('#feesField');
if (!feesInput) {
const showFeesButton = document.querySelectorAll(
'button.base-toggle-wrapper'
)[2];
showFeesButton.click();
feesInput = document.querySelector('#feesField');
}
const calculateShareValueButton = document.querySelector(
'label[for="priceField"] button'
);
calculateShareValueButton.click();
const activityTypeSelect = document.querySelector('#activityTypeField');
const dateInput = document.querySelector('#datefield');
const shareCountInput = document.querySelector('#sharesField');
const totalValueInput = document.querySelector('#amountField');
const taxesInput = document.querySelector('#taxesField');
shareCountInput.value = activity.shareCount;
shareCountInput.dispatchEvent(new Event('input'));
totalValueInput.value =
activity.type === 'buy'
? (
Number.parseFloat(activity.totalValue) +
Number.parseFloat(activity.fees)
).toFixed(2)
: activity.totalValue;
totalValueInput.dispatchEvent(new Event('input'));
dateInput.value = activity.date;
dateInput.dispatchEvent(new Event('input'));
timeInput.value = activity.time;
timeInput.dispatchEvent(new Event('input'));
feesInput.value = activity.fees;
feesInput.dispatchEvent(new Event('input'));
taxesInput.value = activity.taxes;
taxesInput.dispatchEvent(new Event('input'));
activityTypeSelect.value = activity.type === 'buy' ? 'Buy' : 'Sell';
activityTypeSelect.dispatchEvent(new Event('change'));
}
async function waitForSearchResults() {
for (let i = 0; i < 15; i++) {
await new Promise((resolve) => setTimeout(resolve, 200));
let searchResults = document.querySelectorAll('li > div');
if (searchResults.length > 0) {
return searchResults;
}
}
return undefined;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment