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

Check if open browser windows contains a specific string

本文关键字:是否 包含特 字符串 窗口 浏览器 检查      更新时间:2023-09-26

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

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

  • 窗口 1 - 胡萝卜
  • 窗口 2 - 苹果
  • 窗口 5 - 日期
  • 窗口 10 - 香蕉

假设您的五个浏览器有五个地址。您可以使用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));
       }
    }

我写了一个小提琴来演示你如何做到这一点。 我依靠jQuery和浏览器本地存储,因此该解决方案不适用于古老的浏览器。 将代码粘贴到您的内部

$(document).ready

并测试

localStorage.allHaves

位字段以查看每个窗口中还有谁在附近(代码可以共享)。 记得做一个

parseInt

在你得到它之后的localStorage.allHaves值!

一个快速的解决方案,使用 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/