IG小助手

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

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

  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.4
  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. onReelsDW(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. onProfileDW(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. onHighlightsStoryDW(false);
  124. GL_repeat = setInterval(()=>{
  125. onHighlightsStoryThumbnailDW(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. onStoryDW(false);
  133. onStoryThumbnailDW(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.  
  147. // Direct Download Checkbox
  148. /*
  149. if(!$('.AutoDownload_dom').length){
  150. let ckValue = (GM_getValue('AutoDownload'))?'checked':'';
  151. $('body div.mfclru0v.astyfpdk.om3e55n1.jez8cy9q').css('position','relative');
  152. $('body div.mfclru0v.astyfpdk.om3e55n1.jez8cy9q').append(`<div class="AutoDownload_dom" style="position:absolute;left:10px;bottom:7px;padding:0px;line-height:1;display:inline-block;width:fit-content;"><label title="${_i18n("DDL_INTRO")}" style="cursor:help;"><input type="checkbox" value="1" class="AutoDownload" name="AutoDownload" ${ckValue} />${_i18n("DDL")}</label></div>`);
  153. }
  154. */
  155. },checkInterval);
  156.  
  157. // Call general function when user scroll the page
  158. $(document).scroll(function(){
  159. if(currentHeight != $(this).height() && location.href.split("?")[0] == "https://www.instagram.com/"){
  160. onReadyMyDW();
  161. }
  162. });
  163.  
  164. // Profile funcion
  165. async function onProfileDW(isDownload){
  166. if(isDownload){
  167. let date = new Date().getTime();
  168. let timestamp = Math.floor(date / 1000);
  169. let username = location.href.replace(/\/$/ig,'').split('/').at(-1);
  170. let userInfo = await getUserId(username);
  171. try{
  172. let dataURL = await getUserHighSizeProfile(userInfo.user.pk);
  173. saveFiles(dataURL,username,"avatar",timestamp,'jpg');
  174. }
  175. catch(err){
  176. saveFiles(userInfo.user.profile_pic_url,username,"avatar",timestamp,'jpg');
  177. }
  178. }
  179. else{
  180. // Add the profile download button
  181. if(!$('.IG_DWPROFILE').length){
  182. let profileTimer = setInterval(()=>{
  183. if($('.IG_DWPROFILE').length) clearInterval(profileTimer);
  184.  
  185. $('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>`);
  186. },150);
  187.  
  188. return true;
  189. }
  190. }
  191. }
  192.  
  193. // Highlight Stories funcion
  194. async function onHighlightsStoryDW(isDownload){
  195. if(isDownload){
  196. let date = new Date().getTime();
  197. let timestamp = Math.floor(date / 1000);
  198. let highlightId = location.href.replace(/\/$/ig,'').split('/').at(-1);
  199. let nowIndex = $("body > div section._ac0a header._ac0k > ._ac3r ._ac3n ._ac3p[style]").length;
  200. let username = "";
  201. let target = 0;
  202.  
  203. if(GL_dataCache.highlights[highlightId]){
  204. console.log('Fetch from memory cache:', highlightId);
  205.  
  206. let totIndex = GL_dataCache.highlights[highlightId].data.reels_media[0].items.length;
  207. username = GL_dataCache.highlights[highlightId].data.reels_media[0].owner.username;
  208. target = GL_dataCache.highlights[highlightId].data.reels_media[0].items[totIndex-nowIndex];
  209. }
  210. else{
  211. let highStories = await getHighlightsStories(highlightId);
  212. let totIndex = highStories.data.reels_media[0].items.length;
  213. username = highStories.data.reels_media[0].owner.username;
  214. target = highStories.data.reels_media[0].items[totIndex-nowIndex];
  215.  
  216. GL_dataCache.highlights[highlightId] = highStories;
  217. }
  218.  
  219. if(target.is_video){
  220. saveFiles(target.video_resources.at(-1).src,username,"highlights",timestamp,'mp4', highlightId);
  221. }
  222. else{
  223. saveFiles(target.display_resources.at(-1).src,username,"highlights",timestamp,'jpg', highlightId);
  224. }
  225. }
  226. else{
  227. // Add the stories download button
  228. if(!$('.IG_DWHISTORY').length){
  229. $('body > div section._ac0a').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>`);
  230. return true;
  231. }
  232. }
  233. }
  234.  
  235. // Highlight Stories Thumbnail funcion
  236. async function onHighlightsStoryThumbnailDW(isDownload){
  237. if(isDownload){
  238. let date = new Date().getTime();
  239. let timestamp = Math.floor(date / 1000);
  240. let highlightId = location.href.replace(/\/$/ig,'').split('/').at(-1);
  241. let username = "";
  242. let nowIndex = $("body > div section._ac0a header._ac0k > ._ac3r ._ac3n ._ac3p[style]").length;
  243. let target = "";
  244.  
  245. if(GL_dataCache.highlights[highlightId]){
  246. console.log('Fetch from memory cache:', highlightId);
  247.  
  248. let totIndex = GL_dataCache.highlights[highlightId].data.reels_media[0].items.length;
  249. username = GL_dataCache.highlights[highlightId].data.reels_media[0].owner.username;
  250. target = GL_dataCache.highlights[highlightId].data.reels_media[0].items[totIndex-nowIndex];
  251. }
  252. else{
  253. let highStories = await getHighlightsStories(highlightId);
  254. let totIndex = highStories.data.reels_media[0].items.length;
  255. username = highStories.data.reels_media[0].owner.username;
  256. target = highStories.data.reels_media[0].items[totIndex-nowIndex];
  257.  
  258. GL_dataCache.highlights[highlightId] = highStories;
  259. }
  260.  
  261. saveFiles(target.display_resources.at(-1).src,username,"highlights",timestamp,'jpg', highlightId);
  262. }
  263. else{
  264. if($('body > div section._ac0a video.xh8yej3').length){
  265. // Add the stories download button
  266. if(!$('.IG_DWHISTORY_THUMBNAIL').length){
  267. $('body > div section._ac0a').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>`);
  268. }
  269. }
  270. else{
  271. $('.IG_DWHISTORY_THUMBNAIL').remove();
  272. }
  273. }
  274. }
  275.  
  276. // Stories funcion
  277. async function onStoryDW(isDownload,isForce){
  278. if(isDownload){
  279. let date = new Date().getTime();
  280. let timestamp = Math.floor(date / 1000);
  281. let username = $("body > div section._ac0a header._ac0k ._ac0l a + div a").first().text() || location.pathname.split('/').at(2);
  282.  
  283. if($('body > div section._ac0a div._ac0b video.x1lliihq').length){
  284. // Download stories if it is video
  285. let type = "mp4";
  286. let videoURL = "";
  287. let targetURL = location.href.replace(/\/$/ig,'').split("/").at(-1);
  288.  
  289. if(GL_dataCache.stories[username] && !isForce){
  290. console.log('Fetch from memory cache:', username);
  291. GL_dataCache.stories[username].data.reels_media[0].items.forEach(item => {
  292. if(item.id == targetURL){
  293. videoURL = item.video_resources[0].src;
  294. }
  295. });
  296.  
  297. if(videoURL.length == 0){
  298. console.log('Memory cache not found, try fetch from API:', username);
  299. onStoryDW(true,true);
  300. return;
  301. }
  302. }
  303. else{
  304. let userInfo = await getUserId(username);
  305. let userId = userInfo.user.pk;
  306. let stories = await getStories(userId);
  307.  
  308. stories.data.reels_media[0].items.forEach(item => {
  309. if(item.id == targetURL){
  310. videoURL = item.video_resources[0].src;
  311. }
  312. });
  313.  
  314. GL_dataCache.stories[username] = stories;
  315. }
  316.  
  317. if(videoURL.length == 0){
  318. alert(_i18n("NO_VID_URL"));
  319. }
  320. else{
  321. saveFiles(videoURL,username,"stories",timestamp,type);
  322. }
  323.  
  324. }
  325. else{
  326. // Download stories if it is image
  327. let srcset = $('section._ac0a ._aa64 img._aa63').attr('srcset').split(',')[0].split(' ')[0];
  328. let link = (srcset)?srcset:$('section._ac0a ._aa64 img._aa63').attr('src');
  329.  
  330. let downloadLink = link;
  331. let type = 'jpg';
  332.  
  333. saveFiles(downloadLink,username,"stories",timestamp,type);
  334. }
  335. }
  336. else{
  337. // Add the stories download button
  338. let style = "position: absolute;right:-40px;top:15px;padding:5px;line-height:1;background:#fff;border-radius: 5px;cursor:pointer;";
  339. if(!$('.IG_DWSTORY').length){
  340. GL_dataCache.stories = {};
  341. $('body > div section._ac0a').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>`);
  342. return true;
  343. }
  344. }
  345. }
  346.  
  347. // Stories Thumbnail funcion
  348. async function onStoryThumbnailDW(isDownload,isForce){
  349. if(isDownload){
  350. // Download stories if it is video
  351. let date = new Date().getTime();
  352. let timestamp = Math.floor(date / 1000);
  353. let type = 'jpg';
  354. let username = $("body > div section._ac0a header._ac0k ._ac0l a + div a").first().text() || location.pathname.split('/').at(2);
  355. 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;';
  356. // Download thumbnail
  357. let targetURL = location.href.replace(/\/$/ig,'').split("/").at(-1);
  358. let videoThumbnailURL = "";
  359.  
  360.  
  361. if(GL_dataCache.stories[username] && !isForce){
  362. console.log('Fetch from memory cache:', username);
  363. GL_dataCache.stories[username].data.reels_media[0].items.forEach(item => {
  364. if(item.id == targetURL){
  365. videoThumbnailURL = item.display_url;
  366. }
  367. });
  368.  
  369. if(videoThumbnailURL.length == 0){
  370. console.log('Memory cache not found, try fetch from API:', username);
  371. onStoryThumbnailDW(true,true);
  372. return;
  373. }
  374. }
  375. else{
  376. let userInfo = await getUserId(username);
  377. let userId = userInfo.user.pk;
  378. let stories = await getStories(userId);
  379.  
  380. stories.data.reels_media[0].items.forEach(item => {
  381. if(item.id == targetURL){
  382. videoThumbnailURL = item.display_url;
  383. }
  384. });
  385. }
  386.  
  387. saveFiles(videoThumbnailURL,username,"thumbnail",timestamp,type);
  388. }
  389. else{
  390. if($('body > div section._ac0a video.xh8yej3').length){
  391. // Add the stories download button
  392. if(!$('.IG_DWSTORY_THUMBNAIL').length){
  393. $('body > div section._ac0a').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>`);
  394. }
  395. }
  396. else{
  397. $('.IG_DWSTORY_THUMBNAIL').remove();
  398. }
  399. }
  400. }
  401.  
  402. // Reels funcion
  403. async function onReelsDW(isDownload, isVideo){
  404. if(isDownload){
  405. let reelsPath = location.href.split('?').at(0).split('instagram.com/reels/').at(-1).replaceAll('/','');
  406. let data = await getBlobMedia(reelsPath);
  407. let timestamp = new Date().getTime();
  408.  
  409. if(isVideo && data.shortcode_media.is_video){
  410. let type = 'mp4';
  411. saveFiles(data.shortcode_media.video_url,data.shortcode_media.owner.username,"reels",timestamp,type,reelsPath);
  412. }
  413. else{
  414. let type = 'jpg';
  415. saveFiles(data.shortcode_media.display_resources.at(-1).src,data.shortcode_media.owner.username,"reels",timestamp,type,reelsPath);
  416. }
  417. }
  418. else{
  419. //$('.IG_REELS_THUMBNAIL, .IG_REELS').remove();
  420. var timer = setInterval(()=>{
  421. if($('section > main[role="main"] > div div.x1qjc9v5 video').length > 0){
  422. clearInterval(timer);
  423. $('section > main[role="main"] > div').children('div').each(function(){
  424. if($(this).children().length > 0){
  425. if(!$(this).children().find('.IG_REELS').length){
  426. $(this).children().css('position','relative');
  427.  
  428. $(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>`);
  429. $(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>`);
  430.  
  431. // Disable video autoplay
  432. if(USER_SETTING.DISABLE_VIDEO_LOOPING){
  433. $(this).find('video').each(function(){
  434. if(!$(this).data('loop')){
  435. console.log('Added video event listener');
  436. $(this).on('ended',function(){
  437. $(this).attr('data-loop', true);
  438. $(this).parent().find('.xpgaw4o').removeAttr('style');
  439. console.log('paused');
  440.  
  441. this.pause();
  442. });
  443. }
  444. });
  445. }
  446. }
  447. }
  448. });
  449. }
  450. },250);
  451. }
  452. }
  453.  
  454. // Prepare promise to fetch user stories
  455. function getHighlightsStories(highlightId){
  456. return new Promise((resolve,reject)=>{
  457. 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`;
  458.  
  459. GM_xmlhttpRequest({
  460. method: "GET",
  461. url: getURL,
  462. onload: function(response) {
  463. let obj = JSON.parse(response.response);
  464. resolve(obj);
  465. },
  466. onerror: function(err){
  467. reject(err);
  468. }
  469. });
  470. });
  471. }
  472.  
  473. // Prepare promise to fetch user stories
  474. function getStories(userId){
  475. return new Promise((resolve,reject)=>{
  476. 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`;
  477.  
  478. GM_xmlhttpRequest({
  479. method: "GET",
  480. url: getURL,
  481. onload: function(response) {
  482. let obj = JSON.parse(response.response);
  483. resolve(obj);
  484. },
  485. onerror: function(err){
  486. reject(err);
  487. }
  488. });
  489. });
  490. }
  491.  
  492. // Prepare promise to fetch user id by username
  493. function getUserId(username){
  494. return new Promise((resolve,reject)=>{
  495. let getURL = `https://www.instagram.com/web/search/topsearch/?query=${username}`;
  496.  
  497. GM_xmlhttpRequest({
  498. method: "GET",
  499. url: getURL,
  500. onload: function(response) {
  501. let obj = JSON.parse(response.response);
  502. resolve(obj.users[0]);
  503. },
  504. onerror: function(err){
  505. reject(err);
  506. }
  507. });
  508. });
  509. }
  510.  
  511. // Prepare promise to fetch user profile picture by user id
  512. function getUserHighSizeProfile(userId){
  513. return new Promise((resolve,reject)=>{
  514. let getURL = `https://i.instagram.com/api/v1/users/${userId}/info/`;
  515.  
  516. GM_xmlhttpRequest({
  517. method: "GET",
  518. url: getURL,
  519. headers: {
  520. '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'
  521. },
  522. onload: function(response) {
  523. let obj = JSON.parse(response.response);
  524. if(obj.status !== 'ok'){
  525. reject('faild');
  526. }
  527. else{
  528. resolve(obj.user.hd_profile_pic_url_info?.url);
  529. }
  530. },
  531. onerror: function(err){
  532. reject(err);
  533. }
  534. });
  535. });
  536. }
  537.  
  538. // Prepare promise to catch article author using shortcode
  539. function getPostOwner(postPath){
  540. return new Promise((resolve,reject)=>{
  541. if(!postPath) reject("NOPATH");
  542. let postShortCode = postPath;
  543. let getURL = `https://www.instagram.com/graphql/query/?query_hash=2c4c2e343a8f64c625ba02b2aa12c7f8&variables=%7B%22shortcode%22:%22${postShortCode}%22}`;
  544.  
  545. GM_xmlhttpRequest({
  546. method: "GET",
  547. url: getURL,
  548. onload: function(response) {
  549. let obj = JSON.parse(response.response);
  550. resolve(obj.data.shortcode_media.owner.username);
  551. },
  552. onerror: function(err){
  553. reject(err);
  554. }
  555. });
  556. });
  557. }
  558.  
  559. // Prepare promise to cache article which contains blob media
  560. function getBlobMedia(postPath){
  561. return new Promise((resolve,reject)=>{
  562. if(!postPath) reject("NOPATH");
  563. let postShortCode = postPath;
  564. let getURL = `https://www.instagram.com/graphql/query/?query_hash=2c4c2e343a8f64c625ba02b2aa12c7f8&variables=%7B%22shortcode%22:%22${postShortCode}%22}`;
  565.  
  566. GM_xmlhttpRequest({
  567. method: "GET",
  568. url: getURL,
  569. onload: function(response) {
  570. let obj = JSON.parse(response.response);
  571. console.log(obj);
  572. resolve(obj.data);
  573. },
  574. onerror: function(err){
  575. reject(err);
  576. }
  577. });
  578. });
  579. }
  580.  
  581. // Main function
  582. function onReadyMyDW(NoDialog){
  583. // Whether is Instagram dialog?
  584. if(!NoDialog){
  585. // Running if it is dialog
  586. $('article, main > div > div.xdt5ytf > div').each(function(){
  587. $(this).removeAttr('data-snig');
  588. $(this).unbind('click');
  589. });
  590. $('.SNKMS_IG_DW_MAIN,.SNKMS_IG_DW_MAIN_VIDEO').remove();
  591. }
  592. if(NoDialog == false){
  593. var repeat = setInterval(() => {
  594. // div.x13ehr01 << (sigle post in top, not floating) >>
  595. if($('article[data-snig="canDownload"], main > div > div.xdt5ytf > div[data-snig="canDownload"]').length > 0) clearInterval(repeat);
  596. createDownloadButton();
  597. },250);
  598. }
  599. else{
  600. createDownloadButton();
  601. }
  602. }
  603.  
  604. function createDownloadButton(){
  605. // Add download icon per each posts
  606. $('article, main > div > div.xdt5ytf > div').each(function(){
  607. // If it is have not download icon
  608. if(!$(this).attr('data-snig') && !$(this).hasClass('x1iyjqo2')){
  609. console.log("Found article");
  610. var rightPos = 15;
  611. if(this.tagName === "DIV"){
  612. rightPos = ($(this).children('div:last-child').width() !== undefined) ? $(this).children('div:last-child').width()+15 : 335+15
  613. }
  614.  
  615. // Add the download icon
  616. let $childElement = $(this).children("div").children("div");
  617. $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>`);
  618.  
  619. // Disable video autoplay
  620. if(USER_SETTING.DISABLE_VIDEO_LOOPING){
  621. $(this).find('video').each(function(){
  622. if(!$(this).data('loop')){
  623. console.log('Added video event listener');
  624. $(this).on('ended',function(){
  625. $(this).attr('data-loop', true);
  626. this.pause();
  627. });
  628. }
  629. });
  630. }
  631.  
  632. // Running if user click the download icon
  633. $(this).on('click','.SNKMS_IG_DW_MAIN', async function(e){
  634. GL_username = $(this).parent().parent().parent().attr('data-username');
  635. 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);
  636.  
  637. // Create element that download dailog
  638. IG_createDM(GM_getValue('AutoDownload'), true);
  639.  
  640. $("#article-id").html(`<a href="https://www.instagram.com/p/${GL_postPath}">${GL_postPath}</a>`);
  641.  
  642. // Find video/image element and add the download icon
  643. var s = 0;
  644. var multiple = $(this).parent().parent().find('._aap0 ._acaz').length;
  645. var pathname = window.location.pathname;
  646. var fullpathname = "/"+pathname.split('/')[1]+"/"+pathname.split('/')[2]+"/";
  647. var blob = USER_SETTING.FORCE_FETCH_ALL_RESOURCES;
  648.  
  649. // If posts have more than one images or videos.
  650. if(multiple){
  651. $(this).parent().find('._aap0 ._acaz').each(function(){
  652. let element_videos = $(this).parent().parent().find('video');
  653. //if(element_videos && element_videos.attr('src') && element_videos.attr('src').match(/^blob:/ig)){
  654. if(element_videos && element_videos.attr('src')){
  655. blob = true;
  656. }
  657. });
  658.  
  659.  
  660. if(blob){
  661. createMediaCacheDOM(GL_postPath,".IG_SN_DIG .IG_SN_DIG_MAIN .IG_SN_DIG_BODY",_i18n("LOAD_BLOB_MULTIPLE"));
  662. }
  663. else{
  664. $(this).parent().find('._aap0 ._acaz').each(function(){
  665. s++;
  666. let element_videos = $(this).find('video');
  667. let element_images = $(this).find('._aagv img');
  668. let imgLink = (element_images.attr('srcset'))?element_images.attr('srcset').split(" ")[0]:element_images.attr('src');
  669.  
  670. if(element_videos && element_videos.attr('src')){
  671. blob = true;
  672. /*
  673. let video_image = element_videos.attr('poster');
  674. let video_url = element_videos.attr('src');
  675.  
  676. $('.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>`);
  677.  
  678. if(video_url.match(/^blob:/ig)) blob = true;
  679. */
  680. }
  681. if(element_images && imgLink){
  682. $('.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>`);
  683. }
  684.  
  685. });
  686.  
  687. if(blob){
  688. createMediaCacheDOM(GL_postPath,".IG_SN_DIG .IG_SN_DIG_MAIN .IG_SN_DIG_BODY",_i18n("LOAD_BLOB_RELOAD"));
  689. }
  690. }
  691. }
  692. else{
  693. s++;
  694. let element_videos = $(this).parent().parent().find('video');
  695. let element_images = $(this).parent().parent().find('._aagv img');
  696. let imgLink = (element_images.attr('srcset'))?element_images.attr('srcset').split(" ")[0]:element_images.attr('src');
  697.  
  698.  
  699. if(element_videos && element_videos.attr('src')){
  700. createMediaCacheDOM(GL_postPath,".IG_SN_DIG .IG_SN_DIG_MAIN .IG_SN_DIG_BODY",_i18n("LOAD_BLOB_ONE"));
  701.  
  702. /*
  703. let video_image = element_videos.attr('poster');
  704. let video_url = element_videos.attr('src');
  705.  
  706. if(element_videos.attr('src').match(/^blob:/ig)){
  707. createMediaCacheDOM(GL_postPath,".IG_SN_DIG .IG_SN_DIG_MAIN .IG_SN_DIG_BODY",_i18n("LOAD_BLOB_ONE"));
  708. }
  709. else{
  710. $('.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>`);
  711. }
  712. */
  713. }
  714. if(element_images && imgLink){
  715. $('.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>`);
  716. }
  717. }
  718.  
  719.  
  720. $('.IG_SN_DIG .IG_SN_DIG_MAIN .IG_SN_DIG_BODY a').each(function(){
  721. $(this).wrap('<div></div>');
  722. $(this).before('<label class="inner_box_wrapper"><input class="inner_box" type="checkbox"><span></span></label>');
  723. });
  724. });
  725.  
  726. // Add the mark that download is ready
  727. var username = $(this).find("header > div:last-child > div:first-child span > a").first().text();
  728.  
  729. $(this).attr('data-snig','canDownload');
  730. $(this).attr('data-username',username);
  731. }
  732. });
  733. }
  734.  
  735. // Create media element from blob media
  736. async function createMediaCacheDOM(postURL,selector,message){
  737. $(`${selector} a`).remove();
  738. $(selector).append('<p id="_SNLOAD">'+ message +'</p>');
  739. let media = await getBlobMedia(postURL);
  740. let idx = 1;
  741. let resource = media.shortcode_media;
  742.  
  743. // GraphVideo
  744. if(resource.__typename == "GraphVideo" && resource.video_url){
  745. $(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>`);
  746. idx++;
  747. }
  748. // GraphSidecar
  749. if(resource.__typename == "GraphSidecar" && resource.edge_sidecar_to_children){
  750. for(let e of resource.edge_sidecar_to_children.edges){
  751. if(e.node.__typename == "GraphVideo"){
  752. $(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>`);
  753. }
  754.  
  755. if(e.node.__typename == "GraphImage"){
  756. $(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>`);
  757. }
  758. idx++;
  759. }
  760. }
  761. $("#_SNLOAD").remove();
  762. $('.IG_SN_DIG .IG_SN_DIG_MAIN .IG_SN_DIG_BODY a').each(function(){
  763. $(this).wrap('<div></div>');
  764. $(this).before('<label class="inner_box_wrapper"><input class="inner_box" type="checkbox"><span></span></label>');
  765. });
  766. }
  767.  
  768. // Create the download dialog element funcion
  769. function IG_createDM(hasHidden, hasCheckbox){
  770. let isHidden = (hasHidden)?"hidden":"";
  771. $('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>');
  772. $('.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 style="line-height: 14px;font-size:14px;">Article: <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>');
  773.  
  774. if(hasCheckbox){
  775. $('.IG_SN_DIG .IG_SN_DIG_MAIN .IG_SN_DIG_TITLE').append(`<div style="text-align: center;" id="button_group"></div>`);
  776. $('.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>`);
  777. $('.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>`);
  778. $('.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>`);
  779. }
  780. }
  781.  
  782. // Download and rename files
  783. function saveFiles(downloadLink,username,index,timestamp,type,shortcode){
  784. fetch(downloadLink).then(res => {
  785. return res.blob().then(dwel => {
  786. const a = document.createElement("a");
  787. const name = username+'-'+index+'-'+((USER_SETTING.RENAME_SHORTCODE && shortcode)?shortcode+'-':'')+timestamp+'.'+type;
  788. const originally = username + '_' + downloadLink.split('/').at(-1).split('?').at(0);
  789.  
  790. a.href = URL.createObjectURL(dwel);
  791. a.setAttribute("download", (USER_SETTING.AUTO_RENAME)?name:originally);
  792. a.click();
  793. a.remove();
  794. });
  795. });
  796. }
  797.  
  798. // Supported language list
  799. function translateText(lang){
  800. return {
  801. "zh-TW": {
  802. "CLOSE": "關閉",
  803. "ALL_CHECK": "全選",
  804. "BATCH_DOWNLOAD_SELECTED": "批次下載已勾選資源",
  805. "BATCH_DOWNLOAD_DIRECT": "批次下載全部資源",
  806. "IMG": "相片",
  807. "VID": "影片",
  808. "DDL": "快速下載",
  809. "DDL_INTRO": "勾選後將直接下載點選當下位置的相片/影片",
  810. "DW": "下載",
  811. "THUMBNAIL_INTRO": "下載影片縮圖",
  812. "LOAD_BLOB_ONE": "正在載入二進位大型物件...",
  813. "LOAD_BLOB_MULTIPLE": "正在載入多個二進位大型物件...",
  814. "LOAD_BLOB_RELOAD": "正在重新載入二進位大型物件...",
  815. "NO_CHECK_RESOURCE": "您需要勾選資源才能下載。",
  816. "NO_VID_URL": "找不到影片網址",
  817. "SETTING": "設定",
  818. "AUTO_RENAME": "自動重新命名檔案",
  819. "RENAME_SHORTCODE": "重新命名檔案並包含 Shortcode",
  820. "DISABLE_VIDEO_LOOPING": "關閉影片自動循環播放",
  821. "REDIRECT_RIGHT_CLICK_USER_STORY_PICTURE": "右鍵點擊使用者限時動態區域頭貼時重定向",
  822. "FORCE_FETCH_ALL_RESOURCES": "強制提取貼文中所有資源",
  823. "AUTO_RENAME_INTRO": "將檔案自動重新命名為以下格式:\n使用者名稱-類型-時間戳.檔案類型\n例如:instagram-photo-1670350000.jpg\n\n若設為 false,則檔案名稱將保持原始樣貌。 \n例如:instagram_321565527_679025940443063_4318007696887450953_n.jpg",
  824. "RENAME_SHORTCODE_INTRO": "將檔案自動重新命名為以下格式:\n使用者名稱-類型-Shortcode-時間戳.檔案類型\n示例:instagram-photo-CwkxyiVynpW-1670350000.jpg\n\n此功能僅在[自動重新命名檔案]設定為 TRUE 時有效。",
  825. "DISABLE_VIDEO_LOOPING_INTRO": "關閉連續短片和貼文中影片自動循環播放。",
  826. "REDIRECT_RIGHT_CLICK_USER_STORY_PICTURE_INTRO": "右鍵點選首頁限時動態區域中的使用者頭貼時,重新導向到使用者的個人資料頁面。",
  827. "FORCE_FETCH_ALL_RESOURCES_INTRO": "透過 Instagram API 強制取得貼文中的所有資源(照片和影片),以取消每個貼文單次提取三個資源的限制。",
  828. },
  829. "zh-CN": {
  830. "CLOSE": "关闭",
  831. "ALL_CHECK": "全选",
  832. "BATCH_DOWNLOAD_SELECTED": "批量下载已勾选资源",
  833. "BATCH_DOWNLOAD_DIRECT": "批量下载全部资源",
  834. "IMG": "图像",
  835. "VID": "视频",
  836. "DDL": "便捷下载",
  837. "DDL_INTRO": "勾选后将直接下载點擊當下位置的图像/视频",
  838. "DW": "下载",
  839. "THUMBNAIL_INTRO": "下载视频缩略图",
  840. "LOAD_BLOB_ONE": "正在载入大型媒体对象...",
  841. "LOAD_BLOB_MULTIPLE": "正在载入多个大型媒体对象...",
  842. "LOAD_BLOB_RELOAD": "正在重新载入大型媒体对象...",
  843. "NO_CHECK_RESOURCE": "您需要勾選资源才能下載。",
  844. "NO_VID_URL": "找不到视频网址",
  845. "SETTING": "设置",
  846. "AUTO_RENAME": "自动重命名文件",
  847. "RENAME_SHORTCODE": "重命名文件并包含物件短码",
  848. "DISABLE_VIDEO_LOOPING": "禁用视频自动循环",
  849. "REDIRECT_RIGHT_CLICK_USER_STORY_PICTURE": "右键单击用户故事区域头像时重定向",
  850. "FORCE_FETCH_ALL_RESOURCES": "强制抓取帖子中所有资源",
  851. "AUTO_RENAME_INTRO": "将文件自动重新命名为以下格式类型:\n用户名-类型-时间戳.文件类型\n例如:instagram-photo-1670350000.jpg\n\n若设为false,则文件名将保持原样。 \n例如:instagram_321565527_679025940443063_4318007696887450953_n.jpg",
  852. "RENAME_SHORTCODE_INTRO": "自动重命名文件为以下格式类型:\n用户名-类型-短码-时间戳.文件类型\n示例:instagram-photo-CwkxyiVynpW-1670350000.jpg\n\n它仅在[自动重命名文件]设置为 TRUE 时有效。",
  853. "DISABLE_VIDEO_LOOPING_INTRO": "禁用 Reels 和帖子中的视频自动播放。",
  854. "REDIRECT_RIGHT_CLICK_USER_STORY_PICTURE_INTRO": "右键单击主页故事区域中的用户头像,重定向到用户的个人资料页面。",
  855. "FORCE_FETCH_ALL_RESOURCES_INTRO": "通过 Instagram API 强制获取帖子中的所有资源(照片和视频),以取消每个帖子单次抓取三个资源的限制。"
  856. },
  857. "en-US": {
  858. "CLOSE": "Close",
  859. "ALL_CHECK": "Select All",
  860. "BATCH_DOWNLOAD_SELECTED": "Download Selected Resources",
  861. "BATCH_DOWNLOAD_DIRECT": "Download All Resources",
  862. "IMG": "Image",
  863. "VID": "Video",
  864. "DDL": "Quick Download",
  865. "DDL_INTRO": "Checking it will direct download current photo/media in the posts.",
  866. "DW": "Download",
  867. "THUMBNAIL_INTRO": "Download video thumbnail.",
  868. "LOAD_BLOB_ONE": "Loading Blob Media...",
  869. "LOAD_BLOB_MULTIPLE": "Loading Blob Media and others...",
  870. "LOAD_BLOB_RELOAD": "Detect Blob Media, now reloading...",
  871. "NO_CHECK_RESOURCE": "You need to check resource to download.",
  872. "NO_VID_URL": "Can not find video url.",
  873. "SETTING": "Settings",
  874. "AUTO_RENAME": "Automatically Rename Files",
  875. "RENAME_SHORTCODE": "Rename The File and Include Shortcode",
  876. "DISABLE_VIDEO_LOOPING": "Disable Video Auto-looping",
  877. "REDIRECT_RIGHT_CLICK_USER_STORY_PICTURE": "Redirect When Right-Clicking User Story Picture",
  878. "FORCE_FETCH_ALL_RESOURCES": "Forcing Fetch All Resources In the Post",
  879. "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",
  880. "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.",
  881. "DISABLE_VIDEO_LOOPING_INTRO": "Disable video auto-looping in reels and posts.",
  882. "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.",
  883. "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."
  884. }
  885. };
  886. }
  887.  
  888. // Translate display text to user country
  889. function _i18n(text){
  890. let userLang = (lang)?lang:"en-US";
  891. let translate = {
  892. "zh-TW": function(){
  893. return translateText()["zh-TW"];
  894. },
  895. "zh-HK": function(){
  896. return translateText()["zh-TW"];
  897. },
  898. "zh-MO": function(){
  899. return translateText()["zh-TW"];
  900. },
  901. "zh-CN": function(){
  902. return translateText()["zh-CN"];
  903. },
  904. "en-US": function(){
  905. return translateText()["en-US"];
  906. }
  907. }
  908.  
  909. try{
  910. return translate[lang]()[text];
  911. }
  912. catch{
  913. return translate["en-US"]()[text];
  914. }
  915. }
  916.  
  917. function showSetting(){
  918. $('.IG_SN_DIG').remove();
  919. IG_createDM();
  920. $('.IG_SN_DIG #article-id').text('IG Helper Settings');
  921.  
  922. $('.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>`);
  923. $('.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>`);
  924. $('.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>`);
  925. $('.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>`);
  926. $('.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>`);
  927. }
  928.  
  929. // Running if document is ready
  930. $(function(){
  931. // Close the download dialog if user click the close icon
  932. $('body').on('click','.IG_SN_DIG_BTN,.IG_SN_DIG_BG',function(){
  933. $('.IG_SN_DIG').remove();
  934. });
  935.  
  936. $(window).keydown(function(e){
  937. // Hot key [Alt+Q] to close the download dialog
  938. if (e.keyCode == '81' && e.altKey){
  939. $('.IG_SN_DIG').remove();
  940. e.preventDefault();
  941. }
  942. // Hot key [Alt+W] to open the settings dialog
  943. if (e.keyCode == '87' && e.altKey){
  944. showSetting();
  945. e.preventDefault();
  946. }
  947. });
  948.  
  949. $('body').on('change', '.IG_SN_DIG input',function(){
  950. var isChecked = $(this).prop('checked');
  951. var name = $(this).attr('id');
  952.  
  953. GM_setValue(name, isChecked);
  954. USER_SETTING[name] = isChecked;
  955. });
  956.  
  957. $('body').on('click','a[data-needed="direct"]',async function(){
  958. let date = new Date().getTime();
  959. let timestamp = Math.floor(date / 1000);
  960. let username = ($(this).attr('data-username')) ? $(this).attr('data-username') : GL_username;
  961.  
  962. if(!username && $(this).attr('data-path')){
  963. console.log('catching owner name from shortcode:',$(this).attr('data-href'));
  964. username = await getPostOwner($(this).attr('data-path'));
  965. }
  966. saveFiles($(this).attr('data-href'),username,$(this).attr('data-name'),timestamp,$(this).attr('data-type'), $(this).attr('data-path'));
  967. });
  968.  
  969. // Running if user left-click download icon in stories
  970. $('body').on('click','.IG_DWSTORY',function(){
  971. onStoryDW(true);
  972. });
  973.  
  974. // Running if user left-click download icon in stories
  975. $('body').on('click','.IG_DWSTORY_THUMBNAIL',function(){
  976. onStoryThumbnailDW(true);
  977. });
  978.  
  979. // Running if user left-click download icon in profile
  980. $('body').on('click','.IG_DWPROFILE',function(e){
  981. e.stopPropagation();
  982. onProfileDW(true);
  983. });
  984.  
  985. // Running if user left-click download icon in highlight stories
  986. $('body').on('click','.IG_DWHISTORY',function(){
  987. onHighlightsStoryDW(true);
  988. });
  989.  
  990. // Running if user left-click download icon in highlight stories
  991. $('body').on('click','.IG_DWHISTORY_THUMBNAIL',function(){
  992. onHighlightsStoryThumbnailDW(true);
  993. });
  994.  
  995. // Running if user left-click download icon in reels
  996. $('body').on('click','.IG_REELS',function(){
  997. onReelsDW(true,true);
  998. });
  999.  
  1000. // Running if user left-click download icon in reels
  1001. $('body').on('click','.IG_REELS_THUMBNAIL',function(){
  1002. onReelsDW(true,false);
  1003. });
  1004.  
  1005. // Running if user right-click profile picture in stories area
  1006. $('body').on('contextmenu','button[role="menuitem"]',function(){
  1007. if(location.href === 'https://www.instagram.com/' && USER_SETTING.REDIRECT_RIGHT_CLICK_USER_STORY_PICTURE){
  1008. if($(this).find('canvas._aarh').length > 0){
  1009. location.href = 'https://www.instagram.com/'+$(this).children('div').last().text();
  1010. }
  1011. }
  1012. });
  1013.  
  1014. $('body').on('change', '.IG_SN_DIG_TITLE .checkbox', function(){
  1015. var isChecked = $(this).find('input').prop('checked');
  1016. $('.IG_SN_DIG_BODY .inner_box').each(function(){
  1017. $(this).prop('checked', isChecked);
  1018. });
  1019. });
  1020.  
  1021. $('body').on('change', '.IG_SN_DIG_BODY .inner_box', function(){
  1022. var checked = $('.IG_SN_DIG_BODY .inner_box:checked').length;
  1023. var total = $('.IG_SN_DIG_BODY .inner_box').length;
  1024.  
  1025.  
  1026. $('.IG_SN_DIG_TITLE .checkbox').find('input').prop('checked', checked == total);
  1027. });
  1028.  
  1029. $('body').on('click', '.IG_SN_DIG_TITLE #batch_download_selected', function(){
  1030. let index = 0;
  1031. $('.IG_SN_DIG_BODY a[data-needed="direct"]').each(function(){
  1032. if($(this).prev().prop('checked')){
  1033. $(this).click();
  1034. index++;
  1035. }
  1036. });
  1037.  
  1038. if(index == 0){
  1039. alert(_i18n('NO_CHECK_RESOURCE'));
  1040. }
  1041. });
  1042.  
  1043. $('body').on('click', '.IG_SN_DIG_TITLE #batch_download_direct', function(){
  1044. $('.IG_SN_DIG_BODY a[data-needed="direct"]').each(function(){
  1045. $(this).click();
  1046. });
  1047. });
  1048. });
  1049. })(jQuery);