Skip to content

Instantly share code, notes, and snippets.

@evenfrost
Last active November 6, 2015 18:04
Show Gist options
  • Save evenfrost/7f75733fcd643646970f to your computer and use it in GitHub Desktop.
Save evenfrost/7f75733fcd643646970f to your computer and use it in GitHub Desktop.
Adds 'Export' button to work diary for easier reports creation
(() => {
'use strict';
let copy = () => {
let area = document.createElement('textarea');
let notificaitons = angular.element('*[ng-app]').injector().get("uiNotifications");
let titles = [];
let dateArray = new Date().toJSON().slice(0, 10).split('-');
let date = [dateArray[1], dateArray[2], dateArray[0]].join('/');
titles.push(date);
Array.from(document.querySelectorAll('.diary__memo')).forEach(memo => {
let trimmed = memo.textContent.trim();
let text = '- ' + trimmed;
if (trimmed && titles.indexOf(text) === -1) {
titles.push(text);
}
});
area.value = titles.join('\n');
document.body.appendChild(area);
area.style.position = 'absolute';
area.style.left = '-9999px';
area.select();
try {
document.execCommand('copy');
notificaitons.showSuccess('Workflow has been successfully copied to clipboard.');
} catch (err) {
notificaitons.showError(err);
}
document.body.removeChild(area);
};
let process = () => {
let parent = document.querySelector('.diary-box .media-header__info');
let button = document.createElement('button');
button.textContent = 'Export';
button.classList.add('small', 'success', 'button');
button.addEventListener('click', copy);
parent.appendChild(button);
};
let waitForElement = (selector, callback) => {
if (document.querySelector(selector)) {
callback();
} else {
setTimeout(() => {
waitForElement(selector, callback);
}, 50);
}
};
waitForElement('.diary-box .media-header__info', process);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment