自动搜索微软积分-加了按钮

自用微软积分搜索脚本,国际版与国内版通用。在原版本上添加了按钮,点击按钮后执行脚本。解决了原版本打开必应自动运行,导致必应不可用的情况。同时添加了获取百度热搜关键词取代原版的随机字符串关键词,更像人类搜索(需要跨域访问,第一次需要允许该域名,百度域名,安全可靠)。

// ==UserScript==
// @name         自动搜索微软积分-加了按钮
// @namespace    http://tampermonkey.net/
// @version      1.8
// @description  自用微软积分搜索脚本,国际版与国内版通用。在原版本上添加了按钮,点击按钮后执行脚本。解决了原版本打开必应自动运行,导致必应不可用的情况。同时添加了获取百度热搜关键词取代原版的随机字符串关键词,更像人类搜索(需要跨域访问,第一次需要允许该域名,百度域名,安全可靠)。
// @author       liulliu(原版作者青鸟丹心GitHubwanqq)
// @match        https://*.bing.com/*
// @grant        GM_xmlhttpRequest
// @grant        GM_notification
// @grant        GM_openInTab
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_deleteValue
// @license      MIT
// ==/UserScript==

// 设置API URL
const apiUrl = "https://top.baidu.com/api/board?platform=wise&tab=realtime";

// 定义一个变量来存储API响应数据
var responseData = null;

(function() {
    'use strict';

    GM_xmlhttpRequest({
        method: "GET",
        url: apiUrl,
        onload: function(response) {
            // 在这里处理响应数据
            responseData = response.responseText;
            GM_setValue('responseData', responseData);
            //console.log(GM_getValue('responseData', ""));
        }
    });
})();

// try {
//     console.log(GM_getValue('responseData', ""));
//     JSON.parse(GM_getValue('responseData', ""));
// } catch (error) {
//     console.error("无法解析JSON数据: " + error);
// }

//const randomWord = getRandomWordFromJSON(responseData);
//console.log("随机获取的word值: " + randomWord);


const searchUrl = 'https://www.bing.com/search';
const isDesktop = window.matchMedia("(min-width: 768px)").matches;
//start();
// 创建按钮
let Container = document.createElement('div');
Container.id = "sp-ac-container";
Container.style.position = "fixed";
Container.style.left = "90px";
Container.style.top = "21px";
Container.style['z-index'] = "999999";
Container.innerHTML = `<button id="myCustomize" style="position:absolute; left:30px; top:25px;background-color: red; color: white;">执行</button>`;
document.body.appendChild(Container);

// 获取按钮元素
const executeButton = document.getElementById('myCustomize');
//debugger;
if (typeof GM_getValue('ifstart') !== "undefined") {
    // 变量 ifstart 已经定义
    // 执行你的代码
    //showNotification("提示", "已经点击了执行按钮");
    start();
} else {
    // 变量 ifstart 未定义
    // 执行其他操作
    showNotification("提示", "未点击执行按钮");

    //return;
}

//let ifstart = GM_getValue('ifstart', 1);
// 添加点击事件监听器
executeButton.addEventListener('click', function () {
    GM_setValue('ifstart', 1); // 从存储器中获取计数器初始值let ifstart_1 =
    // 启动

    start();
});


function start(){
    // Check if the current URL is bing.com or cn.bing.com
    if (window.location.hostname.endsWith('.bing.com')) {
        // If it is, execute the random search every 2000 milliseconds (2 seconds)
        const loopCount = isDesktop ? 50 : 30; // 根据设备类型设置循环次数50 : 30;
        let count = GM_getValue('searchCount', 0); // 从存储器中获取计数器初始值

        const intervalId = setInterval(function() {
            if (count >= loopCount) {
                clearInterval(intervalId); // 停止循环
                GM_deleteValue('searchCount'); // 删除计数器的值
                GM_deleteValue('ifstart'); // 删除计数器的值
                window.close(); // 关闭 Bing 页面
                return;
            }
            // Generate a random search query
            //const search = generateRandomSearch();
            const search0 = getRandomWordFromJSON();
            const search1 = generateRandomSearch();

            const search = search0 + search1;

            // Enter the search query into the Bing search bar
            const searchBox = document.getElementById('sb_form_q');
            searchBox.value = search;
            searchBox.dispatchEvent(new Event('input')); // 触发输入框的输入事件,以便提交表单

            // Submit the search
            const searchForm = document.getElementById('sb_form');
            searchForm.submit();

            // Increment the counter and update the value in storage
            count++;
            GM_setValue('searchCount', count);
        }, 2000);
    }
}

function getRandomWordFromJSON() {
    try {
        // 尝试解析JSON字符串
        let jsonString = GM_getValue('responseData', "");
        const jsonData = JSON.parse(jsonString);

        // 检查数据是否包含"content"属性
        if (jsonData && jsonData.data && jsonData.data.cards) {
            const contentArray = jsonData.data.cards[0].content; // 假设只需要第一个 "content" 中的数据

            if (contentArray && contentArray.length > 0) {
                // 从 "content" 数组中随机选择一个元素
                const randomIndex = Math.floor(Math.random() * contentArray.length);
                const randomWord = contentArray[randomIndex].word;

                return randomWord;
            }
        }
        return "JSON数据格式不正确或数据为空";
    } catch (error) {
        return "解析JSON数据时出错: " + error;
    }
}



function getRandomWordFromJSON1() {
    try {
        // 尝试解析JSON字符串
        //console.log(jsonString);
        let jsonString=GM_getValue('responseData', "");
        const jsonData = JSON.parse(jsonString);
        console.log(typeof jsonData)
        console.log(JSON.stringify(jsonString, null, 2));

        // 检查数据是否包含"word"属性
        if (jsonData && Array.isArray(jsonData.data) && jsonData.data.length > 0) {
            // 从数据中随机选择一个元素
            const randomIndex = Math.floor(Math.random() * jsonData.data.length);
            const randomWord = jsonData.data[randomIndex].word;

            return randomWord;
        } else {
            return "JSON数据格式不正确或数据为空";
        }
    } catch (error) {
        return "解析JSON数据时出错: " + error;
    }
}


function generateRandomSearch() {
    let search = '';
    // 生成一个包含4位数字和1个字母的随机字符串
    for (let i = 0; i < 4; i++) {
        search += Math.floor(Math.random() * 10);
    }
    search += String.fromCharCode(Math.floor(Math.random() * 26) + 65);
    return search;
}


// 通知函数
function showNotification(title, text) {
    GM_notification({
        title: title,// 通知标题
        text: text,// 通知文本
        timeout: 5000// 通知消失的时间(毫秒)
    });
}