在以往的XmlHttprequest對象中想要跨域請求基本上就是靠jsonp之斯,油猴腳本可以實現(xiàn)自定義網(wǎng)頁腳本光涂,但是他同樣無法避免的要被CORS阻止庞萍。
如何使用 @grant注解 實現(xiàn)使用油猴自帶的GM_xmlhttpRequest發(fā)送跨域請求?
通常新建一個腳本之后是這樣的:
符號 // 不是代表注釋
// ==UserScript==
// @name New Userscript
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match url
// @grant none
// ==/UserScript==
/* jshint -W097 */
'use strict';
// Your code here...
@grant可以使用一些加強函數(shù)這些函數(shù)都是以GM_開頭的。
如果@grant是none的話就只能使用GM_info這個函數(shù)了忘闻。
之后我們的代碼中就可以使用GM_xmlhttpRequest函數(shù)了
// ==UserScript==
// @name New Userscript
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://www.uuuuuu.com/mdeditor
// @grant GM_xmlhttpRequest
// @grant GM_download
// ==/UserScript==
/* jshint -W097 */
'use strict';
GM_xmlhttpRequest({
method: "GET",
url: "http://www.qq.com/",
onload: function(response) {
//這里寫處理函數(shù)
}
});
處理這些屬性之外還有一些其他的屬性:
url - the URL from where the data should be downloaded
name - thefilename - for security reasons the file extension needs to bewhitelisted at the Tampermonkey options page
headers - seeGM_xmlhttpRequest for more details saveAs - boolean value, show asaveAs dialog
onload - function() {} - callback function that iscalled when the download has finished
onerror - function(download) {}
callback function that is called when there was an error
下面給出一個通過js爬取某個網(wǎng)站信息的例子:
jquery 中 (子钝计,父),即從父節(jié)點里尋找子節(jié)點,是選擇li標簽里面所有div標簽私恬,而不是找li標簽外面的div標簽债沮。
比如:
var el = $( '<div></div>' );
el.html(data);
var tda= $("div.content div.header a.name em", el) ;
var detailUrl = $("div.content div.header a.name ", el) ;
完整例子,只是demo
// ==UserScript==
// @name 爬取
// @namespace http://tampermonkey.net/
// @version 0.1
// @description 爬取
// @author bob
// @match https://www.txxxxxx.com/search*
// @requir https://code.jquery.com/jquery-latest.js
// @require https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js
// @run-at document-idle
// @grant GM_xmlhttpRequest
// ==/UserScript==
(function () {
'use strict';
var $ = $ || window.$;
function sleep(numberMillis) {
var now = new Date();
var exitTime = now.getTime() + numberMillis;
while (true) {
now = new Date();
if (now.getTime() > exitTime)
return;
}
}
var enterpriseList = [ ];
var dataList = [];
var errorListIds = [];
//獲取企業(yè)列表
GM_xmlhttpRequest({
method: 'GET',
url: "http://xxx.xxxxxx.com/yyyyyy/queryEnterpriseList?startID=44633&endID=45000",
headers: {
"Content-Type": "application/json"
},
onload: function(response) {
enterpriseList = $.parseJSON( response.responseText );
getData();
},
});
//saveData(data);
var count = 0;
function saveData(data){
GM_xmlhttpRequest({
method: 'POST',
data:JSON.stringify(data),
url: "http://xxx.xxxxx.com/yyyyyy/saveDataList",
dataType: "json",
headers: {
"Content-Type": "application/json"
},
onload: function(response) {
},
});
}
function getData(){
for(var i=0;i<enterpriseList.length;i++){//
var one = {};
var url = 'https://www.uuuuuuuuu.com/search?key='+enterpriseList[i].enterpriseName.replace("(","(").replace(")",')');
try{
var num=Math.floor(Math.random()*10+5);//隨機等待幾秒
sleep(num*1000);
$.ajax({url:url,
async:false,
// timeout:5000,
success:function(data){
var str = "index_verify?type=companysearch"
if(data.indexOf(str) != -1){
i = enterpriseList.length;
console.log('****************************************操作需要驗證');
}
var el = $( '<div></div>' );
el.html(data);
var tda= $("div.content div.header a.name em", el) ;
var detailUrl = $("div.content div.header a.name ", el) ;
if(tda.length >0 && tda[0].innerText == enterpriseList[i].enterpriseName){
$.ajax({url:detailUrl[0].href,
async:false,
// timeout:5000,
success:function(data){
var el = $( '<div></div>' );
el.html(data);
one.enterpriseName = enterpriseList[i].enterpriseName ;//公司名稱
var tr = $("#_container_baseInfo table.table.-striped-col.-border-top-none tr", el);
if(businessTerm.length>8){
if(businessTerm.substring(0,3) != '***'){
one.startTime = businessTerm.substring(0,9) ;
if(businessTerm.substr(11) == '無固定期限'){
one.endTime = '2099-12-31';
}else{
one.endTime = businessTerm.substr(11);
}
}else{
one.startTime = td[12].innerText.replace(/\s+/g,"").replace(/[\n\r]/g,'') ;
if(businessTerm.substr(4) == '無固定期限'){
one.endTime = '2099-12-31';
}else{
one.endTime = businessTerm.substr(4);
}
}
}
}, error:function(data){}});
}else{
one.qualificationNo = '--';
}
dataList.push(one);
if(dataList.length == 10){
saveData(dataList);//保存數(shù)據(jù)
dataList = [];
count += 10;
console.log('已保存數(shù)據(jù):'+count);
}
}, error:function(data){ }});
}catch(e){
}
}
}
})();