tampermonkey(油猴)跨域发送请求

tampermonkey发送请求可以跨域,比原生js方便很多。
制作学习积分外挂时,可以直接获取积分,根据积分情况判断下一步操作。

 GM_xmlhttpRequest({
                method: "get",
                url: "https://xx/open/api/score/today/queryrate",
                headers: {
                    "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
                },

                onload: function(response){
                    var name=response.responseText;
                    name=JSON.parse(name);                    currentScore=parseInt(name.data.dayScoreDtos[0].currentScore)+parseInt(name.data.dayScoreDtos[1].currentScore)+parseInt(name.data.dayScoreDtos[8].currentScore)+parseInt(name.data.dayScoreDtos[10].currentScore);
                    if (code==0){code=currentScore}
                    console.log('获得的积分为:',currentScore)
                    if(currentScore>code){
                        location.href='https://xxxxx/points/my-points.html';
                    }
                    code=currentScore
                },
                onerror: function(response){
                    console.log("请求失败");
                }
            });
在Vue中配置跨域可以通过修改配置文件或者使用插件来实现。下面分别介绍两种方法: 1. 修改配置文件 在Vue的config文件夹下的index.js中,可以找到如下代码: ```javascript module.exports = { dev: { // ... }, // ... } ``` 在其中添加如下代码: ```javascript module.exports = { dev: { // ... proxyTable: { '/api': { target: 'http://localhost:3000', // 接口的域名 changeOrigin: true, // 如果接口跨域,需要进行这个参数配置 pathRewrite: { '^/api': '' // 将/api替换为'' } } } }, // ... } ``` 其中,proxyTable表示代理表;/api表示需要代理的接口路径;target表示接口的域名;changeOrigin表示是否跨域;pathRewrite表示路径重写。 然后在发送请求时,将接口路径改为/api/即可。 例如: ```javascript this.$http.get('/api/userinfo').then((response) => { console.log(response.data) }) ``` 2. 使用插件 可以使用axios或者vue-resource等插件来发送跨域请求。以axios为例,可以在main.js中添加如下代码: ```javascript import axios from 'axios' Vue.prototype.$http = axios.create({ baseURL: 'http://localhost:3000', // 接口的域名 timeout: 5000, // 请求超时时间 withCredentials: true, // 允许携带cookie crossDomain: true // 允许跨域 }) ``` 然后在组件中使用: ```javascript this.$http.get('/userinfo').then((response) => { console.log(response.data) }) ``` 其中,baseURL表示接口的域名;timeout表示请求超时时间;withCredentials和crossDomain表示允许携带cookie和允许跨域
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值