空气外 发表于 2024-4-6 22:39:59

GM_xmlhttpRequest is not defined 怎样解决

![微信截图_20240406223855.png](data/attachment/forum/202404/06/223917hxaygr7q86zqhy57.png)
代码:
// ==UserScript==
// @name         testpost
// @namespace    http://tampermonkey.net/
// @version      2024-04-06
// @descriptiontry to take over the world!
// @author       You
// @match      http://localhost:5184/
// @icon         https://www.google.com/s2/favicons?sz=64&domain=undefined.localhost
// @grant      GM_xmlhttpRequest
// @connect      localhost
// @grant      none
// ==/UserScript==

(function() {
    'use strict';
console.log("now post");
    var jdata= {id:100,name:'yyyyyyyyyyyyyyp'};
    GM_xmlhttpRequest({
      url:"http://localhost:5184/pizzas",
      dataType: "json",

      method :"POST",

      data:"{\"id\":100,\"name\":\"yyyyyyyyyyyyyyp\"}",
      headers: {
            "Content-type": "application/json"
      },
      onload:function(xhr){
            console.log(xhr.responseText);
      }
    });
})();

出错为:
![微信截图_20240406223200.png](data/attachment/forum/202404/06/223805ndxqvs777ytmjyzj.png)

![微信截图_20240406223254.png](data/attachment/forum/202404/06/223824bihn1t28jj31x119.png)

潘钜森 发表于 2024-4-6 22:40:00

**把 `// @grant none` 去掉即可**

空气外 发表于 2024-4-6 23:55:00

潘钜森 发表于 2024-4-6 22:40
**把 `// @grant none` 去掉即可**

谢谢! 谢谢! 谢谢! 谢谢!

李恒道 发表于 2024-4-7 08:54:17

```
none是无沙盒环境,因为之前的漏洞总结的经验,现在已经禁止在无沙盒环境使用GM函数,所以我们脚本在none的情况下无法调用GM函数了

没有none就是沙盒环境,我们开发的脚本可以使用GM函数,此时已经与网页存在隔离,网页无法使用GM函数,但是脚本可以使用GM函数

当我们使用unsafeWindow的时候,相当于我们创造了一个原网页的引用可以对其进行数据的读写,但是同时我们也破坏了油猴的安全模型,当然这并不是十分可怕的事情,我们只需要注意一个问题,就是我们使用unsafeWindow所获取的数据,对其进行处理的行为是否是安全并且可靠的。
```

https://learn.scriptcat.org/%E6%B2%B9%E7%8C%B4%E6%95%99%E7%A8%8B/%E5%85%A5%E9%97%A8%E7%AF%87/%E6%B2%99%E7%9B%92%E6%9C%BA%E5%88%B6%E7%9A%84%E5%89%8D%E4%B8%96%E4%BB%8A%E7%94%9F/
页: [1]
查看完整版本: GM_xmlhttpRequest is not defined 怎样解决