PtSiteToNasTools

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

  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://www.pttime.org/*
  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.7
  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.1.13: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 = {
  169. cmd: "update_site",
  170. data: data
  171. };
  172. // post_data = {}
  173. // post_data.cmd="update_site";
  174. // post_data.data = data;
  175. // data = "cmd=update_site&data=" + encodeURIComponent(JSON.stringify(data)); // JSON.stringify 时候遇到rssurl中的&会自动分割,不能用encode
  176. data = JSON.stringify(data)
  177. console.log("更新site:"+data);
  178. GM_xmlhttpRequest({
  179. method: "POST",
  180. url: nastoolurl + dorandom,
  181. headers : {
  182. // 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
  183. 'Accept': 'application/json, text/javascript, */*; q=0.01',
  184. // 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
  185. 'Content-Type': 'application/json',
  186. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36',
  187. 'Origin': nastoolurl,
  188. 'X-Requested-With': 'XMLHttpRequest',
  189. },
  190. data: data,
  191. onload: function (response) {
  192. console.log("【Debug】nas-tools请求发送成功");
  193. if (JSON.parse(response.responseText).code !== true){ // code : true 为请求成功
  194. console.log("【Debug】添加站点失败,可能由于nas-tools更新导致参数不对");
  195. console.log(response.responseText)
  196. }
  197. },
  198. onerror: function (response) {
  199. console.log("nas-tools更新站点请求失败");
  200. }
  201. });
  202. console.log("【Debug】站点信息发送完毕");
  203. }
  204.  
  205. async function updataCookie(data){
  206. data = "site_id=" + data.site_id + "&site_cookie=" + encodeURIComponent(data.site_cookie);
  207. // data = JSON.stringify(data);
  208. GM_xmlhttpRequest({
  209. method: "POST",
  210. url: nastoolurl + cookieUpdateApi,
  211. headers : {
  212. 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
  213. 'Origin': nastoolurl,
  214. "Authorization":token,
  215. },
  216. data: data,
  217. onload: function (response) {
  218. console.log("【Debug】nas-tools请求发送成功");
  219. if (JSON.parse(response.responseText).code !== 0){
  220. console.log("【Debug】更新站点cookie失败,可能是API返回变更");
  221. }
  222. },
  223. onerror: function (response) {
  224. console.log("nas-tools更新站点cookie请求失败");
  225. }
  226. });
  227. console.log("【Debug】站点信息发送完毕");
  228. }
  229.  
  230. function checkUrlExists(url) {
  231. var cookies = document.cookie;
  232. return new Promise((resolve, reject) => {
  233. GM_xmlhttpRequest({
  234. method: "HEAD",
  235. url: url,
  236. headers: {
  237. "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36",
  238. "Cookie": cookies
  239. },
  240. onload: function(response) {
  241. if (response.status === 200) {
  242. resolve(true); // URL 存在
  243. } else {
  244. console.log(url+"响应:"+response.status)
  245. resolve(false); // URL 不存在
  246. }
  247. },
  248. onerror: function(error) {
  249. resolve(false); // 请求错误,URL 不存在
  250. }
  251. });
  252. });
  253. }
  254.  
  255. async function getData() {
  256. console.log("【Debug】开始获取PT站点信息");
  257. var data = {};
  258. var ptCookie = document.cookie;
  259. if (tampermonkeyVersion == "BETA") {
  260. GM_cookie('list', { // 异步,如果在return data之前还没执行完,部分站点会导致cookie不全。
  261. url: location.href
  262. }, (cookies) => {
  263. if(cookies!=undefined){
  264. ptCookie = cookies.map(c => `${c.name}=${c.value}`).join('; ');
  265. console.log('【Debug】cookie:', ptCookie);
  266. // data.site_cookie = ptCookie;
  267. }
  268. });
  269. }
  270. // var ptUrl = document.URL;
  271. var parser = document.createElement('a')
  272. parser.href = document.URL
  273. var ptUrl = "https://"+parser.hostname
  274. // var ptUrl = window.location.hostname;
  275. // console.log("host: ",window.location.host);
  276. // console.log("hostname: ",window.location.hostname);
  277. if (ptUrl.length > 150){
  278. console.log("【Debug】可能是cf盾,进入首页后再刷新一下页面");
  279. return false;
  280. }
  281. // var ptTitle = (document.title + "").split('::')[0];
  282. // const pattern = /[`~!@#$^\-&*()=|{}':;'\\\[\]\.<>\/?~!@#¥……&*()——|{}【】';:""'。,、?\s]/g; // 去除( "等
  283. // ptTitle = ptTitle.replace(pattern, "").substr(0,15);
  284. var ptTitle = parser.hostname
  285. if (ptCookie == "" && tampermonkeyVersion!="BETA"){
  286. console.log("【Debug】获取的cookie为空,请使用BETA版油猴,退出脚本");
  287. return false
  288. }
  289. // 默认配置
  290. data.site_id = "";
  291. data.site_name = ptTitle;
  292. data.site_pri = my_site_pri;
  293. data.site_rssurl = "";
  294. //data.site_signurl = ptUrl;
  295. data.site_cookie = ptCookie;
  296. data.site_include = my_site_include;
  297. data.site_note = my_site_note;
  298.  
  299. console.log("host_url:"+ host_url);
  300. var sign_url = "/attendance.php"
  301.  
  302. var exists = await checkUrlExists(sign_url);
  303. if (exists) {
  304. console.log(sign_url+ " 存在");
  305. sign_url = ptUrl+"/attendance.php"
  306. } else {
  307. console.log(sign_url+ " 不存在");
  308. sign_url = ptUrl;
  309. }
  310. console.log("sign_url:"+ sign_url);
  311. data.site_signurl = sign_url;
  312.  
  313.  
  314. var user_sites = await getUserSitesByApi();
  315. if (user_sites == ""){ return data } // 第一次使用,一个站点都没有
  316. var host_url = ptUrl.substring(8,).replace("/index.php",""); // 解决因index不是签到url导致nas-tools签到失败
  317.  
  318.  
  319. for (var i = 0, l = user_sites.length; i < l; i++) {
  320. if (user_sites[i].signurl.search(sign_url)!==-1) {
  321. var temp = user_sites[i]
  322. console.log("【Debug】查询到nas-tools中该站点的信息:",JSON.stringify(temp));
  323. if(temp.cookie == ptCookie){
  324. console.log("【Debug】cookie未过期,退出脚本");
  325. return false;
  326. }
  327. data = {};
  328. data.site_id = temp.id;
  329. data.site_cookie = ptCookie;
  330. data.site_signurl = sign_url;
  331. //data.site_id = temp.id;
  332. //data.site_name = temp.name;
  333. //data.site_pri = temp.pri;
  334. //data.site_rssurl = temp.rssurl; // rss中存在&导致后面把json转换字符串在编码时候出现分割,先encode一下,但是后面在encode时候会出错
  335. //data.site_signurl = temp.signurl;
  336. //data.site_cookie = ptCookie;
  337. //data.site_include = ""
  338. //if(temp.signin_enable == true){data.site_include += "Q";} // 签到
  339. //if(temp.rss_enable == true){data.site_include += "D";} // rss
  340. //if(temp.brush_enable == true){data.site_include += "S";} // 刷流
  341. //if(temp.statistic_enable == true){data.site_include += "T";} // 统计
  342. //data.site_note = {};
  343. //data.site_note.rule = temp.rule;
  344. //data.site_note.parse = temp.parse;
  345. //data.site_note.ua = temp.ua;
  346. //temp.chrome == true ? data.site_note.chrome = "Y" :data.site_note.chrome = "N";
  347. //temp.proxy == true ? data.site_note.proxy = "Y" :data.site_note.proxy = "N";
  348. //temp.message == true ? data.site_note.message = "Y" :data.site_note.message = "N";
  349. // temp.subtitle == true ? data.site_note.subtitle = "Y" :data.site_note.subtitle = "N"
  350. // data.site_note.download_setting = temp.download_setting
  351. updataCookie(data);
  352. return false;
  353. }
  354. }
  355. return data;
  356. }
  357.  
  358. async function main() {
  359. var data = await getData();
  360. if(data==false) return ;
  361. // await ajax_post("update_site",data,"")
  362. await sendSiteToNastools(data);
  363. }
  364.  
  365.  
  366. async function ajax_post(cmd, data, handler){
  367. // @require https://lib.baomitu.com/jquery/3.6.0/jquery.min.js
  368. data = {
  369. cmd: cmd,
  370. data: JSON.stringify(data)
  371. };
  372. $$.ajax({ // (已屏蔽:mixed-content) 无法在https里面发生http请求,因此暂时不使用
  373. type: "POST",
  374. url: nastoolurl + dorandom,
  375. dataType: "json",
  376. data: data,
  377. cache: false,
  378. timeout: 0,
  379. success: "", // 待更新
  380. error: function(){ // 待更新
  381. }
  382. });
  383. }