忘机山人
在之前的文章中,我们让 Hexo,hugo 博客 支持了 coco AI 检索,也就是说我们还得使用客户端来检索,那是不是把搜索放在博客上呢?
Coco-AI 在 0.3 的版本中

先找一个 html 来看个效果。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<title>搜索组件嵌入示例</title>
<style>
body {
font-family: sans-serif;
padding: 2rem;
}
#searchbox {
margin-top: 20px;
border: 1px solid #ccc;
padding: 16px;
border-radius: 8px;
}
</style>
</head>
<body>
<h1>欢迎使用 Cloudsmithy 搜索组件</h1>
<p>下面是通过 ES Module 引入的搜索框:</p>
<div id="searchbox"></div>
<script type="module">
import { searchbox } from "http://localhost:9000/integration/cvmhvjl92jog2dokvsd0/widget";
searchbox({
container: "#searchbox",
});
</script>
</body>
</html>
<div id="searchbox"></div>
<script type="module">
import { searchbox } from "http://localhost:9000/integration/cvmhvjl92jog2dokvsd0/widget";
searchbox({ container: "#searchbox" });
</script>
<div id="searchbox"></div>这是一个空的 div 元素,作为挂载容器。你的 searchbox 组件会被渲染进这个 div。
<div id="app"> 一样searchbox,用于后面初始化组件时绑定<script type="module">这表示:这是一个 ES Module 格式的 JavaScript 脚本。
import 加载其他模块、组件import { searchbox } from "http://localhost:9000/integration/cvmhvjl92jog2dokvsd0/widget";从远程地址(本地服务器)中导入 searchbox 函数:
说明这个 URL 返回的是一个 JavaScript 模块,里面导出了 searchbox
类似 Vue、React 中的组件库导入:
import { Button } from "element-plus";
searchbox({container: "#searchbox"});调用 searchbox 函数,初始化组件,并挂载到页面中:
container: "#searchbox" 通过 CSS 选择器指定了上面那个 div就像你在网页上写:
<div id="chat"></div>
<script type="module">
import { chatWidget } from "./chat.js";
chatWidget({ container: "#chat" });
</script>
它就是在某个 div 中挂载一个聊天插件一样。你这段代码的本质就是嵌入并运行一个远程提供的 JS 组件。
这段代码的作用是:
把远程加载的
searchbox组件挂载到网页上的<div id="searchbox">中,完成动态插入功能。
如果你希望我反向帮你写出那个 searchbox 模块怎么写,或者做成本地文件测试,也可以告诉我~
下面是一个可以在任意网站自动嵌入你给出的 <div> 和 <script> 的油猴(Tampermonkey)脚本示例:
<body> 插入一个 #searchbox 的 divsearchbox 组件一开始打算在 hexo 的组件上改,但是在编译阶段有问题,索性使用油猴脚本来实现
// ==UserScript==
// @name Searchbox Embedder
// @namespace http://tampermonkey.net/
// @version 0.1
// @description 在任意网页中注入 searchbox 小部件
// @author You
// @match *://*/*
// @grant none
// @run-at document-end
// ==/UserScript==
(function () {
"use strict";
// 创建 searchbox 容器
const searchboxDiv = document.createElement("div");
searchboxDiv.id = "searchbox";
document.body.appendChild(searchboxDiv);
// 动态加载模块脚本(ESM)
const script = document.createElement("script");
script.type = "module";
script.textContent = `
import { searchbox } from "http://localhost:9000/integration/cvmhvjl92jog2dokvsd0/widget";
searchbox({ container: "#searchbox" });
`;
document.body.appendChild(script);
})();
@match 改为具体的页面,例如:https://example.com/*这个油猴脚本的作用是:在任何网页上自动插入一个 div#searchbox 容器,并加载你提供的远程模块脚本,渲染搜索框组件。
// ==UserScript==
// @name Searchbox Embedder
@name 是脚本的名字,显示在 Tampermonkey 的控制面板中。// @namespace http://tampermonkey.net/
// @version 0.1
// @description 在任意网页中注入 searchbox 小部件
// @author You
// @match *://*/*
// @match https://example.com/*// @grant none
none。// @run-at document-end
DOMContentLoaded)。(function () {
'use strict';
// 创建 searchbox 容器
const searchboxDiv = document.createElement("div");
searchboxDiv.id = "searchbox";
document.body.appendChild(searchboxDiv);
<div id="searchbox"> 并插入到 <body> 中,作为挂载点。// 动态加载模块脚本(ESM)
const script = document.createElement("script");
script.type = "module";
<script type="module">,用来加载 ES 模块。script.textContent = `
import { searchbox } from "http://localhost:9000/integration/cvmhvjl92jog2dokvsd0/widget";
searchbox({ container: "#searchbox" });
`;
searchbox 组件searchbox({ container: "#searchbox" }) 初始化它 document.body.appendChild(script);
})();
<script type="module"> 插入到 <body> 中,触发模块加载和执行。这个脚本做了三件事:
div#searchbox#searchbox 上
评论
0暂无评论