TamperMonkey油猴/暴力猴浏览器脚本插件中文网(原TMchina)

打印 上一主题 下一主题

油猴脚本实现跨域请求或下载文件

[复制链接]
跳转到指定楼层
楼主
发表于 2019-12-11 11:07:12 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多网友,浏览器插件/脚本不再愁!

您需要 登录 才可以下载或查看,没有帐号?注册会员

x

1.介绍
在以往的XmlHttprequest对象中想要跨域请求基本上就是靠jsonp,油猴脚本可以实现自定义网页脚本,但是他同样无法避免的要被CORS阻止。这篇文章就是讲解如何使用@grant注解实现使用油猴自带的GM_xmlhttpRequest发送跨域请求。

2.什么是油猴脚本
油猴脚本是一个浏览器插件通常在google中叫tampermonkey,在火狐中叫greasemonkey。具体的用法可以去百度一下。这里提供两个现在可以使用的脚本网站
https://www.tampermonkey.cn/forum-2-1.html
http://userscripts-mirror.org/
https://greasyfork.org/zh-CN/scripts

3.什么是跨域请求
通俗的来说就是在一个网页去请求了别的网页(这些网页不属于这个网站或者端口协议不同)。

主要原因还是由于安全限制(同源策略, 即JavaScript或Cookie只能访问同域下的内容)
http://blog.csdn.net/hfahe/article/details/7730944

4.如何在脚本中使用GM_xmlhttpRequest

在很久以前的中国有一个石猴子(偏题)。通常新建一个脚本之后是这样的。

  1. // ==UserScript==
  2. // @name         New Userscript
  3. // @namespace    http://tampermonkey.net/
  4. // @version      0.1
  5. // @description  try to take over the world!
  6. // @author       You
  7. // @match        url
  8. // @grant        none
  9. // ==/UserScript==
  10. /* jshint -W097 */
  11. 'use strict';

  12. // Your code here...
复制代码


其他的@..不说,只看@grant在官方文档中是这样描述的:

@grant is used to whitelist GM_* functions, the unsafeWindow object and some powerful window functions. If no @grant tag is given TM guesses the scripts needs.


就是说使用@grant可以使用一些加强函数这些函数都是以GM_开头的

If @grant is followed by 'none' the sandbox is disabled and the script will run directly at the page context. In this mode no GM_* function but the GM_info property will be available.


如果@grant是none的话就只能使用GM_info这个函数了。
老偏题呢。

这是我们要引用

// @grant GM_xmlhttpRequest

  1. 之后我们的代码中就可以使用GM_xmlhttpRequest函数了
  2. // ==UserScript==
  3. // @name         New Userscript
  4. // @namespace    http://tampermonkey.net/
  5. // @version      0.1
  6. // @description  try to take over the world!
  7. // @author       You
  8. // @match        https://www.zybuluo.com/mdeditor
  9. // @grant        GM_xmlhttpRequest
  10. // @grant        GM_download
  11. // ==/UserScript==
  12. /* jshint -W097 */
  13. 'use strict';
  14. GM_xmlhttpRequest({
  15.   method: "GET",
  16.   url: "http://www.qq.com/",
  17.   onload: function(response) {
  18.      //这里写处理函数
  19.   }
  20. });
复制代码


处理这些属性之外还有一些其他的属性:

  • 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


以上内容来自https://tampermonkey.net/documentation.php 和一些自己的总结

作者:onlyxx


TM中文网 - 脚本安装指南

1、本站ZIP导入包一般命名类似为tampermonkey.cn_xxx_script.zip,一般无需解压直接导入(导入不成功则解压看是否里面还有ZIP文件,有的话再导入)。
2、JS版为代码版,可在油猴/暴力猴控制台直接添加脚本的方式把JS文件内的代码复制粘贴进去,保存即可成功安装脚本。
3、脚本导入的相关教程《油猴脚本导入教程》/《暴力猴脚本导入教程


回复

使用道具 举报

沙发
发表于 2021-12-10 03:54:18 | 只看该作者
感谢🙏
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

最近更新的脚本

免责声明

油猴中文网所发布的一切浏览器脚本及应用的帖子仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该应用,请支持开发者,若有VIP相关脚本,我们推荐您购买VIP得到更好的官方服务。(如有侵犯了您权益的应用请点此处联系我们处理

联系我们|Archiver|小黑屋|sitemap|, Processed in 0.030563 second(s), 12 queries , File On.   
快速回复 返回顶部 返回列表