Przeglądaj źródła

【新增】添加自定义搜索引擎功能并优化 README 文档

- 新增自定义搜索引擎输入框,支持用户指定搜索服务
- 更新中英文 README,补充自定义搜索引擎使用说明
- 调整搜索链接生成逻辑,支持携带自定义搜索引擎参数
- 优化深色模式下输入框样式,提升视觉一致性
- 改进动画序列调用方式,传递搜索引擎 URL 参数
- 增强搜索引擎参数解析与校验机制,确保链接有效性
PJ568 1 rok temu
rodzic
commit
89637fd811
3 zmienionych plików z 119 dodań i 20 usunięć
  1. 5 4
      README.en.md
  2. 5 4
      README.md
  3. 109 12
      index.html

+ 5 - 4
README.en.md

@@ -11,10 +11,11 @@ Compared to a complaint, "Search It Yourself" is more like a vow spoken to onese
 ## Usage
 
 1. Visit `index.html`;
-2. Enter search content;
-3. Click the search button;
-4. Copy the generated link;
-5. Send the link to humans who are unwilling or unable to search themselves.
+2. (Optional) Enter custom search engine, format as `https://example.com/search?q=QUERY`;
+3. Enter search content;
+4. Click the search button;
+5. Copy the generated link;
+6. Send the link to humans who are unwilling or unable to search themselves.
 
 ## License
 

+ 5 - 4
README.md

@@ -11,10 +11,11 @@
 ## 使用方法
 
 1. 访问 `index.html`;
-2. 输入搜索内容;
-3. 点击搜索按钮;
-4. 复制生成的链接;
-5. 转发给不愿意或不会自己搜索的人类。
+2. (可选)输入自定义搜索引擎,格式为 `https://example.com/search?q=QUERY` ;
+3. 输入搜索内容;
+4. 点击搜索按钮;
+5. 复制生成的链接;
+6. 转发给不愿意或不会自己搜索的人类。
 
 ## 许可证
 

+ 109 - 12
index.html

@@ -268,6 +268,25 @@
           filter: drop-shadow(0 0 0.3rem black);
         }
       }
+
+      /* 自定义搜索引擎输入框样式 */
+      .search-engine-input {
+        box-sizing: border-box;
+        padding: 12px;
+        font-size: 16px;
+        border: none;
+        background: #f5f5f5;
+        width: 100%;
+      }
+
+      /* 深色模式下的自定义搜索引擎输入框样式 */
+      @media (prefers-color-scheme: dark) {
+        .search-engine-input {
+          background: #333333;
+          color: #f1f1f1;
+          border: 1px solid #555555;
+        }
+      }
     </style>
   </head>
   <body>
@@ -302,7 +321,7 @@
         </button>
       </form>
 
-      <!-- 搜索结果显示区域 -->
+      <!-- 搜索链接区域 -->
       <div class="search-result" id="searchResult" style="display: none">
         <div class="result-code-block" id="resultLink" title="点击复制链接">
           <!-- 搜索链接将在这里显示 -->
@@ -327,10 +346,18 @@
           <p>将生成的链接复制到剪贴板,发给不愿意或不会自己搜索的人类。</p>
         </li>
       </ol>
+      <!-- 自定义搜索引擎输入框 -->
+      <input
+        type="text"
+        class="search-engine-input"
+        id="searchEngineInput"
+        placeholder="https://bing.com/search?q=QUERY"
+      />
       <a
         onclick="javascript:window.open('?q=Z2l0IHJlcG8gc2VhcmNoLWl0LXlvdXJzZWxmIHBqNTY4')"
-        >项目源码</a
       >
+        项目源码
+      </a>
     </div>
 
     <div class="cursor">
@@ -421,11 +448,7 @@
 
         return isEnglish;
       }
-
-      // 初始化时执行语言检测
-      document.addEventListener("DOMContentLoaded", function () {
-        detectLanguage();
-      });
+      detectLanguage();
 
       // 获取页面元素
       const searchForm = document.getElementById("searchForm");
@@ -437,6 +460,8 @@
       const cursor = document.querySelector(".cursor");
       const searchButton = document.querySelector(".search-button");
 
+      const defaultSearchEngine = "https://bing.com/search?q=QUERY";
+
       // 光标动画函数
       function moveCursorTo(element, callback, x = 0, y = 0) {
         const rect = element.getBoundingClientRect();
@@ -481,7 +506,7 @@
       }
 
       // 动画序列控制器
