2013-10-24 42 views
0

假設我有一個網站一次打開多個窗口。打開的窗口號碼 未知,每個窗口中的文本都是未知的。 我應該如何測試或確保至少以下字符串(蘋果,香蕉,胡蘿蔔,日期)包含在一組窗口中。檢查打開的瀏覽器窗口是否包含特定的字符串

銘記只能有一個每頁字符串,例如:

  • 窗口1 - 胡蘿蔔
  • 窗口2 - 蘋果
  • 窗口5 - 日期
  • 窗口10 - 香蕉

回答

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)); 
     } 
    } 
+0

你的答案是偉大的,如果只有我知道多少p年齡會打開(你假設它的五個,也假定我知道他們的地址)。如果知道任何它將在首頁地址,如果我訪問它將打開和未知數量的頁面。 –

0

我寫了一些fiddle展示你如何能夠做到這一點。我依靠jQuery和瀏覽器localStorage,因此該解決方案在古代瀏覽器上無法使用。堅持代碼你

$(document).ready 

內部和測試

localStorage.allHaves 

位域看到的是從周圍的每個窗口(代碼可以共享)的還有誰。記得做個

parseInt 

在得到它之後的localStorage.allHaves值!

+0

我剛剛更正了小提琴的副本和粘貼錯誤,所以如果您使用它,請確保使用正確的版本! – DroidOS

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/

+0

我看着兩種方式,同樣的問題是,你認爲我知道打開頁數(窗口)。我不知道。如果有辦法獲得打開的窗戶數量並檢查每個人,這將是很大的 –

+0

@MoAdel基本上窗口的數量是無關緊要的,你可以隨心所欲地以任何方式打開窗戶,只要你想,但你總是需要將其存儲(推送)到一個數組(窗口數組)中,以便隨後迭代它並搜索每個窗口的內容。因此,測試內容部分以及使用ids四次調用openWindow僅用於在測試環境中創建示例數據。你真正需要的是一個數組或數據結構,包含你正在搜索的單詞和searchContent函數,或者只是迭代windows數組來搜索內容。 – melc

相關問題