关键词搜索

源码搜索 ×
×

Tampermonkey笔记-脚本的搭建和基本使用

发布2022-02-07浏览1922次

详情内容

首先要知道的,网页脚本,主要是解放双手,完成前端相关的工作。

这里直接到Tampermonkey官网在线安装就可以了。然后新建一个脚本:

  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 https://www.baidu.com
  8. // @icon https://www.google.com/s2/favicons?domain=csdn.net
  9. // @grant none
  10. // ==/UserScript==
  11. (function() {
  12. 'use strict';
  13. // Your code here...
  14. })();

要注意的地方:

@match是匹配的网址,这里先给他给成include简单点,如匹配所有

// @include  *

如果要匹配某bbs就:

// @include      *://bbs.xxx.com*

②如果要添加JQuery使用@require就可以了:

// @require      https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.5.1.min.js

如下结构:

  1. // ==UserScript==
  2. // @name New Userscript
  3. // @namespace http://tampermonkey.net/
  4. // @require https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.5.1.min.js
  5. // @version 0.1
  6. // @description try to take over the world!
  7. // @author You
  8. // @include *://bbs.xxx.com*
  9. // @icon https://www.google.com/s2/favicons?domain=csdn.net
  10. // @grant none
  11. // ==/UserScript==
  12. (function() {
  13. 'use strict';
  14. // Your code here...
  15. })();

这样就可以搞自己的脚本了。

比如,当网页加载完成后,填某些表单,自动提交某些数据:

  1. // ==UserScript==
  2. // @name New Userscript
  3. // @namespace http://tampermonkey.net/
  4. // @require https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.5.1.min.js
  5. // @version 0.1
  6. // @description try to take over the world!
  7. // @author You
  8. // @include *://bbs.xxx.com*
  9. // @icon https://www.google.com/s2/favicons?domain=csdn.net
  10. // @grant none
  11. // ==/UserScript==
  12. (function() {
  13. 'use strict';
  14. // Your code here...
  15. window.onload=function(){
  16. // TODO
  17. }
  18. })();

又如,搞页面上搞一个按钮,到时候人为点击下执行脚本:

  1. // ==UserScript==
  2. // @name New Userscript
  3. // @namespace http://tampermonkey.net/
  4. // @require https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.5.1.min.js
  5. // @version 0.1
  6. // @description try to take over the world!
  7. // @author You
  8. // @include *://bbs.xxx.com*
  9. // @icon https://www.google.com/s2/favicons?domain=csdn.net
  10. // @grant none
  11. // ==/UserScript==
  12. (function() {
  13. 'use strict';
  14. // Your code here...
  15. var btn = document.getElementById("zan_btn") || document.createElement("div");
  16. btn.style.padding = "https://cdn.jxasp.com:9143/image/20px 40px";
  17. btn.style.color = "#fff";
  18. btn.style.backgroundColor = "#f78989";
  19. btn.style.border = "1px solid #f78989";
  20. btn.style.position = "fixed";
  21. btn.style.right = "10px";
  22. btn.style.top = "10px";
  23. btn.style.zIndex = "99999";
  24. btn.style.borderRadius = "4px";
  25. btn.style.fontSize = "https://cdn.jxasp.com:9143/image/22px";
  26. btn.style.cursor = "pointer";
  27. btn.innerHTML = "开始脚本";
  28. btn.id = "zan_btn";
  29. btn.onclick = () =>{
  30. // TODO
  31. }
  32. })();

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载