簡體   English   中英

每 30 分鍾搜索一次隨機的谷歌查詢

[英]Search random google query every 30 minutes

我希望每 30 分鍾搜索一次隨機的谷歌查詢(例如https://www.google.com/?q=OjRfujaM )。 我需要在沒有在新選項卡中打開它的情況下搜索它。

請解釋我如何做到這一點。

謝謝。

這是一段代碼,它將從一組搜索短語中隨機選擇,打開一個新窗口,然后在該窗口中對該短語進行 google 搜索,然后在一段時間內將該窗口更改為新的 google 搜索。 你沒有說你想如何選擇隨機搜索短語,所以我想出了一個方法來做到這一點,但你顯然可以插入你自己的方法。 這是帶有工作演示的代碼:

var searchWindow, interval;
var searchPhrases = [
    "california earthquake", "desean jackson", "noah", 
    "louisville basketball", "ultra music festival", "january jones",
    "stephen colbert", "taco bell breakfast", "college board",
    "zac efron", "johnny manziel", "miguel cabrera", "psych"
];
function getRandomSearchPhrase() {
    var rand = Math.floor(Math.random() * searchPhrases.length);
    return searchPhrases[rand];
}

function makeRandomGoogleSearchURL() {
    var search = encodeURI(getRandomSearchPhrase().replace(/\s/g, "+"));
    var url = "https://www.google.com?q=" + search + "#q=" + search;
    return url;
}

// create the interval portion to refresh the Google search
// use short time here for purposes of the demo
function startInterval() {
    if (interval) {
        window.clearInterval(interval);
    }
    interval = window.setInterval(function() {
        if (searchWindow && searchWindow.location) {
            // stop the interval if the window has been closed
            searchWindow.location = makeRandomGoogleSearchURL();
        } else {
            window.clearInterval(interval);
            searchWindow = null;
        }
    }, 5000);
}

// hook up the button to start it all
document.getElementById("start").addEventListener("click", function() {
    if (!searchWindow || !searchWindow.location) {
        searchWindow = window.open(makeRandomGoogleSearchURL(), "myGoogleSearch");
        startInterval();
    }
})

在此處查看工作演示: http : //jsfiddle.net/jfriend00/pgkY5/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM