74 lines
2.1 KiB
JavaScript
74 lines
2.1 KiB
JavaScript
/**
|
|
* This script auto-creates a note with correct filename in /content/notes
|
|
* It pries out the current active Firefox tab URL as webmention potential context target.
|
|
* Hooked up with Mac Shortcut CTRL+OPT+CMD+N !
|
|
* Depends on https://github.com/andikleen/lz4json to decode FF session content (npm lz4 didn't work)
|
|
*/
|
|
|
|
const spawn = require("child_process").spawn
|
|
const { writeFileSync, existsSync, mkdirSync } = require('fs');
|
|
const dayjs = require('dayjs');
|
|
|
|
// options:
|
|
const sublPath = "/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl"
|
|
const firefoxProfileDir = "/Users/wgroeneveld/Library/Application Support/Firefox/Profiles/vm6bu36u.default-release"
|
|
const notesDir = `${__dirname}/../content/notes`
|
|
|
|
const proc = spawn('lz4jsoncat', [
|
|
`${firefoxProfileDir}/sessionstore-backups/recovery.jsonlz4`
|
|
])
|
|
|
|
let output = '';
|
|
proc.stdout.on('data', (chunk) => {
|
|
output += chunk.toString();
|
|
})
|
|
|
|
function pryOutLastActiveTabFromDecodedJsonRecovery(output) {
|
|
const session = JSON.parse(output)
|
|
let lastAccessed = 0
|
|
let url = ""
|
|
|
|
session.windows.forEach(w => {
|
|
w.tabs.forEach(t => {
|
|
if(t.lastAccessed > lastAccessed) {
|
|
lastAccessed = t.lastAccessed
|
|
url = t.entries[t.entries.length - 1].url
|
|
}
|
|
})
|
|
})
|
|
return url
|
|
}
|
|
|
|
function ensureNoteDirectoriesArePresent() {
|
|
const date = dayjs()
|
|
const year = date.format("YYYY")
|
|
const month = date.format("MM")
|
|
const path = `${notesDir}/${year}/${month}`
|
|
|
|
if(!existsSync(`${notesDir}/${year}`)) mkdirSync(`${notesDir}/${year}`)
|
|
if(!existsSync(path)) mkdirSync(path)
|
|
return path
|
|
}
|
|
|
|
proc.on('exit', (code) => {
|
|
const url = pryOutLastActiveTabFromDecodedJsonRecovery(output)
|
|
const date = dayjs()
|
|
|
|
const path = ensureNoteDirectoriesArePresent()
|
|
const filename = `${date.format("DD")}h${date.format("HH")}m${date.format("mm")}s${date.format("ss")}`
|
|
const fullpath = `${path}/${filename}.md`
|
|
const mddata = `---
|
|
date: ${date.format("YYYY-MM-DDTHH:mm:ssZ")}
|
|
context: "${url}"
|
|
---
|
|
|
|
|
|
`
|
|
|
|
writeFileSync(fullpath, mddata, 'utf-8')
|
|
spawn(sublPath, [ fullpath ])
|
|
|
|
console.log(`Created note ${filename} with context ${url}.`)
|
|
});
|
|
|