Last active
July 5, 2022 15:43
-
-
Save ruter/c4d07a8f857cdd7e7ef051e97905db36 to your computer and use it in GitHub Desktop.
A script for Notion Palette Command to change current Notion page's title & icon as the date of today.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(async () => { | |
// The CORS Proxy use to fetch Notion API | |
// Ref. https://github.com/Rob--W/cors-anywhere#server | |
const CORS_PROXY = await getEnv("CORS_PROXY"); | |
// Your Notion Internal Integration Token | |
// Ref. https://www.notion.so/my-integrations | |
const NOTION_TOKEN = await getEnv("NOTION_TOKEN"); | |
const NOTION_API = CORS_PROXY + "https://api.notion.com/v1/pages/"; | |
const COLORS = ["red", "blue", "yellow", "green", "violet", "pink", "fuchsia", "grey"]; | |
let getDateStr = () => { | |
const options = { year: 'numeric', month: '2-digit', day: '2-digit' }; | |
return new Date().toLocaleDateString('sv-SE', options); | |
} | |
let getIconUrl = () => { | |
const color = COLORS[Math.floor((Math.random() * COLORS.length))]; | |
const today = getDateStr(); | |
let url = `https://api.wolai.com/v1/icon?type=2&locale=en&date=${today}&pro=0&color=${color}` | |
return url; | |
} | |
let getNotionPageId = () => { | |
return window.location.pathname.split('-').slice(-1)[0]; | |
} | |
const options = { | |
method: 'PATCH', | |
headers: { | |
Accept: 'application/json', | |
'Notion-Version': '2022-02-22', | |
'Content-Type': 'application/json', | |
Authorization: 'Bearer ' + NOTION_TOKEN, | |
}, | |
body: JSON.stringify({ | |
properties: { | |
title: { | |
title: [ | |
{ | |
type: "text", | |
text: { | |
content: getDateStr(), | |
link: null | |
}, | |
} | |
] | |
} | |
}, | |
icon: { | |
type: 'external', | |
external: { | |
url: getIconUrl() | |
} | |
} | |
}) | |
}; | |
fetch(NOTION_API + getNotionPageId(), options) | |
.then(response => response.json()) | |
.then(response => console.log(response)) | |
.catch(err => console.error(err)); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment