iTranslated by AI
Built a Chrome Extension to Send a List of Created Scrapbox Pages to Slack
Since I didn't think I could get straight back into work mode right after the Golden Week holidays, I'm writing this as a bit of rehabilitation.
This is the first time I've created a Chrome extension. It also serves as part of my learning for Lambda, which I've recently started working with.
About the Chrome Extension Created This Time
Our team uses Scrapbox, and this Chrome extension is useful when you want to get a list of pages you've created, such as when you want to take a weekly summary of the pages you've made.
The main specification is a Chrome extension that, when you click the extension button on a Scrapbox page, sends a list of the pages you've recently created to Slack.
▼ Architecture Diagram

Preparation
About the Scrapbox API
The data to be sent to Slack this time targets the Scrapbox pages I have created.
The following Scrapbox API is used.
Page information within the project
Retrieves a list of pages in the project. Since I want the ones with the newest creation dates, I specify created in the sort parameter. (If the total number of pages is large, it might be better to specify a limit. In this case, it is set to a default of 500.)
/api/pages/:projectname?sort=created
My user information
To retrieve only the pages created by myself, I obtain the user ID to be used as a filter condition.
/api/users/me
Introducing Incoming Webhooks to a Slack Channel
To send to Slack, I use a Slack app.
Add an Incoming Webhook to the destination channel and generate a Webhook URL.
Creating a Lambda Function
Sending to Slack is performed from Lambda.
I'll also post the Lambda script just in case. Please take a look if you're interested.
const HTTPS = require('https');
const sendSlack = (message, url) => {
var options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
}
};
var req = HTTPS.request(url, options, (res) => {
if (res.statusCode === 200) {
console.log("OK:" + res.statusCode);
} else {
console.log("Status Error:" + res.statusCode);
}
});
req.on('error', (e) => {
console.error(e);
});
req.write(message);
req.end();
};
/**
* event.body.url : webhook url
* event.body.page : Page list
**/
exports.handler = (event, context, callback) => {
const body = JSON.parse(event.body)
const page = body.page
const url = body.url
let data = ''
page.map(v => {
data += `- <https://scrapbox.io/{Please enter project name}/${v.title}>\n`
})
var raw = JSON.stringify({
"username": "Scrapbox_to_Slack",
"text": data,
"icon_emoji": ":postbox:"
});
sendSlack(raw, url);
// CORS support
callback(null, {
"statusCode": 200,
headers: {
"Access-Control-Allow-Headers" : "Content-Type",
"Access-Control-Allow-Origin": "https://scrapbox.io",
"Access-Control-Allow-Methods": "OPTIONS,POST,GET"
},
"body": JSON.stringify(raw)
});
};
Creating the API Gateway
Create an API Gateway that will serve as the trigger for Lambda. The Chrome extension will request the API endpoint to execute the Lambda function.
Creating the Chrome Extension
Now for the main topic.
I will proceed with creating the Chrome extension.
A sample format file has been distributed, so I will create it based on this file.
File Structure
The structure and the purpose of the files used are as follows.
| File Name | Purpose |
|---|---|
background.js |
Processes that are always running. Describes the trigger conditions for the extension. |
chrome-icon-144.png |
Icon for the extension |
content.js |
Main process when the extension is executed |
manifest.json |
Configuration file |
options.html |
Options page for the extension (to set destination URL, search page limit, etc.) |
options.js |
Processes on the extension's options page |
Chrome Extension Configuration manifest.json
The manifest.json file describes various settings, such as the name, description, and version of the Chrome extension. Any Chrome extension functions to be used must be declared in this file in advance.
Creating the Extension Options Page options_page
I created options.html as the settings page for the Chrome extension.
By setting the created options.html in the options_page property of manifest.json, you can open it as an extension options page from the extension screen.
In this case, I made it possible to set the Slack Webhook URL for notifications, the Scrapbox project name, the upper limit for the number of pages to search, and the API Gateway endpoint to execute the Lambda.
Information set here is saved via the chrome.storage API.
Set storage in the permissions property of manifest.json, and prepare options.js to handle the saving process.
Chrome Extension Main Process content.js
Describes the processing when the Chrome extension is executed.
I prepared the content.js file to retrieve the page list with the Scrapbox API, filter it by the target user ID, and POST the page list via API Gateway.
Chrome Extension Trigger Conditions
manifest.json has a background property, which allows processes to run regardless of whether the extension is actively being used.
Here, I will describe the process to call the execution when the extension icon is clicked and ensure the extension only runs on Scrapbox pages.
I will set the target background.js file in the background property of manifest.json and describe the process.
With the above settings, it started working correctly, but the icon remained colored even when not on a Scrapbox page... I wonder why... (Maybe because I'm using background...)
Conclusion
That's it! The Chrome extension to send a list of created Scrapbox pages to Slack is complete!
I wish I could have implemented this process using Scrapbox's UserScript, but since it's designed so that third-party APIs cannot be executed, I met the requirements by using a Chrome extension to execute the Slack API via API Gateway.
The Chrome extension created this time is partially processed and published on GitHub, so please take a look if you want to check the full source code.
Discussion