PtSiteToNasTools

P将T站点cookie发送到nastools站点管理

目前为 2023-05-05 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name PtSiteToNasTools
  3. // @author shareit
  4. // @description P将T站点cookie发送到nastools站点管理
  5. // @namespace http://tampermonkey.net/
  6.  
  7. // @include http://hdmayi.com/*
  8. // @include http://ihdbits.me/*
  9. // @include https://zhuque.in/*
  10. // @include https://1ptba.com/*
  11. // @include https://52pt.site/*
  12. // @include https://audiences.me/*
  13. // @include https://byr.pt/*
  14. // @include https://ccfbits.org/*
  15. // @include https://club.hares.top/*
  16. // @include https://cyanbug.net/*
  17. // @include https://discfan.net/*
  18. // @include https://et8.org/*
  19. // @include https://filelist.io/*
  20. // @include https://hdatmos.club/*
  21. // @include https://hdchina.org/*
  22. // @include https://hdcity.leniter.org/*
  23. // @include https://hdfans.org/*
  24. // @include https://hdhome.org/*
  25. // @include https://hdmayi.com/*
  26. // @include https://hdsky.me/*
  27. // @include https://hdtime.org/*
  28. // @include https://hdvideo.one/*
  29. // @include https://hhanclub.top/*
  30. // @include https://hudbt.hust.edu.cn/*
  31. // @include https://iptorre*
  32. // @include https://kp.m-team.cc/*
  33. // @include https://leaves.red/*
  34. // @include https://lemonhd.org/*
  35. // @include https://nanyangpt.com/*
  36. // @include https://npupt.com/*
  37. // @include https://ourbits.club/*
  38. // @include https://piggo.me/*
  39. // @include https://pt.btschool.club/*
  40. // @include https://pt.eastgame.org/*
  41. // @include https://pt.hdbd.us/*
  42. // @include https://pt.keepfrds.com/*
  43. // @include https://ptchina.org/*
  44. // @include https://pterclub.com/*
  45. // @include https://pthome.net/*
  46. // @include https://springsunday.net/*
  47. // @include https://totheglory.im/*
  48. // @include https://u2.dmhy.org/*
  49. // @include https://www.beitai.pt/*
  50. // @include https://www.carpet.net/*
  51. // @include https://www.haidan.video/*
  52. // @include https://www.hd.ai/*
  53. // @include https://www.hdarea.co/*
  54. // @include https://www.hddolby.com/*
  55. // @include https://www.hdpt.xyz/*
  56. // @include https://www.htpt.cc/*
  57. // @include https://www.icc2022.com/*
  58. // @include https://www.nicept.net/*
  59. // @include https://www.oshen.win/*
  60. // @include https://www.pthome.net/*
  61. // @include https://www.tjupt.org/*
  62. // @include https://www.torrent*
  63. // @include https://zmpt.cc/*
  64. // @include https://carpt.net/*
  65. // @include https://wintersakura.net/*
  66. // @include https://sharkpt.net/*
  67. // @include https://gainbound.net/*
  68. // @include https://hdvideo.one/*
  69. // @include https://pt.2xfree.org/*
  70. // @include http://www.oshen.win/*
  71. // @include http://uploads.ltd/*
  72. // @include https://ubits.club/*
  73. // @include http://hdzone.me/*
  74. // @include https://pt.soulvoice.club/*
  75. // @include https://www.gamegamept.com/*
  76. // @include https://pt.itzmx.com/*
  77. // @include https://pt.0ff.cc/*
  78. // @include https://xxxx/*
  79. // @include https://xxxx/*
  80. // @include https://xxxx/*
  81. // @include https://xxxx/*
  82. // @include https://xxxx/*
  83. // @include https://xxxx/*
  84. // @include https://xxxx/*
  85.  
  86.  
  87.  
  88.  
  89. // @version 3.2.4
  90. // @grant GM_xmlhttpRequest
  91. // @grant GM_cookie
  92. // @grant GM_setValue
  93. // @grant GM_getValue
  94. // @license GPL-3.0 License
  95. // ==/UserScript==
  96.  
  97. /*
  98. 日志:
  99. 20230301:适配nas-tools 3.0.0版本,使用新API更新站点Cookie。
  100. 20230124:适配nas-tools 2.8.0版本,下次更新保留ip地址和token到浏览器存储避免更新后重新填写。
  101. 20221124:适配nas-tools 2.5.0版本。
  102. 20221029:适配nas-tools 2.3.0版本。
  103. 20220924:适配nas-tools 2.1.2版本。修复一些Bug
  104. 20220924:适配nas-tools 2.1.0版本。
  105. 20220914:适配nas-tools 2.0.5版本。
  106. */
  107.  
  108. // 设置nas-tools的访问地址,如http://192.168.1.2:300
  109. let nastoolurl = "http://192.168.199.248:3000";
  110. // 获取nas-tools的安全密钥,基础设置-安全-API密钥
  111. var token = "None";
  112. // 如果油猴插件是测试版(可获取更多cookie),保持不变,否则改为"BET"
  113. var tampermonkeyVersion = "BETA";
  114. // 自定义配置:日常观影,下载设置默认(若预设则填写-1),解析rss,ua, 浏览器仿真,N 不用代理服务器,消息通知,字幕下载
  115. var my_site_note = {
  116. "rule": "1000",
  117. "download_setting": "",
  118. "parse": "Y",
  119. "ua": navigator.userAgent,
  120. "chrome": "Y",
  121. "proxy": "N",
  122. "message": "Y",
  123. "subtitle": "Y"
  124. };
  125. // 自定义配置:默认优先级为"2"
  126. var my_site_pri = "2"
  127. // 自定义配置:签到Q、订阅D(未设置rss链接时无法订阅)、刷流S(未设置rss无法刷流)、统计T
  128. var my_site_include = "QDST"
  129. // 下面这些不用修改
  130. var userSitesApi = "/api/v1/site/sites"
  131. var cookieUpdateApi = "/api/v1/site/cookie/update"
  132. var dorandom = "/do?random=0.19956351081249935";
  133. // this.$$ = this.jQuery;
  134. console.log("【DEBUG】当前nastools地址:" + nastoolurl);
  135. console.log("【DEBUG】当前token:" + token);
  136.  
  137. (function () {
  138. 'use strict';
  139. main();
  140. })();
  141.  
  142. async function getUserSitesByApi(){
  143. return new Promise((resolve, reject) => {
  144. GM_xmlhttpRequest({
  145. method: "GET",
  146. url: nastoolurl + userSitesApi,
  147. headers: {
  148. 'Origin': nastoolurl,
  149. "Authorization":token,
  150. },
  151. onload: function (response) {
  152. var data = JSON.parse(response.responseText);
  153. console.log("【Debug】nas-tools请求userSite成功,status:",data.code);
  154. // console.log(response.responseText);
  155. if(data.code==401){
  156. console.log("【Debug】获取userSite失败,请检查密钥是否正确");}
  157. resolve(data.data.user_sites)
  158. },
  159. onerror: function (response) {
  160. console.log("【Debug】nas-tools请求userSite失败,请检查IP地址是否设置正确");
  161. reject("");
  162. }
  163. })
  164. })
  165. }
  166.  
  167. async function sendSiteToNastools(data) {
  168. data = "cmd=update_site&data=" + encodeURIComponent(JSON.stringify(data)); // JSON.stringify 时候遇到rssurl中的&会自动分割,不能用encode
  169. // console.log(data)
  170. GM_xmlhttpRequest({
  171. method: "POST",
  172. url: nastoolurl + dorandom,
  173. headers : {
  174. 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
  175. 'Origin': nastoolurl,
  176. 'X-Requested-With': 'XMLHttpRequest',
  177. },
  178. data: data,
  179. onload: function (response) {
  180. console.log("【Debug】nas-tools请求发送成功");
  181. if (JSON.parse(response.responseText).code !== true){ // code : true 为请求成功
  182. console.log("【Debug】添加站点失败,可能由于nas-tools更新导致参数不对");
  183. console.log(response.responseText)
  184. }
  185. },
  186. onerror: function (response) {
  187. console.log("nas-tools更新站点请求失败");
  188. }
  189. });
  190. console.log("【Debug】站点信息发送完毕");
  191. }
  192.  
  193. async function updataCookie(data){
  194. data = "site_id=" + data.site_id + "&site_cookie=" + encodeURIComponent(data.site_cookie);
  195. // data = JSON.stringify(data);
  196. GM_xmlhttpRequest({
  197. method: "POST",
  198. url: nastoolurl + cookieUpdateApi,
  199. headers : {
  200. 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
  201. 'Origin': nastoolurl,
  202. "Authorization":token,
  203. },
  204. data: data,
  205. onload: function (response) {
  206. console.log("【Debug】nas-tools请求发送成功");
  207. if (JSON.parse(response.responseText).code !== 0){
  208. console.log("【Debug】更新站点cookie失败,可能是API返回变更");
  209. }
  210. },
  211. onerror: function (response) {
  212. console.log("nas-tools更新站点cookie请求失败");
  213. }
  214. });
  215. console.log("【Debug】站点信息发送完毕");
  216. }
  217.  
  218. async function getData() {
  219. console.log("【Debug】开始获取PT站点信息");
  220. var data = {};
  221. var ptCookie = document.cookie;
  222. if (tampermonkeyVersion == "BETA") {
  223. GM_cookie('list', { // 异步,如果在return data之前还没执行完,部分站点会导致cookie不全。
  224. url: location.href
  225. }, (cookies) => {
  226. ptCookie = cookies.map(c => `${c.name}=${c.value}`).join('; ');
  227. console.log('【Debug】cookie:', ptCookie);
  228. // data.site_cookie = ptCookie;
  229. });
  230. }
  231. // var ptUrl = document.URL;
  232. var parser = document.createElement('a')
  233. parser.href = document.URL
  234. var ptUrl = "https://"+parser.hostname
  235. // var ptUrl = window.location.hostname;
  236. // console.log("host: ",window.location.host);
  237. // console.log("hostname: ",window.location.hostname);
  238. if (ptUrl.length > 150){
  239. console.log("【Debug】可能是cf盾,进入首页后再刷新一下页面");
  240. return false;
  241. }
  242. // var ptTitle = (document.title + "").split('::')[0];
  243. // const pattern = /[`~!@#$^\-&*()=|{}':;'\\\[\]\.<>\/?~!@#¥……&*()——|{}【】';:""'。,、?\s]/g; // 去除( "等
  244. // ptTitle = ptTitle.replace(pattern, "").substr(0,15);
  245. var ptTitle = parser.hostname
  246. if (ptCookie == "" && tampermonkeyVersion!="BETA"){
  247. console.log("【Debug】获取的cookie为空,请使用BETA版油猴,退出脚本");
  248. return false
  249. }
  250. // 默认配置
  251. data.site_id = "";
  252. data.site_name = ptTitle;
  253. data.site_pri = my_site_pri;
  254. data.site_rssurl = "";
  255. data.site_signurl = ptUrl;
  256. data.site_cookie = ptCookie;
  257. data.site_include = my_site_include;
  258. data.site_note = my_site_note;
  259. var user_sites = await getUserSitesByApi();
  260. if (user_sites == ""){ return data } // 第一次使用,一个站点都没有
  261. var host_url = ptUrl.substring(8,).replace("/index.php",""); // 解决因index不是签到url导致nas-tools签到失败
  262. for (var i = 0, l = user_sites.length; i < l; i++) {
  263. if (user_sites[i].signurl.search(host_url)!==-1) {
  264. var temp = user_sites[i]
  265. console.log("【Debug】查询到nas-tools中该站点的信息:",temp.name);
  266. if(temp.cookie == ptCookie){
  267. console.log("【Debug】cookie未过期,退出脚本");
  268. return false;
  269. }
  270. data = {};
  271. data.site_id = temp.id;
  272. data.site_cookie = ptCookie;
  273. //data.site_id = temp.id;
  274. //data.site_name = temp.name;
  275. //data.site_pri = temp.pri;
  276. //data.site_rssurl = temp.rssurl; // rss中存在&导致后面把json转换字符串在编码时候出现分割,先encode一下,但是后面在encode时候会出错
  277. //data.site_signurl = temp.signurl;
  278. //data.site_cookie = ptCookie;
  279. //data.site_include = ""
  280. //if(temp.signin_enable == true){data.site_include += "Q";} // 签到
  281. //if(temp.rss_enable == true){data.site_include += "D";} // rss
  282. //if(temp.brush_enable == true){data.site_include += "S";} // 刷流
  283. //if(temp.statistic_enable == true){data.site_include += "T";} // 统计
  284. //data.site_note = {};
  285. //data.site_note.rule = temp.rule;
  286. //data.site_note.parse = temp.parse;
  287. //data.site_note.ua = temp.ua;
  288. //temp.chrome == true ? data.site_note.chrome = "Y" :data.site_note.chrome = "N";
  289. //temp.proxy == true ? data.site_note.proxy = "Y" :data.site_note.proxy = "N";
  290. //temp.message == true ? data.site_note.message = "Y" :data.site_note.message = "N";
  291. // temp.subtitle == true ? data.site_note.subtitle = "Y" :data.site_note.subtitle = "N"
  292. // data.site_note.download_setting = temp.download_setting
  293. updataCookie(data);
  294. return false;
  295. }
  296. }
  297. return data;
  298. }
  299.  
  300. async function main() {
  301. var data = await getData();
  302. if(data==false) return ;
  303. // await ajax_post("update_site",data,"")
  304. await sendSiteToNastools(data);
  305. }
  306.  
  307.  
  308. async function ajax_post(cmd, data, handler){
  309. // @require https://lib.baomitu.com/jquery/3.6.0/jquery.min.js
  310. data = {
  311. cmd: cmd,
  312. data: JSON.stringify(data)
  313. };
  314. $$.ajax({ // (已屏蔽:mixed-content) 无法在https里面发生http请求,因此暂时不使用
  315. type: "POST",
  316. url: nastoolurl + dorandom,
  317. dataType: "json",
  318. data: data,
  319. cache: false,
  320. timeout: 0,
  321. success: "", // 待更新
  322. error: function(){ // 待更新
  323. }
  324. });
  325. }