-      function runAnimationSequence(queryValue) {
+      function runAnimationSequence(queryValue, searchEngineUrl) {
         // 显示光标
         cursor.classList.add("visible");
 
@@ -511,8 +536,13 @@
 
                             // 等待点击效果,然后跳转
                             setTimeout(() => {
-                              // 步骤 6: 跳转到bing搜索
-                              window.location.href = `https://bing.com/search?q=${encodeURIComponent(queryValue)}`;
+                              // 步骤 6: 跳转到自定义搜索引擎
+                              // 将 QUERY 替换为实际的搜索内容
+                              const searchUrl = searchEngineUrl.replace(
+                                "QUERY",
+                                encodeURIComponent(queryValue)
+                              );
+                              window.location.href = searchUrl;
                             }, 200);
                           });
                         },
@@ -534,6 +564,7 @@
       function initializePage() {
         const urlParams = new URLSearchParams(window.location.search);
         let encodedQueryValue = urlParams.get("q");
+        let encodedSearchEngineValue = urlParams.get("se");
 
         // 对获取到的编码值进行 Base64 解码
         let queryValue = "";
@@ -546,6 +577,32 @@
           }
         }
 
+        // 对获取到的自定义搜索引擎值进行 Base64 解码
+        let searchEngineValue = ""; // 格式形似:https://example.com/search?q=QUERY
+        if (encodedSearchEngineValue) {
+          try {
+            searchEngineValue = decodeURIComponent(
+              escape(atob(encodedSearchEngineValue))
+            );
+          } catch (error) {
+            // 如果解码失败,使用默认值
+            searchEngineValue = defaultSearchEngine;
+          }
+        }
+
+        // 如果没有提供自定义搜索引擎,或自定义搜索引擎没有“QUERY”,使用默认的 Bing 搜索
+        if (
+          !searchEngineValue ||
+          searchEngineValue.trim() === "" ||
+          !searchEngineValue.includes("QUERY")
+        ) {
+          searchEngineValue = defaultSearchEngine;
+        } else {
+          // 设置自定义搜索引擎输入框的占位符值为
+          document.getElementById("searchEngineInput").placeholder =
+            searchEngineValue;
+        }
+
         // 如果 q 参数无或空,显示 .list-div
         if (!queryValue || queryValue.trim() === "") {
           listDiv.style.display = "block";
@@ -627,7 +684,7 @@
           );
 
           // 开始动画序列
-          runAnimationSequence(queryValue);
+          runAnimationSequence(queryValue, searchEngineValue);
         }
       }
       initializePage();
@@ -647,9 +704,49 @@
         // 生成搜索链接,对搜索值进行 Base64 编码混淆
         const currentUrl = window.location.href.split("?")[0]; // 获取当前页面路径,去除查询参数
         const obfuscatedValue = btoa(unescape(encodeURIComponent(searchValue)));
-        const searchLink = `${currentUrl}?q=${encodeURIComponent(
+
+        // 获取当前页面输入框中自定义搜索引擎值(优先级高于当前 URL 中的 se 参数)
+        const currentSearchEngine = document
+          .getElementById("searchEngineInput")
+          .value.trim();
+
+        // 获取当前 URL 中的 se 参数(如果有)
+        const urlParams = new URLSearchParams(window.location.search);
+        const paramSearchEngine = urlParams.get("se");
+
+        // 验证自定义搜索引擎格式,确保包含 QUERY 占位符
+        let validSearchEngine = "";
+        if (currentSearchEngine && currentSearchEngine.includes("QUERY")) {
+          validSearchEngine = currentSearchEngine;
+        }
+        // 检查 URL 参数中的搜索引擎(需要先解码再验证)
+        else if (paramSearchEngine) {
+          let decodedSearchEngine = "";
+          try {
+            // 尝试 Base64 解码
+            decodedSearchEngine = decodeURIComponent(
+              escape(atob(paramSearchEngine))
+            );
+          } catch (error) {
+            // 如果解码失败,使用默认值
+            decodedSearchEngine = defaultSearchEngine;
+          }
+
+          // 验证解码后的搜索引擎是否包含 QUERY
+          if (decodedSearchEngine.includes("QUERY")) {
+            validSearchEngine = decodedSearchEngine;
+          }
+        }
+
+        // 构建搜索链接,包含原有的搜索引擎参数
+        let searchLink = `${currentUrl}?q=${encodeURIComponent(
           obfuscatedValue
         )}`;
+        if (validSearchEngine) {
+          searchLink += `&se=${btoa(
+            unescape(encodeURIComponent(validSearchEngine))
+          )}`;
+        }
 
         // 在代码块中显示链接
         resultLink.textContent = searchLink;