检查打开的浏览器窗口是否包含特定字符串
Check if open browser windows contains a specific string
>假设我有一个同时打开多个窗口的网站。数字打开的窗口未知,每个窗口中的文本未知。我应该如何测试或确保至少以下字符串(苹果、香蕉、胡萝卜,日期)包含在窗口集中。
请记住,每页只能有一个字符串,例如:
- 窗口 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/
相关文章:
- 此行是否包含函数声明
- 查找对象数组是否包含其中一个标记的最快方法
- 如何使用JQuery检查输入(电子邮件)字段是否包含特殊字符
- 检查数组是否包含
- 在添加或删除之前,正在使用检查classList是否包含类
- 确定数组是否包含偶数
- 如何检查 iframe 中的锚标记是否包含 URL
- 检查选择器在jQuery中是否包含文本(即不是媒体)
- 检查字符串是否包含计算
- 使用 javascript 检查元素是否包含子标记而不是文本
- 使用 Chai - 如何检查对象属性是否包含 DOM 元素
- 测试两个变量是否包含一些数据 JavaScript
- 如何检查列表是否包含 javascript 中的字符串
- immutablejs:查找映射是否包含键值
- 检查上一个(-1&-2)url是否包含字符串
- 如何检查url是否包含多个字符串.Javascript/jquery
- 检查数组是否包含指定的对象
- 如何检查字符串是否包含非英语字符列表
- 检查Javascript是否包含某种方法
- 检查字符串是否包含数组中的某个字符串