任意のprojectを横断検索するUserScript
code:script.js
export async function search(query, projects) {
// project idを取得する
let ids = await Promise.all(projects.map(async project => {
const res = await fetch(/api/projects/${project});
if (!res.ok) return;
const {id} = await res.json();
return id;
}));
自分が参加しているprojectsを除く
除かないとエラーになる
code:script.js
const joinedIds = await (async () => {
const res = await fetch('/api/projects');
if (!res.ok) return [];
const {projects} = await res.json();
return projects?.map?.(({id}) => id) ?? [];
})();
ids = ids.filter(id => id && !joinedIds.some(joinedId => joinedId === id));
code:script.js
// 横断検索する
const res = await fetch(/api/projects/search/watch-list?q=${encodeURIComponent(query)}${ids.flatMap(id => id ? [&ids=${id}] : []).join('')});
return await res.json();
}
test code
code:js
(async () =>
const {execute} = await import('/api/code/programming-notes/任意のprojectを横断検索するUserScript/test1.js');
console.log(await execute('scrapbox'));
})();
code:test1.js
import {search} from './script.js';
import {projects} from './project-list.js';
export const execute = (query) => search(query, projects);
code:project-list.js
export const projects = [
'hub',
'shokai',
'nishio',
'masui',
'rakusai',
'yuiseki',
'june29',
'ucdktr2016',
'villagepump',
'rashitamemo',
'thinkandcreateteck',
'customize',
'scrapboxlab',
'scrasobox',
'foldrr',
'scrapbox-drinkup',
'motoso',
'public-mrsekut',
'mrsekut-p',
'marshmallow-rm',
'wkpmm',
'sushitecture',
'nwtgck',
'dojineko',
'kadoyau',
'inteltank',
'sta',
'kn1cht',
'miyamonz',
'rmaruon',
'MISONLN41',
'yuta0801',
'choiyakiBox',
'choiyaki-hondana',
'spud-oimo',
'keroxp',
'aioilight',
'takker',
'programming-notes',
];