油猴脚本中GM_xmlhttpRequest的数据如何获取
我想写一个函数包含GM_xmlhttpRequest,但是我该如何通过函数的返回值获得GM_xmlhttpRequest请求得到的数据?啥意思...直接return返回的数据不就可以 [油猴脚本开发指南]脚本ajax的跨域请求
https://bbs.tampermonkey.net.cn/thread-136-1-1.html
(出处: 油猴中文网)
Promise? https://bbs.tampermonkey.net.cn/thread-883-1-1.html 李恒道 发表于 2021-8-26 14:50
https://bbs.tampermonkey.net.cn/thread-883-1-1.html
感谢哥哥 明天就试试{:4_94:} 木羊羽 发表于 2021-8-26 22:06
感谢哥哥 明天就试试
难道哥哥有新的计划? 脚本体验师001 发表于 2021-8-27 19:46
难道哥哥有新的计划?
磁力搜索的那个已经更新了 ```js
// ==UserScript==
// @name New Userscript
// @namespace https://bbs.tampermonkey.net.cn/
// @version 0.1.0
// @description try to take over the world!
// @author You
// @match *
// @grant GM_xmlhttpRequest
// @grant unsafeWindow
// @console log
// ==/UserScript==
return new Promise((resolve, reject) => {
getData().then((data)=>{
return parseData(data)
}).then((data)=>{
console.log(data)
})
resolve();
});
function getData(){
return new Promise((resolve)=>{
GM_xmlhttpRequest({
url:"http://httpbin.org/get",
method :"GET",
headers: {
"Content-type": "application/x-www-form-urlencoded"
},
onload:function(xhr){
console.log(xhr.responseText)
resolve(xhr.responseText)
}
});
})
}
function parseData(data){
return new Promise((resolve)=>{
setTimeout(()=>{
console.log(JSON.parse(data).origin)
resolve("解析完成")
},2)
})
}
```
木羊羽 发表于 2021-8-28 08:07
磁力搜索的那个已经更新了
收到,马上我去看看
页:
[1]
2