GoogleとScrapboxを同時検索するChrome拡張が壊れているので作りたい
- 作成
- 更新
- (1343日前)
- 公開範囲
- personal notes
- Google検索結果とScrapbox検索結果を
同時に 表示できるChrome拡張が あったが 壊れている ため、 作り直したい。 - 関連リンク:
開発メモ
2023-01-29 s 着手
要件整理:
- Google検索の
結果 ページの サイドバーに、 その クエリをScrapboxに 投げた ときの 検索結果も 表示したい - 検索クエリを
取得 - 取得した
クエリで 設定したScrapboxプロジェクトを 検索 - API例:
/scrapboxlab/api/pages/:projectname/search/query
- API例:
- 検索結果をGoogle検索結果の
サイドバーに 表示 - スタイリング
- 検索クエリを
実装
- ✅ 検索
クエリを 取得 - Chrome拡張機能の
作り方は と ほほのChrome拡張機能入門 - と をほほのWWW入門 参考に -
const searchQuery = new URL(window.location.href).searchParams.get("q");
- Chrome拡張機能の
- 取得した
検索クエリでScrapbox検索を 実行 - Chrome拡張でgoogle.comドメイン
からAPIアクセス しようと するとCORSに 引っかかるらしい - 関連
ノート: Scrapboxに おけるCORB・CORS制限 - programming-notes TamperMonkeyスクリプトで回避できそう ダメでしたJS側での解決策は 見つから ずpend
- 関連
- s わかった
- backgroundスクリプトに
するとCORS回避できた (なんでかわからん) - Chrome 拡張機能の
CORS エラーを 回避 (Manifest V3) - Qiita - ↑を
参考に したら 取得できた
- Chrome 拡張機能の
- backgroundスクリプトに
- Chrome拡張でgoogle.comドメイン
- https://github.com/satoooh/scrapbox-enhancer-for-google-search
- なんとか
できた - 所要時間10時間くらい
- なんとか
作りかけ(動作しないPrototype)
userscript.js サンプル
// ==UserScript==
// @name Scrapbox Enhancer for Google Search
// @namespace https://scrapbox.io
// @version 0.1
// @description Enhance your Google search results by seamlessly integrating relevant Scrapbox pages into your search results page.
// @author satoooh
// @match https://www.google.com/search?q=*
// @icon https://www.google.com/s2/favicons?sz=64&domain=self-development.info
// @grant none
// @license MIT
// ==/UserScript==
(function () {
"use strict";
// 検索クエリを取得
const searchQuery = new URL(window.location.href).searchParams.get("q");
console.log("searchQuery =", searchQuery);
// scrapbox 内を検索
function searchScrapbox(query, projectName) {
const url = `https://scrapbox.io/api/pages/${projectName}/search/query?q=${query}`;
console.log("url =", url);
const res = new Promise((resolve, reject) => {
fetch(url)
.then((response) => {
if (!response.ok) {
throw new Error(
`Failed to search Scrapbox: ${response.statusText}`
);
}
return response.json();
})
.then((json) => json.projects)
.then(resolve)
.catch(reject);
});
return res;
}
console.log(searchScrapbox(searchQuery, "satoooh"));
})();
2023-02-07
- Google検索すると
同時にScrapbox内も で検索してくれるChrome拡張 - 井戸端 取り上げて もらった。 アドバイスも もらえた。