如何使用Javascript自动打印一系列HTTP搜索查询的搜索结果?
我有一个tampermonkey脚本,我试图获取一个名称数组,并对每个名称执行搜索并打印页面。加载页面时,它会自动运行,这就是为什么需要使用如何使用Javascript自动打印一系列HTTP搜索查询的搜索结果?,javascript,jquery,arrays,tampermonkey,Javascript,Jquery,Arrays,Tampermonkey,我有一个tampermonkey脚本,我试图获取一个名称数组,并对每个名称执行搜索并打印页面。加载页面时,它会自动运行,这就是为什么需要使用if语句的原因 $(document).ready(function(){ var searchBar = $('input[name="searchfield"]'); var submit = $('button[name="searchbyname"]'); if ( searchBar.val() < 1 ) {
if
语句的原因
$(document).ready(function(){
var searchBar = $('input[name="searchfield"]');
var submit = $('button[name="searchbyname"]');
if ( searchBar.val() < 1 ) {
var namesArray = prompt('enter names to search, separated by commas').split(', ');
$.each(namesArray, function(i, v) {
$(searchBar).val(v);
$(submit).trigger('click');
window.print();
});
}
})
$(文档).ready(函数(){
var searchBar=$('input[name=“searchfield”]”);
var submit=$('button[name=“searchbyname”]”);
if(searchBar.val()<1){
var namesArray=prompt('输入要搜索的名称,用逗号分隔')。拆分(',');
$.each(名称数组、函数(i、v){
$(搜索栏).val(v);
$(提交).trigger('click');
window.print();
});
}
})
我的问题是它只运行
$(submit.trigger('click')代码>在最后一个循环中通过。因此,如果我的数组是“一、二、三”,它将在搜索栏中输入“一”,替换为“二”,然后是“三”,然后才真正触发搜索按钮。提交是否通过AJAX处理
- 是的,这是一个AJAX表单:在截图之前,您需要等待服务器响应和DOM更新
- 不,这是一个普通的POST表单:当文档被下一个HTTP请求替换时,Javascript运行时将消失
- 不,这是DOM中的内联javascript搜索:使用承诺或其他延迟策略来确保在打印之前所有内容都已更新仍然是一个好主意
无论哪种方式,我都建议在按钮上单击一个onclick
处理程序来帮助您进一步调试。我打赌它正在被触发,但您正在继续操作,而不是推迟到响应加载
每个新请求都会对最后一个请求进行重击,因此即使所有请求都会触发,您也只能看到最后一个请求生效。(这个答案是对我所认为的真正需要的回应,“如何使用Javascript自动打印一系列HTTP搜索查询的搜索结果?”,真诚地说,海报将相应地调整问题。)
实际上,您正在尝试使用Javascript打印来自不同页面的搜索结果。您的方法不适用于此目的(因此,$的原始问题。每个
循环无效);每次提交搜索时,Javascript运行时和用户脚本都会被屏蔽
为了实现最终目标,您需要将查询循环与提交HTTP请求的窗口分开。诀窍:创建一个包含此网站的iframe,然后从顶部的窗口控制该iframe
下面是可以直接复制到开发人员控制台的代码。我只在控制台上测试过它,但它应该适合像Tampermonkey这样的脚本注入器
(function(){
// Define the bits that work with this particular website.
var fieldname = 'searchfield';
var formname = 'ECPCIS_list';
// Figure out which names need to be searched.
var query_iter = 0
, queries = prompt('What names do you want to search for?').split(',').filter(function (val) {
return val.trim();
});
if (!queries.length) { return; }
// Store the current URL.
var url = window.location.href;
// Reopen the current document (in the context of the current domain security), and replace the
// document contents with a single iframe. The iframe source is equal to the original URL.
document.open();
document.close();
var iframe = document.createElement('IFRAME');
// Make sure that the styles are set up in such a way that the iframe gets full height.
// We'll add a listener that resizes the `top` window (our script) whenever the iframe loads.
document.body.setAttribute('style', "width:100%;height:100%;margin:0;");
iframe.setAttribute('style', "width:100%;height:100%;");
iframe.setAttribute('scrolling', 'no');
// Create a method to handle the query/repeat lifecycle.
var runQuery = function() {
document.body.style.height = iframe.contentWindow.getComputedStyle(iframe.contentDocument.body).getPropertyValue('height');
// Find the search box. If it doesn't exist yet, continue to wait.
var fields = iframe.contentDocument.getElementsByName(fieldname);
if (fields.length == 0) {
setTimeout(100, runQuery);
return;
}
// If this isn't the first iteration, we need to wait to print the screen.
if (query_iter > 0) {
window.print();
if (query_iter >= queries.length) { return; }
}
// Set the query in the search box.
fields[0].value = queries[query_iter];
// Increment the query iteration.
query_iter += 1;
// Submit the form. This will refresh the iframe.
// When it is done loading, runQuery will be executed again.
iframe.contentDocument.getElementsByName(formname)[0].submit();
}
iframe.addEventListener('load', runQuery);
// Stick the iframe into the DOM and load the original page.
// Now it looks like we're in the same page we were just on; the fact that there are two layers
// is visually hidden. IOW, the "window.print" method will capture the right output.
document.body.appendChild(iframe);
iframe.src = url;
})()
请注意,为您的用例专门设计的部分是顶部的fieldname
和formname
。其余部分是完全通用的;您可以与任何在输入
和表单
元素上具有适当名称
属性的网站一起使用。我认为每次迭代都会触发它。但是,由于页面在提交时正在刷新,因此下面的迭代将完成并附加输入。那么您实际上得到了3个triggersYeah,提交的表单是AJAX表单还是在触发提交按钮后刷新页面?另外,在使用jQuery时,如果您已将jQuery对象保存到var submit
,则只需使用submit.trigger('click')代码>。此外,您可以将其缩短为只调用submit.click()代码>而不是触发器。这个问题并不反映您实际想要知道如何做-您正在尝试从多个连续搜索中自动捕获数据。这篇文章提到了一些自动搜索查询抓取的工具:。如果您将此问题改为“如何使用Javascript自动打印一系列HTTP搜索查询的搜索结果?”,我将为您提供一种无需任何浏览器扩展的技术。当您调用$(submit).trigger('click')
它通过setImmediate
或类似的方式触发单击处理程序。这意味着它会更改循环3次,设置值3次,然后才进行AJAX调用(3次)。有点奇怪的是,您使用假用户交互作为中间阶段-也许您可以将其更改为function(i,v){$.ajax(…);}
。我不认为它使用的是ajax。你可以看到。如果是这种情况,我需要更改什么来修复它?您需要使用不同的工具;)Tampermonkey将脚本注入当前运行时。当您提交一个POST表单(就像那里的表单)时,Javascript运行时结束;从服务器加载一个新文档,并开始一个新的Javascript运行时。您的代码被重新注入并从头开始。