Add Web Archive Button to Search Engines

Add a button to access web archive for each search result on search engine pages

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Add Web Archive Button to Search Engines
// @namespace    http://github.com/dreamking60
// @version      1.0
// @description  Add a button to access web archive for each search result on search engine pages
// @match        https://www.google.com/search*
// @match        https://www.bing.com/search*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    var searchEngine = determineSearchEngine();

    // 遍历每个搜索结果
    var searchResults = document.querySelectorAll(getSelectorForSearchEngine(searchEngine));
    searchResults.forEach(function(result) {
        // 创建一个新按钮
        var archiveButton = document.createElement('a');
        archiveButton.className = 'web-archive-button';
        archiveButton.textContent = 'Web Archive';
        archiveButton.href = 'http://web.archive.org/save/' + result.querySelector('a').href;
        archiveButton.target = '_blank';

        // 将按钮添加到搜索结果后面
        result.appendChild(archiveButton);
    });

    // 确定当前搜索引擎
    function determineSearchEngine() {
        if (window.location.hostname.includes('google.com')) {
            return 'google';
        } else if (window.location.hostname.includes('bing.com')) {
            return 'bing';
        } else {
            return 'unknown';
        }
    }

    // 获取不同搜索引擎的选择器
    function getSelectorForSearchEngine(engine) {
        switch (engine) {
            case 'google':
                return '.tF2Cxc'; // Google的选择器
            case 'bing':
                return '.b_algo'; // Bing的选择器
            default:
                return ''; // 默认选择器
        }
    }
})();