douyin-user-data-download

下载抖音用户主页数据!

目前為 2024-06-14 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name douyin-user-data-download
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.4.2
  5. // @description 下载抖音用户主页数据!
  6. // @author xxmdmst
  7. // @match https://www.douyin.com/*
  8. // @icon https://xxmdmst.oss-cn-beijing.aliyuncs.com/imgs/favicon.ico
  9. // @grant GM_registerMenuCommand
  10. // @grant GM_setValue
  11. // @grant GM_getValue
  12. // @require https://cdnjs.cloudflare.com/ajax/libs/jszip/3.6.0/jszip.min.js
  13. // @license MIT
  14. // ==/UserScript==
  15.  
  16. (function () {
  17. let localDownloadUrl = GM_getValue("localDownloadUrl", 'http://localhost:8080/data');
  18. const startPipeline = (start) => {
  19. if (confirm(start ? "是否开启本地下载通道?\n开启后会向本地服务发送数据,服务地址:\n" + localDownloadUrl : "是否关闭本地下载通道?")) {
  20. GM_setValue("localDownload", start);
  21. window.location.reload();
  22. }
  23. }
  24. let localDownload = GM_getValue("localDownload", false);
  25. if (localDownload) {
  26. GM_registerMenuCommand("✅关闭上报本地通道", () => {
  27. startPipeline(false);
  28. })
  29. } else {
  30. GM_registerMenuCommand("⛔️开启上报本地通道", () => {
  31. startPipeline(true);
  32. })
  33. }
  34.  
  35. GM_registerMenuCommand("♐设置本地上报地址", () => {
  36. localDownloadUrl = GM_getValue("localDownloadUrl", 'http://localhost:8080/data');
  37. let newlocalDownloadUrl = prompt("请输入新的上报地址:", localDownloadUrl);
  38. if (newlocalDownloadUrl === null) {
  39. return;
  40. } else if (!newlocalDownloadUrl.trim()) {
  41. newlocalDownloadUrl = "http://localhost:8080/data";
  42. alert("设置了空白地址,已经恢复默认地址为:" + newlocalDownloadUrl);
  43. localDownloadUrl = newlocalDownloadUrl;
  44. } else {
  45. GM_setValue("localDownloadUrl", newlocalDownloadUrl);
  46. alert("当前上报地址已经修改为:" + newlocalDownloadUrl);
  47. }
  48. GM_setValue("localDownloadUrl", newlocalDownloadUrl);
  49. localDownloadUrl = newlocalDownloadUrl;
  50. });
  51. let table;
  52.  
  53. function initGbkTable() {
  54. // https://en.wikipedia.org/wiki/GBK_(character_encoding)#Encoding
  55. const ranges = [
  56. [0xA1, 0xA9, 0xA1, 0xFE],
  57. [0xB0, 0xF7, 0xA1, 0xFE],
  58. [0x81, 0xA0, 0x40, 0xFE],
  59. [0xAA, 0xFE, 0x40, 0xA0],
  60. [0xA8, 0xA9, 0x40, 0xA0],
  61. [0xAA, 0xAF, 0xA1, 0xFE],
  62. [0xF8, 0xFE, 0xA1, 0xFE],
  63. [0xA1, 0xA7, 0x40, 0xA0],
  64. ];
  65. const codes = new Uint16Array(23940);
  66. let i = 0;
  67.  
  68. for (const [b1Begin, b1End, b2Begin, b2End] of ranges) {
  69. for (let b2 = b2Begin; b2 <= b2End; b2++) {
  70. if (b2 !== 0x7F) {
  71. for (let b1 = b1Begin; b1 <= b1End; b1++) {
  72. codes[i++] = b2 << 8 | b1
  73. }
  74. }
  75. }
  76. }
  77. table = new Uint16Array(65536);
  78. table.fill(0xFFFF);
  79. const str = new TextDecoder('gbk').decode(codes);
  80. for (let i = 0; i < str.length; i++) {
  81. table[str.charCodeAt(i)] = codes[i]
  82. }
  83. }
  84.  
  85. function str2gbk(str, opt = {}) {
  86. if (!table) {
  87. initGbkTable()
  88. }
  89. const NodeJsBufAlloc = typeof Buffer === 'function' && Buffer.allocUnsafe;
  90. const defaultOnAlloc = NodeJsBufAlloc
  91. ? (len) => NodeJsBufAlloc(len)
  92. : (len) => new Uint8Array(len);
  93. const defaultOnError = () => 63;
  94. const onAlloc = opt.onAlloc || defaultOnAlloc;
  95. const onError = opt.onError || defaultOnError;
  96.  
  97. const buf = onAlloc(str.length * 2);
  98. let n = 0;
  99.  
  100. for (let i = 0; i < str.length; i++) {
  101. const code = str.charCodeAt(i);
  102. if (code < 0x80) {
  103. buf[n++] = code;
  104. continue
  105. }
  106. const gbk = table[code];
  107.  
  108. if (gbk !== 0xFFFF) {
  109. buf[n++] = gbk;
  110. buf[n++] = gbk >> 8
  111. } else if (code === 8364) {
  112. buf[n++] = 0x80
  113. } else {
  114. const ret = onError(i, str);
  115. if (ret === -1) {
  116. break
  117. }
  118. if (ret > 0xFF) {
  119. buf[n++] = ret;
  120. buf[n++] = ret >> 8
  121. } else {
  122. buf[n++] = ret
  123. }
  124. }
  125. }
  126. return buf.subarray(0, n)
  127. }
  128.  
  129. function formatSeconds(seconds) {
  130. const timeUnits = ['小时', '分', '秒'];
  131. const timeValues = [
  132. Math.floor(seconds / 3600),
  133. Math.floor((seconds % 3600) / 60),
  134. seconds % 60
  135. ];
  136. return timeValues.map((value, index) => value > 0 ? value + timeUnits[index] : '').join('');
  137. }
  138.  
  139. const timeFormat = (timestamp = null, fmt = 'yyyy-mm-dd') => {
  140. // 其他更多是格式化有如下:
  141. // yyyy:mm:dd|yyyy:mm|yyyy年mm月dd日|yyyy年mm月dd日 hh时MM分等,可自定义组合
  142. timestamp = parseInt(timestamp);
  143. // 如果为null,则格式化当前时间
  144. if (!timestamp) timestamp = Number(new Date());
  145. // 判断用户输入的时间戳是秒还是毫秒,一般前端js获取的时间戳是毫秒(13位),后端传过来的为秒(10位)
  146. if (timestamp.toString().length === 10) timestamp *= 1000;
  147. let date = new Date(timestamp);
  148. let ret;
  149. let opt = {
  150. "y{4,}": date.getFullYear().toString(), // 年
  151. "y+": date.getFullYear().toString().slice(2,), // 年
  152. "m+": (date.getMonth() + 1).toString(), // 月
  153. "d+": date.getDate().toString(), // 日
  154. "h+": date.getHours().toString(), // 时
  155. "M+": date.getMinutes().toString(), // 分
  156. "s+": date.getSeconds().toString() // 秒
  157. // 有其他格式化字符需求可以继续添加,必须转化成字符串
  158. };
  159. for (let k in opt) {
  160. ret = new RegExp("(" + k + ")").exec(fmt);
  161. if (ret) {
  162. fmt = fmt.replace(ret[1], (ret[1].length === 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
  163. }
  164. }
  165. return fmt
  166. };
  167. let aweme_list = [];
  168. let userKey = [
  169. "昵称", "关注", "粉丝", "获赞",
  170. "抖音号", "IP属地", "性别",
  171. "位置", "签名", "作品数", "主页"
  172. ];
  173. let userData = [];
  174. let createEachButtonTimer;
  175.  
  176. function copyText(text, node) {
  177. let oldText = node.textContent;
  178. navigator.clipboard.writeText(text).then(r => {
  179. node.textContent = "复制成功";
  180. }).catch((e) => {
  181. node.textContent = "复制失败";
  182. })
  183. setTimeout(() => node.textContent = oldText, 2000);
  184. }
  185.  
  186. function copyUserData(node) {
  187. if (userData.length === 0) {
  188. alert("没有捕获到用户数据!");
  189. return;
  190. }
  191. let text = [];
  192. for (let i = 0; i < userKey.length; i++) {
  193. let key = userKey[i];
  194. let value = userData[userData.length - 1][i];
  195. if (value) text.push(key + ":" + value.toString().trim());
  196. }
  197. copyText(text.join("\n"), node);
  198. }
  199.  
  200. function createVideoButton(text, top, func) {
  201. const button = document.createElement("button");
  202. button.textContent = text;
  203. button.style.position = "absolute";
  204. button.style.right = "0px";
  205. button.style.top = top;
  206. button.style.opacity = "0.5";
  207. if (func) {
  208. button.addEventListener("click", (event) => {
  209. event.preventDefault();
  210. event.stopPropagation();
  211. func();
  212. });
  213. }
  214. return button;
  215. }
  216.  
  217. function createDownloadLink(blob, filename, ext, prefix = "") {
  218. if (filename === null) {
  219. filename = userData.length > 0 ? userData[userData.length - 1][0] : document.title;
  220. }
  221. const url = URL.createObjectURL(blob);
  222. const link = document.createElement('a');
  223. link.href = url;
  224. link.download = prefix + filename.replace(/[\/:*?"<>|\s]/g, "").slice(0, 40) + "." + ext;
  225. link.click();
  226. URL.revokeObjectURL(url);
  227. }
  228.  
  229. function txt2file(txt, filename, ext) {
  230. createDownloadLink(new Blob([txt], {type: 'text/plain'}), filename, ext);
  231. }
  232.  
  233. function getAwemeName(aweme) {
  234. let name = aweme.item_title ? aweme.item_title : aweme.caption;
  235. if (!name) name = aweme.desc ? aweme.desc : aweme.awemeId;
  236. return `【${aweme.date.slice(0, 10)}】` + name.replace(/[\/:*?"<>|\s]+/g, "").slice(0, 27).replace(/\.\d+$/g, "");
  237. }
  238.  
  239. function createEachButton() {
  240. let targetNodes = document.querySelectorAll("div[data-e2e='user-post-list'] > ul[data-e2e='scroll-list'] > li a");
  241. for (let i = 0; i < targetNodes.length; i++) {
  242. let targetNode = targetNodes[i];
  243. if (targetNode.dataset.added) {
  244. continue;
  245. }
  246. let aweme = aweme_list[i];
  247. let copyDescButton = createVideoButton("复制描述", "0px");
  248. copyDescButton.addEventListener("click", (event) => {
  249. event.preventDefault();
  250. event.stopPropagation();
  251. copyText(aweme.desc, copyDescButton);
  252. })
  253. targetNode.appendChild(copyDescButton);
  254. targetNode.appendChild(createVideoButton("打开视频源", "20px", () => window.open(aweme.url)));
  255. let downloadVideoButton = createVideoButton("下载视频", "40px", () => {
  256. let xhr = new XMLHttpRequest();
  257. xhr.open('GET', aweme.url.replace("http://", "https://"), true);
  258. xhr.responseType = 'blob';
  259. xhr.onload = (e) => {
  260. createDownloadLink(xhr.response, getAwemeName(aweme), (aweme.images ? "mp3" : "mp4"));
  261. };
  262. xhr.onprogress = (event) => {
  263. if (event.lengthComputable) {
  264. downloadVideoButton.textContent = "下载" + (event.loaded * 100 / event.total).toFixed(1) + '%';
  265. }
  266. };
  267. xhr.send();
  268. });
  269. targetNode.appendChild(downloadVideoButton);
  270. if (aweme.images) {
  271. let downloadImageButton = createVideoButton("图片打包下载", "60px", () => {
  272. const zip = new JSZip();
  273. downloadImageButton.textContent = "图片下载并打包中...";
  274. const promises = aweme.images.map((link, index) => {
  275. return fetch(link)
  276. .then((response) => response.arrayBuffer())
  277. .then((buffer) => {
  278. downloadImageButton.textContent = `图片已下载【${index + 1}/${aweme.images.length}】`;
  279. zip.file(`image_${index + 1}.jpg`, buffer);
  280. });
  281. });
  282. Promise.all(promises)
  283. .then(() => {
  284. return zip.generateAsync({type: "blob"});
  285. })
  286. .then((content) => {
  287. createDownloadLink(content, getAwemeName(aweme), "zip", "【图文】");
  288. downloadImageButton.textContent = "图文打包完成";
  289. });
  290. });
  291. targetNode.appendChild(downloadImageButton);
  292. }
  293. targetNode.dataset.added = "true";
  294. }
  295. }
  296.  
  297. function flush() {
  298. if (createEachButtonTimer !== undefined) {
  299. clearTimeout(createEachButtonTimer);
  300. createEachButtonTimer = undefined;
  301. }
  302. createEachButtonTimer = setTimeout(createEachButton, 500);
  303. data_button.p2.textContent = `${aweme_list.length}`;
  304. let img_num = aweme_list.filter(a => a.images).length;
  305. img_button.p2.textContent = `${img_num}`;
  306. msg_pre.textContent = `已加载${aweme_list.length}个作品,${img_num}个图文\n激活上方头像可展开下载按钮`;
  307. }
  308.  
  309. let flag = false;
  310.  
  311. function formatJsonData(json_data) {
  312. return json_data.aweme_list.map(item => Object.assign(
  313. {
  314. "awemeId": item.aweme_id,
  315. "item_title": item.item_title,
  316. "caption": item.caption,
  317. "desc": item.desc,
  318. "tag": item.text_extra.map(tag => tag.hashtag_name).filter(tag => tag).join("#"),
  319. "video_tag": item.video_tag.map(tag => tag.tag_name).filter(tag => tag).join("->")
  320. },
  321. {
  322. "diggCount": item.statistics.digg_count,
  323. "commentCount": item.statistics.comment_count,
  324. "collectCount": item.statistics.collect_count,
  325. "shareCount": item.statistics.share_count
  326. },
  327. {
  328. "date": timeFormat(item.create_time, "yyyy-mm-dd hh:MM:ss"),
  329. "duration": formatSeconds(Math.round(item.video.duration / 1000)),
  330. "url": item.video.play_addr.url_list[0],
  331. "cover": item.video.cover.url_list[0],
  332. "images": item.images ? item.images.map(row => row.url_list.pop()) : null,
  333. "uid": item.author.uid,
  334. "nickname": item.author.nickname
  335. }
  336. ));
  337. }
  338.  
  339. function sendLocalData(jsonData) {
  340. if (!localDownload) return;
  341. fetch(localDownloadUrl, {
  342. method: 'POST',
  343. headers: {
  344. 'Content-Type': 'application/json'
  345. },
  346. body: JSON.stringify(jsonData)
  347. })
  348. .then(response => response.json())
  349. .then(responseData => {
  350. console.log('成功:', responseData);
  351. })
  352. .catch(error => {
  353. console.log('上报失败,请检查本地程序是否已经启动!');
  354. });
  355. }
  356.  
  357. function interceptResponse() {
  358. const originalSend = XMLHttpRequest.prototype.send;
  359. XMLHttpRequest.prototype.send = function () {
  360. const self = this;
  361. this.onreadystatechange = function () {
  362. if (self.readyState === 4 && self._url) {
  363. if (self._url.indexOf("/aweme/v1/web/aweme/post") > -1) {
  364. let jsonData = formatJsonData(JSON.parse(self.response));
  365. aweme_list.push(...jsonData);
  366. if (domLoadedTimer === null) {
  367. flush();
  368. } else {
  369. flag = true;
  370. }
  371. sendLocalData(jsonData);
  372. } else if (self._url.indexOf("/aweme/v1/web/user/profile/other") > -1) {
  373. let userInfo = JSON.parse(self.response).user;
  374. for (let key in userInfo) {
  375. if (!userInfo[key]) userInfo[key] = "";
  376. }
  377. if (userInfo.district) userInfo.city += "·" + userInfo.district;
  378. userInfo.unique_id = '\t' + (userInfo.unique_id ? userInfo.unique_id : userInfo.short_id);
  379. userData.push([
  380. userInfo.nickname, userInfo.following_count, userInfo.mplatform_followers_count,
  381. userInfo.total_favorited, userInfo.unique_id, userInfo.ip_location.replace("IP属地:", ""),
  382. userInfo.gender === 2 ? "女" : "男",
  383. userInfo.city, '"' + userInfo.signature + '"', userInfo.aweme_count, "https://www.douyin.com/user/" + userInfo.sec_uid
  384. ]);
  385. }
  386. }
  387. };
  388. originalSend.apply(this, arguments);
  389. };
  390. }
  391.  
  392.  
  393. function downloadData(node, encoding) {
  394. if (aweme_list.length === 0) {
  395. alert("还没有发现作品数据,请进入https://www.douyin.com/user/开头的链接刷新网页后重试!");
  396. return;
  397. }
  398. if (node.disabled) {
  399. alert("下载正在处理中,请不要重复点击按钮!");
  400. return;
  401. }
  402. node.disabled = true;
  403. try {
  404. // if (userData.length > 0) {
  405. // text += userKey.join(",") + "\n";
  406. // text += userData.map(row => row.join(",")).join("\n") + "\n\n";
  407. // }
  408. let text = "作品描述,作品链接,点赞数,评论数,收藏数,分享数,发布时间,时长,标签,分类,封面,下载链接\n";
  409. aweme_list.forEach(aweme => {
  410. text += ['"' + aweme.desc.replace(/,/g, ',').replace(/"/g, '""') + '"',
  411. "https://www.douyin.com/video/" + aweme.awemeId,
  412. aweme.diggCount, aweme.commentCount,
  413. aweme.collectCount, aweme.shareCount, aweme.date,
  414. aweme.duration, aweme.tag, aweme.video_tag,
  415. aweme.cover, aweme.url].join(",") + "\n"
  416. });
  417. if (encoding === "gbk") {
  418. text = str2gbk(text);
  419. }
  420. txt2file(text, null, "csv");
  421. } finally {
  422. node.disabled = false;
  423. }
  424. }
  425.  
  426. let img_button, data_button, msg_pre;
  427.  
  428. function createMsgBox() {
  429. msg_pre = document.createElement('pre');
  430. msg_pre.textContent = '等待上方头像加载完毕';
  431. msg_pre.style.color = 'white';
  432. msg_pre.style.position = 'fixed';
  433. msg_pre.style.right = '5px';
  434. msg_pre.style.top = '60px';
  435. msg_pre.style.color = 'white';
  436. msg_pre.style.zIndex = '90000';
  437. msg_pre.style.opacity = "0.5";
  438. document.body.appendChild(msg_pre);
  439. }
  440.  
  441. function scrollPageToBottom(scroll_button) {
  442. let scrollInterval;
  443.  
  444. function scrollLoop() {
  445. let endText = document.querySelector("div[data-e2e='user-post-list'] > ul[data-e2e='scroll-list'] + div div").innerText;
  446. if (endText || (userData.length > 0 && aweme_list.length > userData[userData.length - 1][9] - 5)) {
  447. clearInterval(scrollInterval);
  448. scrollInterval = null;
  449. scroll_button.p1.textContent = "已加载全部!";
  450. } else {
  451. scrollTo(0, document.body.scrollHeight);
  452. }
  453. }
  454.  
  455. scroll_button.addEventListener('click', () => {
  456. if (!scrollInterval) {
  457. scrollInterval = setInterval(scrollLoop, 1200);
  458. scroll_button.p1.textContent = "停止自动下拉";
  459. } else {
  460. clearInterval(scrollInterval);
  461. scrollInterval = null;
  462. scroll_button.p1.textContent = "开启自动下拉";
  463. }
  464. });
  465. }
  466.  
  467. function createAllButton() {
  468. let dom = document.querySelector("#douyin-header-menuCt pace-island > div > div:nth-last-child(1) ul a:nth-last-child(1)");
  469. let baseNode = dom.cloneNode(true);
  470. baseNode.removeAttribute("target");
  471. baseNode.removeAttribute("rel");
  472. baseNode.removeAttribute("href");
  473. let svgChild = baseNode.querySelector("svg");
  474. if (svgChild) baseNode.removeChild(svgChild);
  475.  
  476. function createNewButton(name, num = "0") {
  477. let button = baseNode.cloneNode(true);
  478. button.p1 = button.querySelector("p:nth-child(1)");
  479. button.p2 = button.querySelector("p:nth-child(2)");
  480. button.p1.textContent = name;
  481. button.p2.textContent = num;
  482. dom.after(button);
  483. return button;
  484. }
  485.  
  486. function createCommonElement(tagName, attrs = {}, text = "") {
  487. const tag = document.createElement(tagName);
  488. for (const [k, v] of Object.entries(attrs)) {
  489. tag.setAttribute(k, v);
  490. }
  491. if (text) tag.textContent = text;
  492. tag.addEventListener('click', (event) => event.stopPropagation());
  493. return tag;
  494. }
  495.  
  496. img_button = createNewButton("图文打包下载");
  497. img_button.addEventListener('click', () => downloadImg(img_button));
  498.  
  499. let downloadCoverButton = createNewButton("封面打包下载", "");
  500. downloadCoverButton.addEventListener('click', () => downloadCover(downloadCoverButton));
  501.  
  502. data_button = createNewButton("下载已加载的数据");
  503. data_button.p1.after(createCommonElement("label", {'for': 'gbk'}, 'gbk'));
  504. let checkbox = createCommonElement("input", {'type': 'checkbox', 'id': 'gbk'});
  505. checkbox.checked = localStorage.getItem("gbk") === "1";
  506. checkbox.onclick = (event) => {
  507. event.stopPropagation();
  508. localStorage.setItem("gbk", checkbox.checked ? "1" : "0");
  509. };
  510. data_button.p1.after(checkbox);
  511. data_button.addEventListener('click', () => downloadData(data_button, checkbox.checked ? "gbk" : "utf-8"));
  512.  
  513. scrollPageToBottom(createNewButton("开启自动下拉到底", ""));
  514.  
  515. let share_button = document.querySelector("#frame-user-info-share-button");
  516. if (share_button) {
  517. let node = share_button.cloneNode(true);
  518. node.span = node.querySelector("span");
  519. node.span.innerHTML = "复制作者信息";
  520. node.addEventListener('click', () => copyUserData(node.span));
  521. share_button.after(node);
  522. }
  523. }
  524.  
  525. async function downloadCover(node) {
  526. if (aweme_list.length === 0) {
  527. alert("还没有发现任何作品数据,请进入https://www.douyin.com/user/开头的链接刷新网页后重试!");
  528. return;
  529. }
  530. if (node.disabled) {
  531. alert("下载正在处理中,请不要重复点击按钮!");
  532. return;
  533. }
  534. node.disabled = true;
  535. try {
  536. const zip = new JSZip();
  537. msg_pre.textContent = `下载封面并打包中...`;
  538. let promises = aweme_list.map((aweme, index) => {
  539. let awemeName = getAwemeName(aweme) + ".jpg";
  540. return fetch(aweme.cover)
  541. .then(response => response.arrayBuffer())
  542. .then(buffer => zip.file(awemeName, buffer))
  543. .then(() => msg_pre.textContent = `${index + 1}/${aweme_list.length} ` + awemeName)
  544. });
  545. Promise.all(promises).then(() => {
  546. return zip.generateAsync({type: "blob"})
  547. }).then((content) => {
  548. createDownloadLink(content, null, "zip", "【封面】");
  549. msg_pre.textContent = "封面打包完成";
  550. node.disabled = false;
  551. })
  552. } finally {
  553. node.disabled = false;
  554. }
  555. }
  556.  
  557. async function downloadImg(node) {
  558. if (node.disabled) {
  559. alert("下载正在处理中,请不要重复点击按钮!");
  560. return;
  561. }
  562. node.disabled = true;
  563. try {
  564. const zip = new JSZip();
  565. let flag = true;
  566. let aweme_img_list = aweme_list.filter(a => a.images);
  567. for (let [i, aweme] of aweme_img_list.entries()) {
  568. let awemeName = getAwemeName(aweme);
  569. msg_pre.textContent = `${i + 1}/${aweme_img_list.length} ` + awemeName;
  570. let folder = zip.folder(awemeName);
  571. await Promise.all(aweme.images.map((link, index) => {
  572. return fetch(link)
  573. .then((res) => res.arrayBuffer())
  574. .then((buffer) => {
  575. folder.file(`image_${index + 1}.jpg`, buffer);
  576. });
  577. }));
  578. flag = false;
  579. }
  580. if (flag) {
  581. alert("当前页面未发现图文链接");
  582. node.disabled = false;
  583. return;
  584. }
  585. msg_pre.textContent = "图文打包中...";
  586. zip.generateAsync({type: "blob"})
  587. .then((content) => {
  588. createDownloadLink(content, null, "zip", "【图文】");
  589. msg_pre.textContent = "图文打包完成";
  590. node.disabled = false;
  591. });
  592. } finally {
  593. node.disabled = false;
  594. }
  595.  
  596. }
  597.  
  598. function douyinVideoDownloader() {
  599. function run() {
  600. let downloadOption = [{name: '打开视频源', id: 'toLink'}];
  601. let videoElements = document.querySelectorAll('video');
  602. if (videoElements.length === 0) return;
  603. //把自动播放的video标签选择出来
  604. let playVideoElements = [];
  605. videoElements.forEach(function (element) {
  606. let autoplay = element.getAttribute('autoplay');
  607. if (autoplay !== null) {
  608. playVideoElements.push(element);
  609. }
  610. })
  611. let videoContainer = location.href.indexOf('modal_id') !== -1
  612. ? playVideoElements[0]
  613. : playVideoElements[playVideoElements.length - 1];
  614. if (!videoContainer) return;
  615. //获取视频播放地址
  616. let url = videoContainer && videoContainer.children.length > 0 && videoContainer.children[0].src
  617. ? videoContainer.children[0].src
  618. : videoContainer.src;
  619. //获取视频ID,配合自定义id使用
  620. let videoId;
  621. let resp = url.match(/^(https:)?\/\/.+\.com\/([a-zA-Z0-9]+)\/[a-zA-Z0-9]+\/video/);
  622. let res = url.match(/blob:https:\/\/www.douyin.com\/(.*)/);
  623. if (resp && resp[2]) {
  624. videoId = resp[2];
  625. } else if (res && res[1]) {
  626. videoId = res[1]
  627. } else {
  628. videoId = videoContainer.getAttribute('data-xgplayerid')
  629. }
  630. let playContainer = videoContainer.parentNode.parentNode.querySelector('.xg-right-grid');
  631. if (!playContainer) return;
  632. //在对主页就行视频浏览时会出现多个按钮,删除不需要的,只保留当前对应的
  633. let videoDownloadDom = playContainer.querySelector('#scriptVideoDownload' + videoId);
  634. if (videoDownloadDom) {
  635. let dom = playContainer.querySelectorAll('.xgplayer-playclarity-setting');
  636. dom.forEach(function (d) {
  637. let btn = d.querySelector('.btn');
  638. if (d.id !== 'scriptVideoDownload' + videoId && btn.innerText === '下载') {
  639. d.parentNode.removeChild(d);
  640. }
  641. });
  642. return;
  643. }
  644. if (videoContainer && playContainer) {
  645. let playClarityDom = playContainer.querySelector('.xgplayer-playclarity-setting');
  646. if (!playClarityDom) return;
  647.  
  648. let palyClarityBtn = playClarityDom.querySelector('.btn');
  649. if (!palyClarityBtn) return;
  650.  
  651. let downloadDom = playClarityDom.cloneNode(true);
  652. downloadDom.setAttribute('id', 'scriptVideoDownload' + videoId);
  653.  
  654. if (location.href.indexOf('search') === -1) {
  655. downloadDom.style = 'margin-top:-68px;padding-top:100px;padding-left:20px;padding-right:20px;';
  656. } else {
  657. downloadDom.style = 'margin-top:0px;padding-top:100px;';
  658. }
  659.  
  660. let downloadText = downloadDom.querySelector('.btn');
  661. downloadText.innerText = '下载';
  662. downloadText.style = 'font-size:14px;font-weight:600;';
  663. downloadText.setAttribute('id', 'zhmDouyinDownload' + videoId);
  664. let detail = playContainer.querySelector('xg-icon:nth-of-type(1)').children[0];
  665. let linkUrl = detail.getAttribute('href') ? detail.getAttribute('href') : location.href;
  666.  
  667. if (linkUrl.indexOf('www.douyin.com') === -1) {
  668. linkUrl = '//www.douyin.com' + linkUrl;
  669. }
  670.  
  671. downloadText.setAttribute('data-url', linkUrl);
  672. downloadText.removeAttribute('target');
  673. downloadText.setAttribute('href', 'javascript:void(0);');
  674.  
  675. let virtualDom = downloadDom.querySelector('.virtual');
  676. downloadDom.onmouseover = function () {
  677. if (location.href.indexOf('search') === -1) {
  678. virtualDom.style = 'display:block !important';
  679. } else {
  680. virtualDom.style = 'display:block !important;margin-bottom:37px;';
  681. }
  682. }
  683.  
  684. downloadDom.onmouseout = function () {
  685. virtualDom.style = 'display:none !important';
  686. }
  687.  
  688. let downloadHtml = '';
  689. downloadOption.forEach(function (item) {
  690. if (item.id === "toLink") {
  691. downloadHtml += `<div style="text-align:center;" class="item ${item.id}" id="${item.id}${videoId}">${item.name}</div>`;
  692. }
  693. })
  694. if (downloadDom.querySelector('.virtual')) {
  695. downloadDom.querySelector('.virtual').innerHTML = downloadHtml;
  696. }
  697. playClarityDom.after(downloadDom);
  698. //直接打开
  699. let toLinkDom = playContainer.querySelector('#toLink' + videoId);
  700. if (toLinkDom) {
  701. toLinkDom.addEventListener('click', function () {
  702. if (url.match(/^blob/)) {
  703. alert("加密视频地址,无法直接打开");
  704. } else {
  705. window.open(url);
  706. }
  707. })
  708. }
  709. }
  710. }
  711.  
  712. setInterval(run, 500);
  713. }
  714.  
  715. if (document.title === "验证码中间页") {
  716. return
  717. }
  718. createMsgBox();
  719. interceptResponse();
  720. douyinVideoDownloader();
  721. let domLoadedTimer;
  722. const checkElementLoaded = () => {
  723. const element = document.querySelector('#douyin-header-menuCt pace-island > div > div:nth-last-child(1) ul a');
  724. if (element) {
  725. console.log('顶部栏加载完毕');
  726. msg_pre.textContent = "头像加载完成\n若需要下载用户数据,需进入目标用户主页";
  727. clearInterval(domLoadedTimer);
  728. domLoadedTimer = null;
  729. createAllButton();
  730. // scrollPageToBottom();
  731. if (flag) flush();
  732. }
  733. };
  734. window.onload = () => {
  735. domLoadedTimer = setInterval(checkElementLoaded, 700);
  736. }
  737. })();