检查打开的浏览器窗口是否包含特定字符串

时间:2013-10-24 06:38:52

标签: javascript browser

假设我有一个可以同时打开多个窗口的网站。数字 打开的窗口未知,每个窗口中的文本都是未知的。 我该如何测试或确保至少以下字符串(苹果,香蕉, 胡萝卜,日期)包含在一组窗口中。

请记住,每页只能有一个字符串,例如:

  • 窗口1 - 胡萝卜
  • Window 2 - Apple
  • 窗口5 - 日期
  • Window 10 - Banana

3 个答案:

答案 0 :(得分:0)

假设您的五个浏览器有五个地址。您可以使用URLConnection获取网页内容,而不是搜索该内容的特定字词。使用浏览器地址循环跟随代码。

    String[] strings = {"apple", "banana", "carrot", "date"};
    URL oracle = new URL("http://www.oracle.com/");
    URLConnection yc = oracle.openConnection();
    BufferedReader in = new BufferedReader(new InputStreamReader(
            yc.getInputStream()));
    StringBuilder output = new StringBuilder();
    String inputLine;
    while ((inputLine = in.readLine()) != null) {
        output.append(inputLine);
    }
    for (String token : strings) {
       if(output.indexOf(token) != -1){
           System.out.println(token +" is found at "+ output.indexOf(token));
       }
    }

答案 1 :(得分:0)

我写了一些fiddle来证明你如何能够做到这一点。我依赖于jQuery和浏览器localStorage,因此该解决方案不适用于古老的浏览器。将代码粘贴在

$(document).ready

并测试

localStorage.allHaves

位域以查看每个窗口还有其他人(代码可以共享)。记得做一个

parseInt
得到它后,在 of localStorage.allHaves值上

答案 2 :(得分:0)

使用jquery的一个快速解决方案,向您展示您需要的所有主要功能,

<强> JS

$(document).ready(function () {
    var searchForWords = [['Carrot',false],['Apple',false],['Date',false],['Banana',false]]


    var windows=[];
    windows.push(openWindow(1));
    windows.push(openWindow(2));
    windows.push(openWindow(3));
    windows.push(openWindow(4));

    windows.forEach(function(w){
        searchForWords.forEach(function(word){
            if($(w.document).find('body').html().indexOf(word[0])!=-1){
                console.log('found word'+word[0]);
                word[1]=true;
            }
        });
    });

    var allFound = true;
    searchForWords.forEach(function(word){
        if(!word[1]){
            allFound=false;
            return;
        }
    });

    if(allFound){
        alert('all words found');
    }else{
        alert('did not find all words!');
    }


});

function openWindow(wId) {
    var w = window.open();
    w.document.write('<html><head><title>test</title>');
    w.document.write($("#test" + wId).html());
    w.document.write('</body></html>');
    w.document.close();
    return w;
}

<强> HTML

<input type="button" value="check content"></input><!-- this button is used in 2nd version, http://jsfiddle.net/xtxK9/1/ -->


<div id="test1" class="test-content">this is content for the test window1 Carrot</div>
<div id="test2" class="test-content">this is content for the test window2 Apple</div>
<div id="test3" class="test-content">this is content for the test window3 Date</div>
<div id="test4" class="test-content">this is content for the test window4 Banana</div>

<强> CSS

.test-content {
    display:none;
}

http://jsfiddle.net/xtxK9/

点击按钮时调用“checkContent”函数的另一个版本可以在这里找到,

<强> JS

$(document).ready(function () {
    var searchForWords = [['Carrot',false],['Apple',false],['Date',false],['Banana',false]]


    var windows=[];
    windows.push(openWindow(1));
    windows.push(openWindow(2));
    windows.push(openWindow(3));
    windows.push(openWindow(4));

    $('#check-button').click(function(){checkContent(searchForWords,windows);});
});

function checkContent(searchForWords,openWindows){
    openWindows.forEach(function(w){
        searchForWords.forEach(function(word){
            if($(w.document).find('body').html().indexOf(word[0])!=-1){
                console.log('found word'+word[0]);
                word[1]=true;
            }
        });
    });

    var allFound = true;
    searchForWords.forEach(function(word){
        if(!word[1]){
            allFound=false;
            return;
        }
    });

    if(allFound){
        alert('all words found');
    }else{
        alert('did not find all words!');
    }
};

function openWindow(wId) {
    var w = window.open();
    w.document.write('<html><head><title>test</title>');
    w.document.write($("#test" + wId).html());
    w.document.write('</body></html>');
    w.document.close();
    return w;
}

http://jsfiddle.net/xtxK9/1/