Skip to content

Instantly share code, notes, and snippets.

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 subtleGradient/968cdec14f6a2e783b27f6dcd2e09166 to your computer and use it in GitHub Desktop.
Save subtleGradient/968cdec14f6a2e783b27f6dcd2e09166 to your computer and use it in GitHub Desktop.
bookmarklet = () =>
(async () => {
/*!
Copyright 2020 Thomas Aylott <oblivious@subtlegradient.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
async function download(fileName, data) {
const content = typeof data === 'string' || data instanceof Blob ? data : JSON.stringify(data);
const downloadURL = window.URL.createObjectURL(new Blob([content], { type: 'application/json' }));
const anchor = document.createElement('a');
anchor.download = fileName;
anchor.href = downloadURL;
await new Promise(resolve => setTimeout(resolve, 0));
anchor.dispatchEvent(new MouseEvent('click'));
}
async function getJSON() {
return [...document.querySelectorAll(`[data-sprint-id].ghx-backlog-container`)].map(sprint => {
const keyValues = {};
sprint.querySelectorAll('[data-fieldname][data-fieldvalue]').forEach(({ dataset: { fieldname, fieldvalue } }) => {
keyValues[fieldname] = fieldname.endsWith('Date') ? new Date(fieldvalue) : fieldvalue;
});
const issues = [...sprint.querySelectorAll(`[data-issue-key].ghx-issue-compact`)].map(issue => {
const { title: issueType } = issue.querySelector(`.ghx-type[title]`) || {};
const { title: priority } = issue.querySelector(`.ghx-priority[title]`) || {};
const { title: summary } = issue.querySelector(`.ghx-summary[title]`) || {};
const { title: id, href } = issue.querySelector(`a.js-key-link[href][title]`) || {};
const {
title: epic,
dataset: { epickey }
} = issue.querySelector(`[data-epickey][title]`) || { dataset: {} };
const { innerText: points } = issue.querySelector(`aui-badge.ghx-statistic-badge[title]`) || {};
return {
issueType,
priority,
summary,
id,
href,
epic,
epickey,
points: +points
};
});
return { name, ...keyValues, issues };
});
}
async function main() {
download(`${location.host} ${Date.now()}.json`, JSON.stringify(await getJSON()));
}
try {
await main();
} catch (e) {
alert(e);
throw e;
}
})();
BOOKMARKLET = `javascript:void (${encodeURIComponent(bookmarklet.toString())})()`;
prompt(
`Copy this and paste it into your console.
Then paste that as a new bookmark.
Press enter to execute now`,
`copy(BOOKMARKLET)`
) && bookmarklet();
// https://gist.github.com/subtleGradient/968cdec14f6a2e783b27f6dcd2e09166
bookmarklet = () =>
(async () => {
/*!
Copyright 2020 Thomas Aylott <oblivious@subtlegradient.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
async function download(fileName, data) {
const content = typeof data === 'string' || data instanceof Blob ? data : JSON.stringify(data);
const downloadURL = window.URL.createObjectURL(new Blob([content], { type: 'application/json' }));
const anchor = document.createElement('a');
anchor.download = fileName;
anchor.href = downloadURL;
await new Promise(resolve => setTimeout(resolve, 0));
anchor.dispatchEvent(new MouseEvent('click'));
}
function getData() {
return [...document.querySelectorAll(`[data-sprint-id].ghx-backlog-container`)].map(sprint => {
const keyValues = {};
sprint.querySelectorAll('[data-fieldname][data-fieldvalue]').forEach(({ dataset: { fieldname, fieldvalue } }) => {
keyValues[fieldname] = fieldname.endsWith('Date') ? new Date(fieldvalue) : fieldvalue;
});
const issues = [...sprint.querySelectorAll(`[data-issue-key].ghx-issue-compact`)].map(issue => {
const isDone = issue.classList.contains('ghx-done');
const { title: issueType } = issue.querySelector(`.ghx-type[title]`) || {};
const {
dataset: { tooltip: assignee }
} = issue.querySelector(`.ghx-avatar-img[data-tooltip]`) || { dataset: {} };
const { title: priority } = issue.querySelector(`.ghx-priority[title]`) || {};
const { title: summary } = issue.querySelector(`.ghx-summary[title]`) || {};
const { title: id, href } = issue.querySelector(`a.js-key-link[href][title]`) || {};
const {
title: epic,
dataset: { epickey }
} = issue.querySelector(`[data-epickey][title]`) || { dataset: {} };
const { innerText: points } = issue.querySelector(`aui-badge.ghx-statistic-badge[title]`) || {};
return {
isDone,
issueType,
priority,
summary,
id,
href,
epic: epic || 'Other',
epickey,
points: +points,
assignee: (assignee && assignee.split(': ').reverse()[0]) || 'unassigned'
};
});
return { name, ...keyValues, issues };
});
}
async function getJSON() {
return getData().map(({ sprintName, startDate, endDate, sprintGoal, issues }) => {
const epics = Object.keys(issues.reduce((epics, { epic }) => ({ ...epics, [epic || 'Other']: true }), {}));
return {
title: `${sprintName}`,
children: [
{ string: sprintGoal, heading: 1 },
...epics.map(epic => ({
string: `[[${epic}]]`,
children: [
...issues
.filter(issue => issue.epic === epic)
.map(({ id, isDone = false, epickey, epic, href, issueType, points, priority, summary, assignee }) => ({
string: `{{[[${isDone ? `DONE` : `TODO`}]]}} [[${issueType}]] [${id}](${href}) #[[${points}-points]] [[${priority}]] #[[${assignee}]]\n${summary}`
}))
]
}))
]
};
});
}
async function main() {
download(`${location.host} ${Date.now()}.roam.json`, JSON.stringify(await getJSON()));
}
try {
await main();
} catch (e) {
alert(e);
throw e;
}
})();
BOOKMARKLET = `javascript:void (${encodeURIComponent(bookmarklet.toString())})()`;
prompt(
`Copy this and paste it into your console.
Then paste that as a new bookmark.
Press enter to execute now`,
`copy(BOOKMARKLET)`
) && bookmarklet();
@subtleGradient
Copy link
Author

subtleGradient commented Feb 22, 2020

How do use one of these bookmarklets

  1. CRITICAL STEP
    Read the code and double check that it doesn't do anything dangerous
  2. View Raw
  3. Select all & copy
  4. Open your browser devtools
  5. Paste into the devtools console
  6. A prompt will open
  7. Copy the selected text copy(BOOKMARKLET) and then cancel out of the prompt
  8. Paste copy(BOOKMARKLET) into your devtools console and hit enter
  9. Create a new bookmark in your browser bookmarks list
  10. Edit the URL and paste what you copied in step #7
  11. Click the bookmark to execute the script

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