IG小助手

一键下载对方 Instagram 帖子中的相片、视频甚至是他们的快拍、Reels及头像图片!

当前为 2023-11-25 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name IG Helper
  3. // @name:zh-TW IG小精靈
  4. // @name:zh-CN IG小助手
  5. // @name:ja IG助手
  6. // @name:ko IG조수
  7. // @namespace https://github.snkms.com/
  8. // @version 2.11.13
  9. // @description Downloading is possible for both photos and videos from posts, as well as for stories, reels or profile picture.
  10. // @description:zh-TW 一鍵下載對方 Instagram 貼文中的相片、影片甚至是他們的限時動態、連續短片及大頭貼圖片!
  11. // @description:zh-CN 一键下载对方 Instagram 帖子中的相片、视频甚至是他们的快拍、Reels及头像图片!
  12. // @description:ja 投稿された写真や動画だけでなく、ストーリーズやリール動画からもダウンロードが可能です。
  13. // @description:ko 게시물에 게시된 사진과 동영상 뿐만 아니라 스토리나 릴스에서도 다운로드가 가능합니다.
  14. // @author SN-Koarashi (5026)
  15. // @match https://*.instagram.com/*
  16. // @grant GM_addStyle
  17. // @grant GM_setValue
  18. // @grant GM_getValue
  19. // @grant GM_xmlhttpRequest
  20. // @grant GM_registerMenuCommand
  21. // @grant GM_getResourceText
  22. // @connect i.instagram.com
  23. // @require https://code.jquery.com/jquery-3.6.3.min.js#sha256-pvPw+upLPUjgMXY0G+8O0xUf+/Im1MZjXxxgOcBQBXU=
  24. // @resource INTERNAL_CSS https://raw.githubusercontent.com/SN-Koarashi/ig-helper/master/style.css
  25. // @supportURL https://github.com/SN-Koarashi/ig-helper/
  26. // @contributionURL https://ko-fi.com/snkoarashi
  27. // @icon https://www.google.com/s2/favicons?domain=www.instagram.com
  28. // @compatible firefox >= 87
  29. // @compatible chrome >= 90
  30. // @compatible edge >= 90
  31. // @license GPL-3.0-only
  32. // ==/UserScript==
  33.  
  34. (function($) {
  35. 'use strict';
  36. // Icon download by https://www.flaticon.com/authors/pixel-perfect
  37.  
  38. /******** USER SETTINGS ********/
  39. // !!! DO NOT CHANGE THIS AREA !!!
  40. // PLEASE CHANGE SETTING WITH MENU
  41. const USER_SETTING = {
  42. 'AUTO_RENAME': (GM_getValue('AUTO_RENAME'))?GM_getValue('AUTO_RENAME'):true,
  43. 'RENAME_SHORTCODE': (GM_getValue('RENAME_SHORTCODE'))?GM_getValue('RENAME_SHORTCODE'):true,
  44. 'DISABLE_VIDEO_LOOPING': (GM_getValue('DISABLE_VIDEO_LOOPING'))?GM_getValue('DISABLE_VIDEO_LOOPING'):false,
  45. 'REDIRECT_RIGHT_CLICK_USER_STORY_PICTURE': (GM_getValue('REDIRECT_RIGHT_CLICK_USER_STORY_PICTURE'))?GM_getValue('REDIRECT_RIGHT_CLICK_USER_STORY_PICTURE'):false,
  46. 'FORCE_FETCH_ALL_RESOURCES': (GM_getValue('FORCE_FETCH_ALL_RESOURCES'))?GM_getValue('FORCE_FETCH_ALL_RESOURCES'):false
  47. };
  48. /*******************************/
  49.  
  50. const checkInterval = 250;
  51. const lang = navigator.language || navigator.userLanguage;
  52. const style = GM_getResourceText("INTERNAL_CSS");
  53.  
  54. GM_addStyle(style);
  55. GM_registerMenuCommand(_i18n('SETTING'), () => {
  56. showSetting();
  57. });
  58.  
  59. var currentURL = location.href;
  60. var currentHeight = $(document).height();
  61. var firstStarted = false;
  62. var pageLoaded = false;
  63.  
  64. var GL_postPath;
  65. var GL_username;
  66. var GL_repeat;
  67. var GL_dataCache = {
  68. stories: {},
  69. highlights: {}
  70. };
  71.  
  72. // Main Timer
  73. var timer = setInterval(function(){
  74. currentHeight = $(document).height();
  75. // Call Instagram dialog function if url changed.
  76. if(currentURL != location.href || !firstStarted || !pageLoaded){
  77. clearInterval(GL_repeat);
  78. pageLoaded = false;
  79. firstStarted = true;
  80. currentURL = location.href;
  81.  
  82. if(location.href.startsWith("https://www.instagram.com/p/") || location.href.startsWith("https://www.instagram.com/reel/")){
  83. GL_dataCache.stories = {};
  84.  
  85. console.log('isDialog');
  86. setTimeout(()=>{
  87. onReadyMyDW(false);
  88. },150);
  89. pageLoaded = true;
  90. }
  91.  
  92. if(location.href.startsWith("https://www.instagram.com/reels/")){
  93. console.log('isReels');
  94. setTimeout(()=>{
  95. onReels(false);
  96. },150);
  97. pageLoaded = true;
  98. }
  99.  
  100. if(location.href.split("?")[0] == "https://www.instagram.com/"){
  101. GL_dataCache.stories = {};
  102.  
  103. console.log('isHomepage');
  104. setTimeout(()=>{
  105. onReadyMyDW(false);
  106. },150);
  107. pageLoaded = true;
  108. }
  109. if($('div[id^="mount"] > div > div > div').first().is(':hidden') && $('canvas._aarh').length && location.href.match(/^(https:\/\/www\.instagram\.com\/)([0-9A-Za-z\.\-_]+)\/?$/ig) && !location.href.match(/^(https:\/\/www\.instagram\.com\/(stories|explore)\/?)/ig)){
  110. console.log('isProfile');
  111. setTimeout(()=>{
  112. onProfileAvatar(false);
  113. },150);
  114. pageLoaded = true;
  115. }
  116.  
  117. if(!pageLoaded){
  118. // Call Instagram stories function
  119. if(location.href.match(/^(https:\/\/www\.instagram\.com\/stories\/highlights\/)/ig)){
  120. GL_dataCache.highlights = {};
  121.  
  122. console.log('isHighlightsStory');
  123. onHighlightsStory(false);
  124. GL_repeat = setInterval(()=>{
  125. onHighlightsStoryThumbnail(false);
  126. },checkInterval);
  127.  
  128. if($(".IG_DWHISTORY").length) setTimeout(()=>{pageLoaded = true;},150);
  129. }
  130. else if(location.href.match(/^(https:\/\/www\.instagram\.com\/stories\/)/ig)){
  131. console.log('isStory');
  132. onStory(false);
  133. onStoryThumbnail(false);
  134.  
  135. if($(".IG_DWSTORY").length) setTimeout(()=>{pageLoaded = true;},150);
  136. }
  137. else{
  138. pageLoaded = false;
  139. // Remove the download icon
  140. $('.IG_DWSTORY').remove();
  141. $('.IG_DWSTORY_THUMBNAIL').remove();
  142. }
  143. }
  144.  
  145. }
  146. },checkInterval);
  147.  
  148. // Call general function when user scroll the page
  149. $(document).scroll(function(){
  150. if(currentHeight != $(this).height() && location.href.split("?")[0] == "https://www.instagram.com/"){
  151. onReadyMyDW();
  152. }
  153. });
  154.  
  155. /**
  156. * onProfileAvatar
  157. * Trigger user avatar download event or button display event.
  158. *
  159. * @param {Boolean} isDownload - Check if it is a download operation
  160. * @return {void}
  161. */
  162. async function onProfileAvatar(isDownload){
  163. if(isDownload){
  164. let date = new Date().getTime();
  165. let timestamp = Math.floor(date / 1000);
  166. let username = location.href.replace(/\/$/ig,'').split('/').at(-1);
  167. let userInfo = await getUserId(username);
  168. try{
  169. let dataURL = await getUserHighSizeProfile(userInfo.user.pk);
  170. saveFiles(dataURL,username,"avatar",timestamp,'jpg');
  171. }
  172. catch(err){
  173. saveFiles(userInfo.user.profile_pic_url,username,"avatar",timestamp,'jpg');
  174. }
  175. }
  176. else{
  177. // Add the profile download button
  178. if(!$('.IG_DWPROFILE').length){
  179. let profileTimer = setInterval(()=>{
  180. if($('.IG_DWPROFILE').length) clearInterval(profileTimer);
  181.  
  182. $('body > div main canvas._aarh').parent().append(`<div title="${_i18n("DW")}" class="IG_DWPROFILE"><svg width="16" height="16" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Capa_1" x="0px" y="0px" viewBox="0 0 512 512" style="enable-background:new 0 0 512 512;" xml:space="preserve"><g><g><path d="M382.56,233.376C379.968,227.648,374.272,224,368,224h-64V16c0-8.832-7.168-16-16-16h-64c-8.832,0-16,7.168-16,16v208h-64 c-6.272,0-11.968,3.68-14.56,9.376c-2.624,5.728-1.6,12.416,2.528,17.152l112,128c3.04,3.488,7.424,5.472,12.032,5.472 c4.608,0,8.992-2.016,12.032-5.472l112-128C384.192,245.824,385.152,239.104,382.56,233.376z"/></g></g><g><g><path d="M432,352v96H80v-96H16v128c0,17.696,14.336,32,32,32h416c17.696,0,32-14.304,32-32V352H432z"/></g></g></div>`);
  183. },150);
  184. }
  185. }
  186. }
  187.  
  188. /**
  189. * onHighlightsStory
  190. * Trigger user's highlight download event or button display event.
  191. *
  192. * @param {Boolean} isDownload - Check if it is a download operation
  193. * @return {void}
  194. */
  195. async function onHighlightsStory(isDownload){
  196. if(isDownload){
  197. let date = new Date().getTime();
  198. let timestamp = Math.floor(date / 1000);
  199. let highlightId = location.href.replace(/\/$/ig,'').split('/').at(-1);
  200. let nowIndex = $("body > div section._ac0a header._ac0k > ._ac3r ._ac3n ._ac3p[style]").length ||
  201. $('body > div section:visible > div > div:not([class]) > div > div div.x1ned7t2.x78zum5 div.x1caxmr6').length ||
  202. $('body > div div:not([hidden]) section:visible > div div[style]:not([class]) > div').find('div div.x1ned7t2.x78zum5 div.x1caxmr6').length;
  203. let username = "";
  204. let target = 0;
  205.  
  206. if(GL_dataCache.highlights[highlightId]){
  207. console.log('Fetch from memory cache:', highlightId);
  208.  
  209. let totIndex = GL_dataCache.highlights[highlightId].data.reels_media[0].items.length;
  210. username = GL_dataCache.highlights[highlightId].data.reels_media[0].owner.username;
  211. target = GL_dataCache.highlights[highlightId].data.reels_media[0].items[totIndex-nowIndex];
  212. }
  213. else{
  214. let highStories = await getHighlightStories(highlightId);
  215. let totIndex = highStories.data.reels_media[0].items.length;
  216. username = highStories.data.reels_media[0].owner.username;
  217. target = highStories.data.reels_media[0].items[totIndex-nowIndex];
  218.  
  219. GL_dataCache.highlights[highlightId] = highStories;
  220. }
  221.  
  222.  
  223.  
  224. if(target.is_video){
  225. saveFiles(target.video_resources.at(-1).src,username,"highlights",timestamp,'mp4', highlightId);
  226. }
  227. else{
  228. saveFiles(target.display_resources.at(-1).src,username,"highlights",timestamp,'jpg', highlightId);
  229. }
  230. }
  231. else{
  232. // Add the stories download button
  233. if(!$('.IG_DWHISTORY').length){
  234. let $element = null;
  235.  
  236. // Default detecter (section layout mode)
  237. if($('body > div section._ac0a').length > 0){
  238. $element = $('body > div section:visible._ac0a');
  239. }
  240. else{
  241. $element = $('body > div section:visible > div > div[style]:not([class])');
  242. $element.css('position','relative');
  243. }
  244.  
  245. // Detecter for div layout mode
  246. // GitHub issue #3: DiceMast3r
  247. if($element.length === 0){
  248. let $$element = $('body > div div:not([hidden]) section:visible > div div[style]:not([class]) > div');
  249. let nowSize = 0;
  250.  
  251. $$element.each(function(){
  252. if($(this).width() > nowSize){
  253. nowSize = $(this).width();
  254. $element = $(this);
  255. }
  256. });
  257. }
  258.  
  259.  
  260. if($element != null){
  261. //$element.css('position','relative');
  262. $element.append(`<div title="${_i18n("DW")}" class="IG_DWHISTORY"><svg width="16" height="16" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Capa_1" x="0px" y="0px" viewBox="0 0 512 512" style="enable-background:new 0 0 512 512;" xml:space="preserve"><g><g><path d="M382.56,233.376C379.968,227.648,374.272,224,368,224h-64V16c0-8.832-7.168-16-16-16h-64c-8.832,0-16,7.168-16,16v208h-64 c-6.272,0-11.968,3.68-14.56,9.376c-2.624,5.728-1.6,12.416,2.528,17.152l112,128c3.04,3.488,7.424,5.472,12.032,5.472 c4.608,0,8.992-2.016,12.032-5.472l112-128C384.192,245.824,385.152,239.104,382.56,233.376z"/></g></g><g><g><path d="M432,352v96H80v-96H16v128c0,17.696,14.336,32,32,32h416c17.696,0,32-14.304,32-32V352H432z"/></g></g></div>`);
  263. }
  264. }
  265. }
  266. }
  267.  
  268. /**
  269. * onHighlightsStoryThumbnail
  270. * Trigger user's highlight video thumbnail download event or button display event.
  271. *
  272. * @param {Boolean} isDownload - Check if it is a download operation
  273. * @return {void}
  274. */
  275. async function onHighlightsStoryThumbnail(isDownload){
  276. if(isDownload){
  277. let date = new Date().getTime();
  278. let timestamp = Math.floor(date / 1000);
  279. let highlightId = location.href.replace(/\/$/ig,'').split('/').at(-1);
  280. let username = "";
  281. let nowIndex = $("body > div section._ac0a header._ac0k > ._ac3r ._ac3n ._ac3p[style]").length ||
  282. $('body > div section:visible > div > div:not([class]) > div > div div.x1ned7t2.x78zum5 div.x1caxmr6').length ||
  283. $('body > div div:not([hidden]) section:visible > div div[style]:not([class]) > div').find('div div.x1ned7t2.x78zum5 div.x1caxmr6').length;
  284. let target = "";
  285.  
  286. if(GL_dataCache.highlights[highlightId]){
  287. console.log('Fetch from memory cache:', highlightId);
  288.  
  289. let totIndex = GL_dataCache.highlights[highlightId].data.reels_media[0].items.length;
  290. username = GL_dataCache.highlights[highlightId].data.reels_media[0].owner.username;
  291. target = GL_dataCache.highlights[highlightId].data.reels_media[0].items[totIndex-nowIndex];
  292. }
  293. else{
  294. let highStories = await getHighlightStories(highlightId);
  295. let totIndex = highStories.data.reels_media[0].items.length;
  296. username = highStories.data.reels_media[0].owner.username;
  297. target = highStories.data.reels_media[0].items[totIndex-nowIndex];
  298.  
  299. GL_dataCache.highlights[highlightId] = highStories;
  300. }
  301.  
  302. saveFiles(target.display_resources.at(-1).src,username,"highlights",timestamp,'jpg', highlightId);
  303. }
  304. else{
  305. if($('body > div section video.xh8yej3').length){
  306. // Add the stories download button
  307. if(!$('.IG_DWHISTORY_THUMBNAIL').length){
  308. let $element = null;
  309.  
  310. // Default detecter (section layout mode)
  311. if($('body > div section._ac0a').length > 0){
  312. $element = $('body > div section:visible._ac0a');
  313. }
  314. else{
  315. $element = $('body > div section:visible > div > div[style]:not([class])');
  316. $element.css('position','relative');
  317. }
  318.  
  319. // Detecter for div layout mode
  320. // GitHub issue #3: DiceMast3r
  321. if($element.length === 0){
  322. let $$element = $('body > div div:not([hidden]) section:visible > div div[style]:not([class]) > div');
  323. let nowSize = 0;
  324.  
  325. $$element.each(function(){
  326. if($(this).width() > nowSize){
  327. nowSize = $(this).width();
  328. $element = $(this);
  329. }
  330. });
  331. }
  332.  
  333. if($element != null){
  334. $element.append(`<div title="${_i18n("THUMBNAIL_INTRO")}" class="IG_DWHISTORY_THUMBNAIL"><svg width="16" height="16" xmlns="http://www.w3.org/2000/svg" enable-background="new 0 0 24 24" height="512" viewBox="0 0 24 24" width="512"><circle cx="8.25" cy="5.25" r=".5"/><path d="m8.25 6.5c-.689 0-1.25-.561-1.25-1.25s.561-1.25 1.25-1.25 1.25.561 1.25 1.25-.561 1.25-1.25 1.25zm0-1.5c-.138 0-.25.112-.25.25 0 .275.5.275.5 0 0-.138-.112-.25-.25-.25z"/><path d="m7.25 11.25 2-2.5 2.25 1.5 2.25-3.5 3 4.5z"/><path d="m16.75 12h-9.5c-.288 0-.551-.165-.676-.425s-.09-.568.09-.793l2-2.5c.243-.304.678-.372 1.002-.156l1.616 1.077 1.837-2.859c.137-.212.372-.342.625-.344.246-.026.49.123.63.334l3 4.5c.153.23.168.526.037.77-.13.244-.385.396-.661.396zm-4.519-1.5h3.118l-1.587-2.381zm-3.42 0h1.712l-1.117-.745z"/><path d="m22.25 14h-2.756c-.778 0-1.452.501-1.676 1.247l-.859 2.862c-.16.533-.641.891-1.197.891h-7.524c-.556 0-1.037-.358-1.197-.891l-.859-2.861c-.224-.747-.897-1.248-1.676-1.248h-2.756c-.965 0-1.75.785-1.75 1.75v5.5c0 1.517 1.233 2.75 2.75 2.75h18.5c1.517 0 2.75-1.233 2.75-2.75v-5.5c0-.965-.785-1.75-1.75-1.75z"/><path d="m4 12c-.552 0-1-.448-1-1v-8c0-1.654 1.346-3 3-3h12c1.654 0 3 1.346 3 3v8c0 .552-.448 1-1 1s-1-.448-1-1v-8c0-.551-.449-1-1-1h-12c-.551 0-1 .449-1 1v8c0 .552-.448 1-1 1z"/></svg></div>`);
  335. }
  336. }
  337. }
  338. else{
  339. $('.IG_DWHISTORY_THUMBNAIL').remove();
  340. }
  341. }
  342. }
  343.  
  344. /**
  345. * onStory
  346. * Trigger user's story download event or button display event.
  347. *
  348. * @param {Boolean} isDownload - Check if it is a download operation
  349. * @return {void}
  350. */
  351. async function onStory(isDownload,isForce){
  352. if(isDownload){
  353. let date = new Date().getTime();
  354. let timestamp = Math.floor(date / 1000);
  355. let username = $("body > div section._ac0a header._ac0k ._ac0l a + div a").first().text() || location.pathname.split('/').at(2);
  356.  
  357. if($('body > div section._ac0a div._ac0b video.x1lliihq').length){
  358. // Download stories if it is video
  359. let type = "mp4";
  360. let videoURL = "";
  361. let targetURL = location.href.replace(/\/$/ig,'').split("/").at(-1);
  362.  
  363. if(GL_dataCache.stories[username] && !isForce){
  364. console.log('Fetch from memory cache:', username);
  365. GL_dataCache.stories[username].data.reels_media[0].items.forEach(item => {
  366. if(item.id == targetURL){
  367. videoURL = item.video_resources[0].src;
  368. }
  369. });
  370.  
  371. if(videoURL.length == 0){
  372. console.log('Memory cache not found, try fetch from API:', username);
  373. onStory(true,true);
  374. return;
  375. }
  376. }
  377. else{
  378. let userInfo = await getUserId(username);
  379. let userId = userInfo.user.pk;
  380. let stories = await getStories(userId);
  381.  
  382. stories.data.reels_media[0].items.forEach(item => {
  383. if(item.id == targetURL){
  384. videoURL = item.video_resources[0].src;
  385. }
  386. });
  387.  
  388. GL_dataCache.stories[username] = stories;
  389. }
  390.  
  391. if(videoURL.length == 0){
  392. alert(_i18n("NO_VID_URL"));
  393. }
  394. else{
  395. saveFiles(videoURL,username,"stories",timestamp,type);
  396. }
  397.  
  398. }
  399. else{
  400. // Download stories if it is image
  401. let srcset = $('section._ac0a ._aa64 img._aa63').attr('srcset').split(',')[0].split(' ')[0];
  402. let link = (srcset)?srcset:$('section._ac0a ._aa64 img._aa63').attr('src');
  403.  
  404. let downloadLink = link;
  405. let type = 'jpg';
  406.  
  407. saveFiles(downloadLink,username,"stories",timestamp,type);
  408. }
  409. }
  410. else{
  411. // Add the stories download button
  412. let style = "position: absolute;right:-40px;top:15px;padding:5px;line-height:1;background:#fff;border-radius: 5px;cursor:pointer;";
  413. if(!$('.IG_DWSTORY').length){
  414. GL_dataCache.stories = {};
  415. let $element = null;
  416. // Default detecter (section layout mode)
  417. if($('body > div section._ac0a').length > 0){
  418. $element = $('body > div section:visible._ac0a');
  419. }
  420. else{
  421. $element = $('body > div section:visible > div > div[style]:not([class])');
  422. $element.css('position','relative');
  423. }
  424.  
  425. // Detecter for div layout mode
  426. // GitHub issue #3: DiceMast3r
  427. if($element.length === 0){
  428. let $$element = $('body > div div:not([hidden]) section:visible > div div[style]:not([class]) > div');
  429. let nowSize = 0;
  430.  
  431. $$element.each(function(){
  432. if($(this).width() > nowSize){
  433. nowSize = $(this).width();
  434. $element = $(this);
  435. }
  436. });
  437. }
  438.  
  439. if($element != null){
  440. $element.css('position','relative');
  441. $element.append(`<div title="${_i18n("DW")}" class="IG_DWSTORY"><svg width="16" height="16" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Capa_1" x="0px" y="0px" viewBox="0 0 512 512" style="enable-background:new 0 0 512 512;" xml:space="preserve"><g><g><path d="M382.56,233.376C379.968,227.648,374.272,224,368,224h-64V16c0-8.832-7.168-16-16-16h-64c-8.832,0-16,7.168-16,16v208h-64 c-6.272,0-11.968,3.68-14.56,9.376c-2.624,5.728-1.6,12.416,2.528,17.152l112,128c3.04,3.488,7.424,5.472,12.032,5.472 c4.608,0,8.992-2.016,12.032-5.472l112-128C384.192,245.824,385.152,239.104,382.56,233.376z"/></g></g><g><g><path d="M432,352v96H80v-96H16v128c0,17.696,14.336,32,32,32h416c17.696,0,32-14.304,32-32V352H432z"/></g></g></div>`);
  442. }
  443. }
  444. }
  445. }
  446.  
  447. /**
  448. * onStoryThumbnail
  449. * Trigger user's story video thumbnail download event or button display event.
  450. *
  451. * @param {Boolean} isDownload - Check if it is a download operation
  452. * @return {void}
  453. */
  454. async function onStoryThumbnail(isDownload,isForce){
  455. if(isDownload){
  456. // Download stories if it is video
  457. let date = new Date().getTime();
  458. let timestamp = Math.floor(date / 1000);
  459. let type = 'jpg';
  460. let username = $("body > div section._ac0a header._ac0k ._ac0l a + div a").first().text() || location.pathname.split('/').at(2);
  461. let style = 'margin:5px 0px;padding:5px 0px;color:#111;font-size:1rem;line-height:1rem;text-align:center;border:1px solid #000;border-radius: 5px;';
  462. // Download thumbnail
  463. let targetURL = location.href.replace(/\/$/ig,'').split("/").at(-1);
  464. let videoThumbnailURL = "";
  465.  
  466.  
  467. if(GL_dataCache.stories[username] && !isForce){
  468. console.log('Fetch from memory cache:', username);
  469. GL_dataCache.stories[username].data.reels_media[0].items.forEach(item => {
  470. if(item.id == targetURL){
  471. videoThumbnailURL = item.display_url;
  472. }
  473. });
  474.  
  475. if(videoThumbnailURL.length == 0){
  476. console.log('Memory cache not found, try fetch from API:', username);
  477. onStoryThumbnail(true,true);
  478. return;
  479. }
  480. }
  481. else{
  482. let userInfo = await getUserId(username);
  483. let userId = userInfo.user.pk;
  484. let stories = await getStories(userId);
  485.  
  486. stories.data.reels_media[0].items.forEach(item => {
  487. if(item.id == targetURL){
  488. videoThumbnailURL = item.display_url;
  489. }
  490. });
  491. }
  492.  
  493. saveFiles(videoThumbnailURL,username,"thumbnail",timestamp,type);
  494. }
  495. else{
  496. if($('body > div section video.xh8yej3').length){
  497. // Add the stories download button
  498. if(!$('.IG_DWSTORY_THUMBNAIL').length){
  499. let $element = null;
  500. // Default detecter (section layout mode)
  501. if($('body > div section._ac0a').length > 0){
  502. $element = $('body > div section:visible._ac0a');
  503. }
  504. else{
  505. $element = $('body > div section:visible > div > div[style]:not([class])');
  506. $element.css('position','relative');
  507. }
  508.  
  509. // Detecter for div layout mode
  510. // GitHub issue #3: DiceMast3r
  511. if($element.length === 0){
  512. let $$element = $('body > div div:not([hidden]) section:visible > div div[style]:not([class]) > div');
  513. let nowSize = 0;
  514.  
  515. $$element.each(function(){
  516. if($(this).width() > nowSize){
  517. nowSize = $(this).width();
  518. $element = $(this);
  519. }
  520. });
  521. }
  522.  
  523. if($element != null){
  524. $element.css('position','relative');
  525. $element.append(`<div title="${_i18n("THUMBNAIL_INTRO")}" class="IG_DWSTORY_THUMBNAIL"><svg width="16" height="16" xmlns="http://www.w3.org/2000/svg" enable-background="new 0 0 24 24" height="512" viewBox="0 0 24 24" width="512"><circle cx="8.25" cy="5.25" r=".5"/><path d="m8.25 6.5c-.689 0-1.25-.561-1.25-1.25s.561-1.25 1.25-1.25 1.25.561 1.25 1.25-.561 1.25-1.25 1.25zm0-1.5c-.138 0-.25.112-.25.25 0 .275.5.275.5 0 0-.138-.112-.25-.25-.25z"/><path d="m7.25 11.25 2-2.5 2.25 1.5 2.25-3.5 3 4.5z"/><path d="m16.75 12h-9.5c-.288 0-.551-.165-.676-.425s-.09-.568.09-.793l2-2.5c.243-.304.678-.372 1.002-.156l1.616 1.077 1.837-2.859c.137-.212.372-.342.625-.344.246-.026.49.123.63.334l3 4.5c.153.23.168.526.037.77-.13.244-.385.396-.661.396zm-4.519-1.5h3.118l-1.587-2.381zm-3.42 0h1.712l-1.117-.745z"/><path d="m22.25 14h-2.756c-.778 0-1.452.501-1.676 1.247l-.859 2.862c-.16.533-.641.891-1.197.891h-7.524c-.556 0-1.037-.358-1.197-.891l-.859-2.861c-.224-.747-.897-1.248-1.676-1.248h-2.756c-.965 0-1.75.785-1.75 1.75v5.5c0 1.517 1.233 2.75 2.75 2.75h18.5c1.517 0 2.75-1.233 2.75-2.75v-5.5c0-.965-.785-1.75-1.75-1.75z"/><path d="m4 12c-.552 0-1-.448-1-1v-8c0-1.654 1.346-3 3-3h12c1.654 0 3 1.346 3 3v8c0 .552-.448 1-1 1s-1-.448-1-1v-8c0-.551-.449-1-1-1h-12c-.551 0-1 .449-1 1v8c0 .552-.448 1-1 1z"/></svg></div>`);
  526. }
  527. }
  528. }
  529. else{
  530. $('.IG_DWSTORY_THUMBNAIL').remove();
  531. }
  532. }
  533. }
  534.  
  535. /**
  536. * onReels
  537. * Trigger user's reels download event or button display event.
  538. *
  539. * @param {Boolean} isDownload - Check if it is a download operation
  540. * @param {Boolean} isVideo - Check if reel is a video element
  541. * @return {void}
  542. */
  543. async function onReels(isDownload, isVideo){
  544. if(isDownload){
  545. let reelsPath = location.href.split('?').at(0).split('instagram.com/reels/').at(-1).replaceAll('/','');
  546. let data = await getBlobMedia(reelsPath);
  547. let timestamp = new Date().getTime();
  548.  
  549. if(isVideo && data.shortcode_media.is_video){
  550. let type = 'mp4';
  551. saveFiles(data.shortcode_media.video_url,data.shortcode_media.owner.username,"reels",timestamp,type,reelsPath);
  552. }
  553. else{
  554. let type = 'jpg';
  555. saveFiles(data.shortcode_media.display_resources.at(-1).src,data.shortcode_media.owner.username,"reels",timestamp,type,reelsPath);
  556. }
  557. }
  558. else{
  559. //$('.IG_REELS_THUMBNAIL, .IG_REELS').remove();
  560. var timer = setInterval(()=>{
  561. if($('section > main[role="main"] > div div.x1qjc9v5 video').length > 0){
  562. clearInterval(timer);
  563. $('section > main[role="main"] > div').children('div').each(function(){
  564. if($(this).children().length > 0){
  565. if(!$(this).children().find('.IG_REELS').length){
  566. $(this).children().css('position','relative');
  567.  
  568. $(this).children().append(`<div title="${_i18n("DW")}" class="IG_REELS"><svg width="16" height="16" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Capa_1" x="0px" y="0px" viewBox="0 0 512 512" style="enable-background:new 0 0 512 512;" xml:space="preserve"><g><g><path d="M382.56,233.376C379.968,227.648,374.272,224,368,224h-64V16c0-8.832-7.168-16-16-16h-64c-8.832,0-16,7.168-16,16v208h-64 c-6.272,0-11.968,3.68-14.56,9.376c-2.624,5.728-1.6,12.416,2.528,17.152l112,128c3.04,3.488,7.424,5.472,12.032,5.472 c4.608,0,8.992-2.016,12.032-5.472l112-128C384.192,245.824,385.152,239.104,382.56,233.376z"/></g></g><g><g><path d="M432,352v96H80v-96H16v128c0,17.696,14.336,32,32,32h416c17.696,0,32-14.304,32-32V352H432z"/></g></g></div>`);
  569. $(this).children().append(`<div title="${_i18n("THUMBNAIL_INTRO")}" class="IG_REELS_THUMBNAIL"><svg width="16" height="16" xmlns="http://www.w3.org/2000/svg" enable-background="new 0 0 24 24" height="512" viewBox="0 0 24 24" width="512"><circle cx="8.25" cy="5.25" r=".5"/><path d="m8.25 6.5c-.689 0-1.25-.561-1.25-1.25s.561-1.25 1.25-1.25 1.25.561 1.25 1.25-.561 1.25-1.25 1.25zm0-1.5c-.138 0-.25.112-.25.25 0 .275.5.275.5 0 0-.138-.112-.25-.25-.25z"/><path d="m7.25 11.25 2-2.5 2.25 1.5 2.25-3.5 3 4.5z"/><path d="m16.75 12h-9.5c-.288 0-.551-.165-.676-.425s-.09-.568.09-.793l2-2.5c.243-.304.678-.372 1.002-.156l1.616 1.077 1.837-2.859c.137-.212.372-.342.625-.344.246-.026.49.123.63.334l3 4.5c.153.23.168.526.037.77-.13.244-.385.396-.661.396zm-4.519-1.5h3.118l-1.587-2.381zm-3.42 0h1.712l-1.117-.745z"/><path d="m22.25 14h-2.756c-.778 0-1.452.501-1.676 1.247l-.859 2.862c-.16.533-.641.891-1.197.891h-7.524c-.556 0-1.037-.358-1.197-.891l-.859-2.861c-.224-.747-.897-1.248-1.676-1.248h-2.756c-.965 0-1.75.785-1.75 1.75v5.5c0 1.517 1.233 2.75 2.75 2.75h18.5c1.517 0 2.75-1.233 2.75-2.75v-5.5c0-.965-.785-1.75-1.75-1.75z"/><path d="m4 12c-.552 0-1-.448-1-1v-8c0-1.654 1.346-3 3-3h12c1.654 0 3 1.346 3 3v8c0 .552-.448 1-1 1s-1-.448-1-1v-8c0-.551-.449-1-1-1h-12c-.551 0-1 .449-1 1v8c0 .552-.448 1-1 1z"/></svg></div>`);
  570.  
  571. // Disable video autoplay
  572. if(USER_SETTING.DISABLE_VIDEO_LOOPING){
  573. $(this).find('video').each(function(){
  574. if(!$(this).data('loop')){
  575. console.log('Added video event listener');
  576. $(this).on('ended',function(){
  577. $(this).attr('data-loop', true);
  578. $(this).parent().find('.xpgaw4o').removeAttr('style');
  579. console.log('paused');
  580.  
  581. this.pause();
  582. });
  583. }
  584. });
  585. }
  586. }
  587. }
  588. });
  589. }
  590. },250);
  591. }
  592. }
  593.  
  594. /**
  595. * getHighlightStories
  596. * Get a list of all stories in highlight Id.
  597. *
  598. * @param {Integer} highlightId
  599. * @return {Object}
  600. */
  601. function getHighlightStories(highlightId){
  602. return new Promise((resolve,reject)=>{
  603. let getURL = `https://www.instagram.com/graphql/query/?query_hash=45246d3fe16ccc6577e0bd297a5db1ab&variables=%7B%22highlight_reel_ids%22:%5B%22${highlightId}%22%5D,%22precomposed_overlay%22:false%7D`;
  604.  
  605. GM_xmlhttpRequest({
  606. method: "GET",
  607. url: getURL,
  608. onload: function(response) {
  609. let obj = JSON.parse(response.response);
  610. resolve(obj);
  611. },
  612. onerror: function(err){
  613. reject(err);
  614. }
  615. });
  616. });
  617. }
  618.  
  619. /**
  620. * getStories
  621. * Get a list of all stories in user Id.
  622. *
  623. * @param {Integer} userId
  624. * @return {Object}
  625. */
  626. function getStories(userId){
  627. return new Promise((resolve,reject)=>{
  628. let getURL = `https://www.instagram.com/graphql/query/?query_hash=15463e8449a83d3d60b06be7e90627c7&variables=%7B%22reel_ids%22:%5B%22${userId}%22%5D,%22precomposed_overlay%22:false%7D`;
  629.  
  630. GM_xmlhttpRequest({
  631. method: "GET",
  632. url: getURL,
  633. onload: function(response) {
  634. let obj = JSON.parse(response.response);
  635. resolve(obj);
  636. },
  637. onerror: function(err){
  638. reject(err);
  639. }
  640. });
  641. });
  642. }
  643.  
  644. /**
  645. * getUserId
  646. * Get user's id with username
  647. *
  648. * @param {String} username
  649. * @return {Integer}
  650. */
  651. function getUserId(username){
  652. return new Promise((resolve,reject)=>{
  653. let getURL = `https://www.instagram.com/web/search/topsearch/?query=${username}`;
  654.  
  655. GM_xmlhttpRequest({
  656. method: "GET",
  657. url: getURL,
  658. onload: function(response) {
  659. let obj = JSON.parse(response.response);
  660. resolve(obj.users[0]);
  661. },
  662. onerror: function(err){
  663. reject(err);
  664. }
  665. });
  666. });
  667. }
  668.  
  669. /**
  670. * getUserHighSizeProfile
  671. * Get user's high quality avatar image.
  672. *
  673. * @param {Integer} userId
  674. * @return {String}
  675. */
  676. function getUserHighSizeProfile(userId){
  677. return new Promise((resolve,reject)=>{
  678. let getURL = `https://i.instagram.com/api/v1/users/${userId}/info/`;
  679.  
  680. GM_xmlhttpRequest({
  681. method: "GET",
  682. url: getURL,
  683. headers: {
  684. 'User-Agent': 'Mozilla/5.0 (Linux; Android 10; Pixel 7 XL)Build/RP1A.20845.002; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/5.0 Chrome/117.0.5938.60 Mobile Safari/537.36 Instagram 307.0.0.34.111'
  685. },
  686. onload: function(response) {
  687. let obj = JSON.parse(response.response);
  688. if(obj.status !== 'ok'){
  689. reject('faild');
  690. }
  691. else{
  692. resolve(obj.user.hd_profile_pic_url_info?.url);
  693. }
  694. },
  695. onerror: function(err){
  696. reject(err);
  697. }
  698. });
  699. });
  700. }
  701.  
  702. /**
  703. * getPostOwner
  704. * Get post's author with post shortcode
  705. *
  706. * @param {String} postPath
  707. * @return {String}
  708. */
  709. function getPostOwner(postPath){
  710. return new Promise((resolve,reject)=>{
  711. if(!postPath) reject("NOPATH");
  712. let postShortCode = postPath;
  713. let getURL = `https://www.instagram.com/graphql/query/?query_hash=2c4c2e343a8f64c625ba02b2aa12c7f8&variables=%7B%22shortcode%22:%22${postShortCode}%22}`;
  714.  
  715. GM_xmlhttpRequest({
  716. method: "GET",
  717. url: getURL,
  718. onload: function(response) {
  719. let obj = JSON.parse(response.response);
  720. resolve(obj.data.shortcode_media.owner.username);
  721. },
  722. onerror: function(err){
  723. reject(err);
  724. }
  725. });
  726. });
  727. }
  728.  
  729. /**
  730. * getBlobMedia
  731. * Get list of all media files in post with post shortcode
  732. *
  733. * @param {String} postPath
  734. * @return {Object}
  735. */
  736. function getBlobMedia(postPath){
  737. return new Promise((resolve,reject)=>{
  738. if(!postPath) reject("NOPATH");
  739. let postShortCode = postPath;
  740. let getURL = `https://www.instagram.com/graphql/query/?query_hash=2c4c2e343a8f64c625ba02b2aa12c7f8&variables=%7B%22shortcode%22:%22${postShortCode}%22}`;
  741.  
  742. GM_xmlhttpRequest({
  743. method: "GET",
  744. url: getURL,
  745. onload: function(response) {
  746. let obj = JSON.parse(response.response);
  747. console.log(obj);
  748. resolve(obj.data);
  749. },
  750. onerror: function(err){
  751. reject(err);
  752. }
  753. });
  754. });
  755. }
  756.  
  757. /**
  758. * onReadyMyDW
  759. * Create an event entry point for the download button for the post
  760. *
  761. * @param {Boolean} NoDialog - Check if it not showing the dialog
  762. * @return {void}
  763. */
  764. function onReadyMyDW(NoDialog){
  765. // Whether is Instagram dialog?
  766. if(!NoDialog){
  767. // Running if it is dialog
  768. $('article, main > div > div.xdt5ytf > div').each(function(){
  769. $(this).removeAttr('data-snig');
  770. $(this).unbind('click');
  771. });
  772. $('.SNKMS_IG_DW_MAIN,.SNKMS_IG_DW_MAIN_VIDEO').remove();
  773. }
  774. if(NoDialog == false){
  775. var repeat = setInterval(() => {
  776. // div.x13ehr01 << (sigle post in top, not floating) >>
  777. if($('article[data-snig="canDownload"], main > div > div.xdt5ytf > div[data-snig="canDownload"]').length > 0) clearInterval(repeat);
  778. createDownloadButton();
  779. },250);
  780. }
  781. else{
  782. createDownloadButton();
  783. }
  784. }
  785.  
  786. /**
  787. * createDownloadButton
  788. * Create a download button in the upper right corner of each post
  789. *
  790. * @return {void}
  791. */
  792. function createDownloadButton(){
  793. // Add download icon per each posts
  794. $('article, main > div > div.xdt5ytf > div').each(function(){
  795. // If it is have not download icon
  796. if(!$(this).attr('data-snig') && !$(this).hasClass('x1iyjqo2')){
  797. console.log("Found article");
  798. var rightPos = 15;
  799. if(this.tagName === "DIV"){
  800. rightPos = ($(this).children('div:last-child').width() !== undefined) ? $(this).children('div:last-child').width()+15 : 335+15
  801. }
  802.  
  803. // Add the download icon
  804. let $childElement = $(this).children("div").children("div");
  805. $childElement.eq((this.tagName === "DIV")? 0 : $childElement.length - 2).append(`<div title="${_i18n("DW")}" class="SNKMS_IG_DW_MAIN" style="right:${rightPos}px;"><svg width="16" height="16" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Capa_1" x="0px" y="0px" viewBox="0 0 512 512" style="enable-background:new 0 0 512 512;" xml:space="preserve"><g><g><path d="M382.56,233.376C379.968,227.648,374.272,224,368,224h-64V16c0-8.832-7.168-16-16-16h-64c-8.832,0-16,7.168-16,16v208h-64 c-6.272,0-11.968,3.68-14.56,9.376c-2.624,5.728-1.6,12.416,2.528,17.152l112,128c3.04,3.488,7.424,5.472,12.032,5.472 c4.608,0,8.992-2.016,12.032-5.472l112-128C384.192,245.824,385.152,239.104,382.56,233.376z"/></g></g><g><g><path d="M432,352v96H80v-96H16v128c0,17.696,14.336,32,32,32h416c17.696,0,32-14.304,32-32V352H432z"/></g></g></div>`);
  806.  
  807. // Disable video autoplay
  808. if(USER_SETTING.DISABLE_VIDEO_LOOPING){
  809. $(this).find('video').each(function(){
  810. if(!$(this).data('loop')){
  811. console.log('Added video event listener');
  812. $(this).on('ended',function(){
  813. $(this).attr('data-loop', true);
  814. this.pause();
  815. });
  816. }
  817. });
  818. }
  819.  
  820. // Running if user click the download icon
  821. $(this).on('click','.SNKMS_IG_DW_MAIN', async function(e){
  822. GL_username = $(this).parent().parent().parent().attr('data-username');
  823. GL_postPath = location.pathname.split('/p/').at(-1).replaceAll('/','') || $(this).parent().parent().children("div:last-child").children("div").children("div:last-child").find('a[href^="/p/"]').last().attr("href").split("/").at(2);
  824.  
  825. // Create element that download dailog
  826. IG_createDM(GM_getValue('AutoDownload'), true);
  827.  
  828. $("#article-id").html(`<a href="https://www.instagram.com/p/${GL_postPath}">${GL_postPath}</a>`);
  829.  
  830. // Find video/image element and add the download icon
  831. var s = 0;
  832. var multiple = $(this).parent().parent().find('._aap0 ._acaz').length;
  833. var pathname = window.location.pathname;
  834. var fullpathname = "/"+pathname.split('/')[1]+"/"+pathname.split('/')[2]+"/";
  835. var blob = USER_SETTING.FORCE_FETCH_ALL_RESOURCES;
  836.  
  837. // If posts have more than one images or videos.
  838. if(multiple){
  839. $(this).parent().find('._aap0 ._acaz').each(function(){
  840. let element_videos = $(this).parent().parent().find('video');
  841. //if(element_videos && element_videos.attr('src') && element_videos.attr('src').match(/^blob:/ig)){
  842. if(element_videos && element_videos.attr('src')){
  843. blob = true;
  844. }
  845. });
  846.  
  847.  
  848. if(blob){
  849. createMediaListDOM(GL_postPath,".IG_SN_DIG .IG_SN_DIG_MAIN .IG_SN_DIG_BODY",_i18n("LOAD_BLOB_MULTIPLE"));
  850. }
  851. else{
  852. $(this).parent().find('._aap0 ._acaz').each(function(){
  853. s++;
  854. let element_videos = $(this).find('video');
  855. let element_images = $(this).find('._aagv img');
  856. let imgLink = (element_images.attr('srcset'))?element_images.attr('srcset').split(" ")[0]:element_images.attr('src');
  857.  
  858. if(element_videos && element_videos.attr('src')){
  859. blob = true;
  860. /*
  861. let video_image = element_videos.attr('poster');
  862. let video_url = element_videos.attr('src');
  863.  
  864. $('.IG_SN_DIG .IG_SN_DIG_MAIN .IG_SN_DIG_BODY').append(`<a data-needed="direct" data-path="${GL_postPath}" data-name="IGTV" data-type="mp4" data-globalIndex="${s}" href="javascript:;" data-href="${video_url}"><img width="100" src="${video_image}" /><br/>- ${_i18n("VID")} ${s} -</a>`);
  865.  
  866. if(video_url.match(/^blob:/ig)) blob = true;
  867. */
  868. }
  869. if(element_images && imgLink){
  870. $('.IG_SN_DIG .IG_SN_DIG_MAIN .IG_SN_DIG_BODY').append(`<a data-needed="direct" data-path="${GL_postPath}" data-name="photo" data-type="jpg" data-globalIndex="${s}" href="javascript:;" data-href="${imgLink}"><img width="100" src="${imgLink}" /><br/>- ${_i18n("IMG")} ${s} -</a>`);
  871. }
  872.  
  873. });
  874.  
  875. if(blob){
  876. createMediaListDOM(GL_postPath,".IG_SN_DIG .IG_SN_DIG_MAIN .IG_SN_DIG_BODY",_i18n("LOAD_BLOB_RELOAD"));
  877. }
  878. }
  879. }
  880. else{
  881. s++;
  882. let element_videos = $(this).parent().parent().find('video');
  883. let element_images = $(this).parent().parent().find('._aagv img');
  884. let imgLink = (element_images.attr('srcset'))?element_images.attr('srcset').split(" ")[0]:element_images.attr('src');
  885.  
  886.  
  887. if(element_videos && element_videos.attr('src')){
  888. createMediaListDOM(GL_postPath,".IG_SN_DIG .IG_SN_DIG_MAIN .IG_SN_DIG_BODY",_i18n("LOAD_BLOB_ONE"));
  889.  
  890. /*
  891. let video_image = element_videos.attr('poster');
  892. let video_url = element_videos.attr('src');
  893.  
  894. if(element_videos.attr('src').match(/^blob:/ig)){
  895. createMediaListDOM(GL_postPath,".IG_SN_DIG .IG_SN_DIG_MAIN .IG_SN_DIG_BODY",_i18n("LOAD_BLOB_ONE"));
  896. }
  897. else{
  898. $('.IG_SN_DIG .IG_SN_DIG_MAIN .IG_SN_DIG_BODY').append(`<a data-needed="direct" data-path="${GL_postPath}" data-name="video" data-type="mp4" data-globalIndex="${s}" href="javascript:;" data-href="${video_url}"><img width="100" src="${video_image}" /><br/>- ${_i18n("VID")} ${s} -</a>`);
  899. }
  900. */
  901. }
  902. if(element_images && imgLink){
  903. $('.IG_SN_DIG .IG_SN_DIG_MAIN .IG_SN_DIG_BODY').append(`<a data-needed="direct" data-path="${GL_postPath}" data-name="photo" data-type="jpg" data-globalIndex="${s}" href="javascript:;" href="" data-href="${imgLink}"><img width="100" src="${imgLink}" /><br/>- ${_i18n("IMG")} ${s} -</a>`);
  904. }
  905. }
  906.  
  907.  
  908. $('.IG_SN_DIG .IG_SN_DIG_MAIN .IG_SN_DIG_BODY a').each(function(){
  909. $(this).wrap('<div></div>');
  910. $(this).before('<label class="inner_box_wrapper"><input class="inner_box" type="checkbox"><span></span></label>');
  911. });
  912. });
  913.  
  914. // Add the mark that download is ready
  915. var username = $(this).find("header > div:last-child > div:first-child span").first().text();
  916.  
  917. $(this).attr('data-snig','canDownload');
  918. $(this).attr('data-username',username);
  919. }
  920. });
  921. }
  922.  
  923. /**
  924. * createMediaListDOM
  925. * Create a list of media elements from post URLs
  926. *
  927. * @param {String} postURL
  928. * @param {String} selector - Use CSS element selectors to choose where it appears.
  929. * @param {String} message - i18n display loading message
  930. * @return {void}
  931. */
  932. async function createMediaListDOM(postURL,selector,message){
  933. $(`${selector} a`).remove();
  934. $(selector).append('<p id="_SNLOAD">'+ message +'</p>');
  935. let media = await getBlobMedia(postURL);
  936. let idx = 1;
  937. let resource = media.shortcode_media;
  938.  
  939. // GraphVideo
  940. if(resource.__typename == "GraphVideo" && resource.video_url){
  941. $(selector).append(`<a data-blob="true" data-needed="direct" data-path="${resource.shortcode}" data-name="video" data-type="mp4" data-username="${resource.owner.username}" data-globalIndex="${idx}" href="javascript:;" data-href="${resource.video_url}"><img width="100" src="${resource.display_resources[1].src}" /><br/>- ${_i18n("VID")} ${idx} -</a>`);
  942. idx++;
  943. }
  944. // GraphSidecar
  945. if(resource.__typename == "GraphSidecar" && resource.edge_sidecar_to_children){
  946. for(let e of resource.edge_sidecar_to_children.edges){
  947. if(e.node.__typename == "GraphVideo"){
  948. $(selector).append(`<a data-blob="true" data-needed="direct" data-path="${resource.shortcode}" data-name="video" data-type="mp4" data-username="${resource.owner.username}" data-globalIndex="${idx}" href="javascript:;" data-href="${e.node.video_url}"><img width="100" src="${e.node.display_resources[1].src}" /><br/>- ${_i18n("VID")} ${idx} -</a>`);
  949. }
  950.  
  951. if(e.node.__typename == "GraphImage"){
  952. $(selector).append(`<a data-blob="true" data-needed="direct" data-path="${resource.shortcode}" data-name="photo" data-type="jpg" data-username="${resource.owner.username}" data-globalIndex="${idx}" href="javascript:;" data-href="${e.node.display_resources[e.node.display_resources.length - 1].src}"><img width="100" src="${e.node.display_resources[1].src}" /><br/>- ${_i18n("IMG")} ${idx} -</a>`);
  953. }
  954. idx++;
  955. }
  956. }
  957. $("#_SNLOAD").remove();
  958. $('.IG_SN_DIG .IG_SN_DIG_MAIN .IG_SN_DIG_BODY a').each(function(){
  959. $(this).wrap('<div></div>');
  960. $(this).before('<label class="inner_box_wrapper"><input class="inner_box" type="checkbox"><span></span></label>');
  961. });
  962. }
  963.  
  964. /**
  965. * IG_createDM
  966. * A dialog showing a list of all media files in the post
  967. *
  968. * @param {Boolean} hasHidden
  969. * @param {Boolean} hasCheckbox
  970. * @return {void}
  971. */
  972. function IG_createDM(hasHidden, hasCheckbox){
  973. let isHidden = (hasHidden)?"hidden":"";
  974. $('body').append('<div class="IG_SN_DIG '+isHidden+'"><div class="IG_SN_DIG_BG"></div><div class="IG_SN_DIG_MAIN"><div class="IG_SN_DIG_TITLE"></div><div class="IG_SN_DIG_BODY"></div></div></div>');
  975. $('.IG_SN_DIG .IG_SN_DIG_MAIN .IG_SN_DIG_TITLE').append('<div style="position:relative;height:36px;text-align:center;margin-bottom: 7px;"><div style="position:absolute;left:0px;line-height: 18px;"><kbd>Alt</kbd>+<kbd>Q</kbd> ['+_i18n("CLOSE")+']</div><div style="line-height: 18px;">IG Helper</div><div id="post_info" style="line-height: 14px;font-size:14px;">Post ID: <span id="article-id"></span></div><svg width="26" height="26" class="IG_SN_DIG_BTN" style="cursor:pointer;position:absolute;right:0px;top:0px;fill: rgb(var(--ig-primary-text));" xmlns="http://www.w3.org/2000/svg" id="bold" enable-background="new 0 0 24 24" height="512" viewBox="0 0 24 24" width="512"><path d="m14.828 12 5.303-5.303c.586-.586.586-1.536 0-2.121l-.707-.707c-.586-.586-1.536-.586-2.121 0l-5.303 5.303-5.303-5.304c-.586-.586-1.536-.586-2.121 0l-.708.707c-.586.586-.586 1.536 0 2.121l5.304 5.304-5.303 5.303c-.586.586-.586 1.536 0 2.121l.707.707c.586.586 1.536.586 2.121 0l5.303-5.303 5.303 5.303c.586.586 1.536.586 2.121 0l.707-.707c.586-.586.586-1.536 0-2.121z"/></svg></div>');
  976.  
  977. if(hasCheckbox){
  978. $('.IG_SN_DIG .IG_SN_DIG_MAIN .IG_SN_DIG_TITLE').append(`<div style="text-align: center;" id="button_group"></div>`);
  979. $('.IG_SN_DIG .IG_SN_DIG_MAIN .IG_SN_DIG_TITLE > div#button_group').append(`<button id="batch_download_selected">${_i18n('BATCH_DOWNLOAD_SELECTED')}</button>`);
  980. $('.IG_SN_DIG .IG_SN_DIG_MAIN .IG_SN_DIG_TITLE > div#button_group').append(`<button id="batch_download_direct">${_i18n('BATCH_DOWNLOAD_DIRECT')}</button>`);
  981. $('.IG_SN_DIG .IG_SN_DIG_MAIN .IG_SN_DIG_TITLE').append(`<br/><label class="checkbox"><input value="yes" type="checkbox" />${_i18n('ALL_CHECK')}</label>`);
  982. }
  983. }
  984.  
  985. /**
  986. * saveFiles
  987. * Download the specified media URL to the computer
  988. *
  989. * @param {String} downloadLink
  990. * @param {String} username
  991. * @param {String} sourceType
  992. * @param {Integer} timestamp
  993. * @param {String} filetype
  994. * @param {String} shortcode
  995. * @return {void}
  996. */
  997. function saveFiles(downloadLink,username,sourceType,timestamp,filetype,shortcode){
  998. fetch(downloadLink).then(res => {
  999. return res.blob().then(dwel => {
  1000. const a = document.createElement("a");
  1001. const name = username+'-'+sourceType+'-'+((USER_SETTING.RENAME_SHORTCODE && shortcode)?shortcode+'-':'')+timestamp+'.'+filetype;
  1002. const originally = username + '_' + downloadLink.split('/').at(-1).split('?').at(0);
  1003.  
  1004. a.href = URL.createObjectURL(dwel);
  1005. a.setAttribute("download", (USER_SETTING.AUTO_RENAME)?name:originally);
  1006. a.click();
  1007. a.remove();
  1008. });
  1009. });
  1010. }
  1011.  
  1012. /**
  1013. * translateText
  1014. * i18n translation text
  1015. *
  1016. * @param {String} lang
  1017. * @return {void}
  1018. */
  1019. function translateText(lang){
  1020. return {
  1021. "zh-TW": {
  1022. "CLOSE": "關閉",
  1023. "ALL_CHECK": "全選",
  1024. "BATCH_DOWNLOAD_SELECTED": "批次下載已勾選資源",
  1025. "BATCH_DOWNLOAD_DIRECT": "批次下載全部資源",
  1026. "IMG": "相片",
  1027. "VID": "影片",
  1028. "DDL": "快速下載",
  1029. "DDL_INTRO": "勾選後將直接下載點選當下位置的相片/影片",
  1030. "DW": "下載",
  1031. "THUMBNAIL_INTRO": "下載影片縮圖",
  1032. "LOAD_BLOB_ONE": "正在載入二進位大型物件...",
  1033. "LOAD_BLOB_MULTIPLE": "正在載入多個二進位大型物件...",
  1034. "LOAD_BLOB_RELOAD": "正在重新載入二進位大型物件...",
  1035. "NO_CHECK_RESOURCE": "您需要勾選資源才能下載。",
  1036. "NO_VID_URL": "找不到影片網址",
  1037. "SETTING": "設定",
  1038. "AUTO_RENAME": "自動重新命名檔案",
  1039. "RENAME_SHORTCODE": "重新命名檔案並包含 Shortcode",
  1040. "DISABLE_VIDEO_LOOPING": "關閉影片自動循環播放",
  1041. "REDIRECT_RIGHT_CLICK_USER_STORY_PICTURE": "右鍵點擊使用者限時動態區域頭貼時重定向",
  1042. "FORCE_FETCH_ALL_RESOURCES": "強制提取貼文中所有資源",
  1043. "AUTO_RENAME_INTRO": "將檔案自動重新命名為以下格式:\n使用者名稱-類型-時間戳.檔案類型\n例如:instagram-photo-1670350000.jpg\n\n若設為 false,則檔案名稱將保持原始樣貌。 \n例如:instagram_321565527_679025940443063_4318007696887450953_n.jpg",
  1044. "RENAME_SHORTCODE_INTRO": "將檔案自動重新命名為以下格式:\n使用者名稱-類型-Shortcode-時間戳.檔案類型\n示例:instagram-photo-CwkxyiVynpW-1670350000.jpg\n\n此功能僅在[自動重新命名檔案]設定為 TRUE 時有效。",
  1045. "DISABLE_VIDEO_LOOPING_INTRO": "關閉連續短片和貼文中影片自動循環播放。",
  1046. "REDIRECT_RIGHT_CLICK_USER_STORY_PICTURE_INTRO": "右鍵點選首頁限時動態區域中的使用者頭貼時,重新導向到使用者的個人資料頁面。",
  1047. "FORCE_FETCH_ALL_RESOURCES_INTRO": "透過 Instagram API 強制取得貼文中的所有資源(照片和影片),以取消每個貼文單次提取三個資源的限制。",
  1048. },
  1049. "zh-CN": {
  1050. "CLOSE": "关闭",
  1051. "ALL_CHECK": "全选",
  1052. "BATCH_DOWNLOAD_SELECTED": "批量下载已勾选资源",
  1053. "BATCH_DOWNLOAD_DIRECT": "批量下载全部资源",
  1054. "IMG": "图像",
  1055. "VID": "视频",
  1056. "DDL": "便捷下载",
  1057. "DDL_INTRO": "勾选后将直接下载點擊當下位置的图像/视频",
  1058. "DW": "下载",
  1059. "THUMBNAIL_INTRO": "下载视频缩略图",
  1060. "LOAD_BLOB_ONE": "正在载入大型媒体对象...",
  1061. "LOAD_BLOB_MULTIPLE": "正在载入多个大型媒体对象...",
  1062. "LOAD_BLOB_RELOAD": "正在重新载入大型媒体对象...",
  1063. "NO_CHECK_RESOURCE": "您需要勾選资源才能下載。",
  1064. "NO_VID_URL": "找不到视频网址",
  1065. "SETTING": "设置",
  1066. "AUTO_RENAME": "自动重命名文件",
  1067. "RENAME_SHORTCODE": "重命名文件并包含物件短码",
  1068. "DISABLE_VIDEO_LOOPING": "禁用视频自动循环",
  1069. "REDIRECT_RIGHT_CLICK_USER_STORY_PICTURE": "右键单击用户故事区域头像时重定向",
  1070. "FORCE_FETCH_ALL_RESOURCES": "强制抓取帖子中所有资源",
  1071. "AUTO_RENAME_INTRO": "将文件自动重新命名为以下格式类型:\n用户名-类型-时间戳.文件类型\n例如:instagram-photo-1670350000.jpg\n\n若设为false,则文件名将保持原样。 \n例如:instagram_321565527_679025940443063_4318007696887450953_n.jpg",
  1072. "RENAME_SHORTCODE_INTRO": "自动重命名文件为以下格式类型:\n用户名-类型-短码-时间戳.文件类型\n示例:instagram-photo-CwkxyiVynpW-1670350000.jpg\n\n它仅在[自动重命名文件]设置为 TRUE 时有效。",
  1073. "DISABLE_VIDEO_LOOPING_INTRO": "禁用 Reels 和帖子中的视频自动播放。",
  1074. "REDIRECT_RIGHT_CLICK_USER_STORY_PICTURE_INTRO": "右键单击主页故事区域中的用户头像,重定向到用户的个人资料页面。",
  1075. "FORCE_FETCH_ALL_RESOURCES_INTRO": "通过 Instagram API 强制获取帖子中的所有资源(照片和视频),以取消每个帖子单次抓取三个资源的限制。"
  1076. },
  1077. "en-US": {
  1078. "CLOSE": "Close",
  1079. "ALL_CHECK": "Select All",
  1080. "BATCH_DOWNLOAD_SELECTED": "Download Selected Resources",
  1081. "BATCH_DOWNLOAD_DIRECT": "Download All Resources",
  1082. "IMG": "Image",
  1083. "VID": "Video",
  1084. "DDL": "Quick Download",
  1085. "DDL_INTRO": "Checking it will direct download current photo/media in the posts.",
  1086. "DW": "Download",
  1087. "THUMBNAIL_INTRO": "Download video thumbnail.",
  1088. "LOAD_BLOB_ONE": "Loading Blob Media...",
  1089. "LOAD_BLOB_MULTIPLE": "Loading Blob Media and others...",
  1090. "LOAD_BLOB_RELOAD": "Detect Blob Media, now reloading...",
  1091. "NO_CHECK_RESOURCE": "You need to check resource to download.",
  1092. "NO_VID_URL": "Can not find video url.",
  1093. "SETTING": "Settings",
  1094. "AUTO_RENAME": "Automatically Rename Files",
  1095. "RENAME_SHORTCODE": "Rename The File and Include Shortcode",
  1096. "DISABLE_VIDEO_LOOPING": "Disable Video Auto-looping",
  1097. "REDIRECT_RIGHT_CLICK_USER_STORY_PICTURE": "Redirect When Right-Clicking User Story Picture",
  1098. "FORCE_FETCH_ALL_RESOURCES": "Forcing Fetch All Resources In the Post",
  1099. "AUTO_RENAME_INTRO": "Auto rename file to format type following:\nUSERNAME-TYPE-TIMESTAMP.FILETYPE\nExample: instagram-photo-1670350000.jpg\n\nIf set to false, the file name will remain as it is.\nExample: instagram_321565527_679025940443063_4318007696887450953_n.jpg",
  1100. "RENAME_SHORTCODE_INTRO": "Auto rename file to format type following:\nUSERNAME-TYPE-SHORTCODE-TIMESTAMP.FILETYPE\nExample: instagram-photo-CwkxyiVynpW-1670350000.jpg\n\nIt will ONLY work in [Automatically Rename Files] setting to TRUE.",
  1101. "DISABLE_VIDEO_LOOPING_INTRO": "Disable video auto-looping in reels and posts.",
  1102. "REDIRECT_RIGHT_CLICK_USER_STORY_PICTURE_INTRO": "Redirect to a user's profile page when right-clicking on their user avatar in the story area on the homepage.",
  1103. "FORCE_FETCH_ALL_RESOURCES_INTRO": "Force fetching of all resources (photos and videos) in a post via the Instagram API to remove the limit of three resources per post."
  1104. }
  1105. };
  1106. }
  1107.  
  1108. /**
  1109. * _i18n
  1110. * Perform i18n translation
  1111. *
  1112. * @param {String} text
  1113. * @return {void}
  1114. */
  1115. function _i18n(text){
  1116. let userLang = (lang)?lang:"en-US";
  1117. let translate = {
  1118. "zh-TW": function(){
  1119. return translateText()["zh-TW"];
  1120. },
  1121. "zh-HK": function(){
  1122. return translateText()["zh-TW"];
  1123. },
  1124. "zh-MO": function(){
  1125. return translateText()["zh-TW"];
  1126. },
  1127. "zh-CN": function(){
  1128. return translateText()["zh-CN"];
  1129. },
  1130. "en-US": function(){
  1131. return translateText()["en-US"];
  1132. }
  1133. }
  1134.  
  1135. try{
  1136. return translate[lang]()[text];
  1137. }
  1138. catch{
  1139. return translate["en-US"]()[text];
  1140. }
  1141. }
  1142.  
  1143. /**
  1144. * showSetting
  1145. * Show script settings window
  1146. *
  1147. * @return {void}
  1148. */
  1149. function showSetting(){
  1150. $('.IG_SN_DIG').remove();
  1151. IG_createDM();
  1152. $('.IG_SN_DIG #post_info').text('IG Helper Settings');
  1153.  
  1154. $('.IG_SN_DIG .IG_SN_DIG_BODY').append(`<label class="globalSettings" title="${_i18n('AUTO_RENAME_INTRO')}"><span>${_i18n('AUTO_RENAME')}</span> <input id="AUTO_RENAME" value="box" type="checkbox" ${(USER_SETTING.AUTO_RENAME)?'checked':''}><div class="chbtn"><div class="rounds"></div></div></label>`);
  1155. $('.IG_SN_DIG .IG_SN_DIG_BODY').append(`<label class="globalSettings" title="${_i18n('RENAME_SHORTCODE_INTRO')}"><span>${_i18n('RENAME_SHORTCODE')}</span> <input id="RENAME_SHORTCODE" value="box" type="checkbox" ${(USER_SETTING.RENAME_SHORTCODE)?'checked':''}><div class="chbtn"><div class="rounds"></div></div></label>`);
  1156. $('.IG_SN_DIG .IG_SN_DIG_BODY').append(`<label class="globalSettings" title="${_i18n('DISABLE_VIDEO_LOOPING_INTRO')}"><span>${_i18n('DISABLE_VIDEO_LOOPING')}</span> <input id="DISABLE_VIDEO_LOOPING" value="box" type="checkbox" ${(USER_SETTING.DISABLE_VIDEO_LOOPING)?'checked':''}><div class="chbtn"><div class="rounds"></div></div></label>`);
  1157. $('.IG_SN_DIG .IG_SN_DIG_BODY').append(`<label class="globalSettings" title="${_i18n('REDIRECT_RIGHT_CLICK_USER_STORY_PICTURE_INTRO')}"><span>${_i18n('REDIRECT_RIGHT_CLICK_USER_STORY_PICTURE')}</span> <input id="REDIRECT_RIGHT_CLICK_USER_STORY_PICTURE" value="box" type="checkbox" ${(USER_SETTING.REDIRECT_RIGHT_CLICK_USER_STORY_PICTURE)?'checked':''}><div class="chbtn"><div class="rounds"></div></div></label>`);
  1158. $('.IG_SN_DIG .IG_SN_DIG_BODY').append(`<label class="globalSettings" title="${_i18n('FORCE_FETCH_ALL_RESOURCES_INTRO')}"><span>${_i18n('FORCE_FETCH_ALL_RESOURCES')}</span> <input id="FORCE_FETCH_ALL_RESOURCES" value="box" type="checkbox" ${(USER_SETTING.FORCE_FETCH_ALL_RESOURCES)?'checked':''}><div class="chbtn"><div class="rounds"></div></div></label>`);
  1159. }
  1160.  
  1161. /**
  1162. * showDebugDOM
  1163. * Show full DOM tree
  1164. *
  1165. * @return {void}
  1166. */
  1167. function showDebugDOM(text){
  1168. $('.IG_SN_DIG').remove();
  1169. IG_createDM();
  1170. $('.IG_SN_DIG #post_info').text('IG Debug DOM Tree');
  1171.  
  1172. $('.IG_SN_DIG .IG_SN_DIG_BODY').append(`<textarea style="width:100%;box-sizing: border-box;height:300px;"></textarea>`);
  1173. $('.IG_SN_DIG .IG_SN_DIG_BODY textarea').text(text.innerHTML);
  1174. }
  1175.  
  1176. // Running if document is ready
  1177. $(function(){
  1178. // Close the download dialog if user click the close icon
  1179. $('body').on('click','.IG_SN_DIG_BTN,.IG_SN_DIG_BG',function(){
  1180. $('.IG_SN_DIG').remove();
  1181. });
  1182.  
  1183. $(window).keydown(function(e){
  1184. // Hot key [Alt+Q] to close the download dialog
  1185. if (e.keyCode == '81' && e.altKey){
  1186. $('.IG_SN_DIG').remove();
  1187. e.preventDefault();
  1188. }
  1189. // Hot key [Alt+W] to open the settings dialog
  1190. if (e.keyCode == '87' && e.altKey){
  1191. showSetting();
  1192. e.preventDefault();
  1193. }
  1194.  
  1195. // Hot key [Alt+Z] to open the settings dialog
  1196. if (e.keyCode == '90' && e.altKey){
  1197. let text = $('div[id^="mount"]')[0];
  1198. showDebugDOM(text);
  1199. e.preventDefault();
  1200. }
  1201. });
  1202.  
  1203. $('body').on('change', '.IG_SN_DIG input',function(){
  1204. var isChecked = $(this).prop('checked');
  1205. var name = $(this).attr('id');
  1206.  
  1207. GM_setValue(name, isChecked);
  1208. USER_SETTING[name] = isChecked;
  1209. });
  1210.  
  1211. $('body').on('click','a[data-needed="direct"]',async function(){
  1212. let date = new Date().getTime();
  1213. let timestamp = Math.floor(date / 1000);
  1214. let username = ($(this).attr('data-username')) ? $(this).attr('data-username') : GL_username;
  1215.  
  1216. if(!username && $(this).attr('data-path')){
  1217. console.log('catching owner name from shortcode:',$(this).attr('data-href'));
  1218. username = await getPostOwner($(this).attr('data-path'));
  1219. }
  1220. saveFiles($(this).attr('data-href'),username,$(this).attr('data-name'),timestamp,$(this).attr('data-type'), $(this).attr('data-path'));
  1221. });
  1222.  
  1223. // Running if user left-click download icon in stories
  1224. $('body').on('click','.IG_DWSTORY',function(){
  1225. onStory(true);
  1226. });
  1227.  
  1228. // Running if user left-click download icon in stories
  1229. $('body').on('click','.IG_DWSTORY_THUMBNAIL',function(){
  1230. onStoryThumbnail(true);
  1231. });
  1232.  
  1233. // Running if user left-click download icon in profile
  1234. $('body').on('click','.IG_DWPROFILE',function(e){
  1235. e.stopPropagation();
  1236. onProfileAvatar(true);
  1237. });
  1238.  
  1239. // Running if user left-click download icon in highlight stories
  1240. $('body').on('click','.IG_DWHISTORY',function(){
  1241. onHighlightsStory(true);
  1242. });
  1243.  
  1244. // Running if user left-click download icon in highlight stories
  1245. $('body').on('click','.IG_DWHISTORY_THUMBNAIL',function(){
  1246. onHighlightsStoryThumbnail(true);
  1247. });
  1248.  
  1249. // Running if user left-click download icon in reels
  1250. $('body').on('click','.IG_REELS',function(){
  1251. onReels(true,true);
  1252. });
  1253.  
  1254. // Running if user left-click download icon in reels
  1255. $('body').on('click','.IG_REELS_THUMBNAIL',function(){
  1256. onReels(true,false);
  1257. });
  1258.  
  1259. // Running if user right-click profile picture in stories area
  1260. $('body').on('contextmenu','button[role="menuitem"]',function(){
  1261. if(location.href === 'https://www.instagram.com/' && USER_SETTING.REDIRECT_RIGHT_CLICK_USER_STORY_PICTURE){
  1262. if($(this).find('canvas._aarh').length > 0){
  1263. location.href = 'https://www.instagram.com/'+$(this).children('div').last().text();
  1264. }
  1265. }
  1266. });
  1267.  
  1268. $('body').on('change', '.IG_SN_DIG_TITLE .checkbox', function(){
  1269. var isChecked = $(this).find('input').prop('checked');
  1270. $('.IG_SN_DIG_BODY .inner_box').each(function(){
  1271. $(this).prop('checked', isChecked);
  1272. });
  1273. });
  1274.  
  1275. $('body').on('change', '.IG_SN_DIG_BODY .inner_box', function(){
  1276. var checked = $('.IG_SN_DIG_BODY .inner_box:checked').length;
  1277. var total = $('.IG_SN_DIG_BODY .inner_box').length;
  1278.  
  1279.  
  1280. $('.IG_SN_DIG_TITLE .checkbox').find('input').prop('checked', checked == total);
  1281. });
  1282.  
  1283. $('body').on('click', '.IG_SN_DIG_TITLE #batch_download_selected', function(){
  1284. let index = 0;
  1285. $('.IG_SN_DIG_BODY a[data-needed="direct"]').each(function(){
  1286. if($(this).prev().prop('checked')){
  1287. $(this).click();
  1288. index++;
  1289. }
  1290. });
  1291.  
  1292. if(index == 0){
  1293. alert(_i18n('NO_CHECK_RESOURCE'));
  1294. }
  1295. });
  1296.  
  1297. $('body').on('click', '.IG_SN_DIG_TITLE #batch_download_direct', function(){
  1298. $('.IG_SN_DIG_BODY a[data-needed="direct"]').each(function(){
  1299. $(this).click();
  1300. });
  1301. });
  1302. });
  1303. })(jQuery);