IG小助手

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

当前为 2022-11-07 提交的版本,查看 最新版本

  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.5.2.5
  9. // @description Downloading Instagram posts photos and videos or their stories!
  10. // @description:zh-TW 一鍵下載對方 Instagram 貼文中的相片、影片甚至是他們的限時動態!
  11. // @description:zh-CN 一键下载对方 Instagram 帖子中的相片、视频甚至是他们的快拍!
  12. // @description:ja 写真、ビデオ、そしてお互いの Instagram 投稿からのストーリーずズのワンクリックダウンロード!
  13. // @description:ko Instagram 게시물에서 사진, 비디오 또는 이야기를 다운로드하십시오.
  14. // @author SN-Koarashi (5026)
  15. // @match https://*.instagram.com/*
  16. // @grant GM_setValue
  17. // @grant GM_getValue
  18. // @grant GM_xmlhttpRequest
  19. // @require https://code.jquery.com/jquery-3.6.0.min.js
  20. // @supportURL https://www.facebook.com/smileopwe/
  21. // @icon https://www.google.com/s2/favicons?domain=www.instagram.com
  22. // @compatible firefox >= 87
  23. // @compatible chrome >= 90
  24. // @compatible edge >= 90
  25. // @license GPLv3
  26. // ==/UserScript==
  27.  
  28. (function($) {
  29. 'use strict';
  30. // Icon download by https://www.flaticon.com/authors/pixel-perfect
  31.  
  32. const checkInterval = 250;
  33. const lang = navigator.language || navigator.userLanguage;
  34.  
  35. var currentURL = location.href;
  36. var currentHeight = $(document).height();
  37. var firstStarted = false;
  38. var pageLoaded = false;
  39.  
  40. var GL_postPath;
  41. var GL_username;
  42. var GL_repeat
  43. var GL_dataCache = {
  44. stories: {},
  45. highlights: {}
  46. };
  47.  
  48. // Main Timer
  49. var timer = setInterval(function(){
  50. currentHeight = $(document).height();
  51.  
  52. // Call Instagram dialog function if url changed.
  53. if(currentURL != location.href || !firstStarted || !pageLoaded){
  54. clearInterval(GL_repeat);
  55. pageLoaded = false;
  56. firstStarted = true;
  57. currentURL = location.href;
  58.  
  59. if(location.href.startsWith("https://www.instagram.com/p/") || location.href.startsWith("https://www.instagram.com/reel/")){
  60. GL_dataCache.stories = {};
  61.  
  62. console.log('isDialog');
  63. setTimeout(()=>{
  64. onReadyMyDW(false);
  65. },150);
  66. pageLoaded = true;
  67. }
  68.  
  69. if(location.href.split("?")[0] == "https://www.instagram.com/"){
  70. GL_dataCache.stories = {};
  71.  
  72. console.log('isHomepage');
  73. setTimeout(()=>{
  74. onReadyMyDW(false);
  75. },150);
  76. pageLoaded = true;
  77. }
  78. if(!$('body > div div.x9f619 div._adqx[data-visualcompletion="loading-state"]').length && $('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)){
  79. console.log('isProfile');
  80. setTimeout(()=>{
  81. onProfileDW(false);
  82. },150);
  83. pageLoaded = true;
  84. }
  85.  
  86. if(!pageLoaded){
  87. // Call Instagram stories function
  88. if(location.href.match(/^(https:\/\/www\.instagram\.com\/stories\/highlights\/)/ig)){
  89. GL_dataCache.highlights = {};
  90.  
  91. console.log('isHighlightsStory');
  92. onHighlightsStoryDW(false);
  93. GL_repeat = setInterval(()=>{
  94. onHighlightsStoryThumbnailDW(false);
  95. },checkInterval);
  96.  
  97. if($(".IG_DWHISTORY").length) setTimeout(()=>{pageLoaded = true;},150);
  98. }
  99. else if(location.href.match(/^(https:\/\/www\.instagram\.com\/stories\/)/ig)){
  100. console.log('isStory');
  101. onStoryDW(false);
  102. onStoryThumbnailDW(false);
  103.  
  104. if($(".IG_DWSTORY").length) setTimeout(()=>{pageLoaded = true;},150);
  105. }
  106. else{
  107. pageLoaded = false;
  108. // Remove the download icon
  109. $('.IG_DWSTORY').remove();
  110. $('.IG_DWSTORY_THUMBNAIL').remove();
  111. }
  112. }
  113.  
  114. }
  115.  
  116. // Direct Download Checkbox
  117. /*
  118. if(!$('.AutoDownload_dom').length){
  119. let ckValue = (GM_getValue('AutoDownload'))?'checked':'';
  120. $('body div.mfclru0v.astyfpdk.om3e55n1.jez8cy9q').css('position','relative');
  121. $('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>`);
  122. }
  123. */
  124. },checkInterval);
  125.  
  126. // Call general function when user scroll the page
  127. $(document).scroll(function(){
  128. if(currentHeight != $(this).height() && location.href.split("?")[0] == "https://www.instagram.com/"){
  129. onReadyMyDW();
  130. }
  131. });
  132.  
  133. // Profile funcion
  134. async function onProfileDW(isDownload){
  135. if(isDownload){
  136. let date = new Date().getTime();
  137. let timestamp = Math.floor(date / 1000);
  138. let username = location.href.replace(/\/$/ig,'').split('/').at(-1);
  139. let userInfo = await getUserId(username);
  140.  
  141. saveFiles(userInfo.user.profile_pic_url,username,"avatar",timestamp,'jpg');
  142. }
  143. else{
  144. // Add the stories download button
  145. let style = "position: absolute;right:0px;top:0px;padding:5px;line-height:1;background:#fff;border-radius: 50%;cursor:pointer;border: 1px solid #ccc";
  146. if(!$('.IG_DWPROFILE').length){
  147. $('body > div main canvas._aarh').parent().append(`<div title="${_i18n("DW")}" class="IG_DWPROFILE" style="${style}"><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>`);
  148. return true;
  149. }
  150. }
  151. }
  152.  
  153. // Highlight Stories funcion
  154. async function onHighlightsStoryDW(isDownload){
  155. if(isDownload){
  156. let date = new Date().getTime();
  157. let timestamp = Math.floor(date / 1000);
  158. let highlightId = location.href.replace(/\/$/ig,'').split('/').at(-1);
  159. let nowIndex = $("body > div section._ac0a header._ac0k > ._ac3r ._ac3n ._ac3p[style]").length;
  160. let username = "";
  161. let target = 0;
  162.  
  163. if(GL_dataCache.highlights[highlightId]){
  164. console.log('Fetch from memory cache:', highlightId);
  165.  
  166. let totIndex = GL_dataCache.highlights[highlightId].data.reels_media[0].items.length;
  167. username = GL_dataCache.highlights[highlightId].data.reels_media[0].owner.username;
  168. target = GL_dataCache.highlights[highlightId].data.reels_media[0].items[totIndex-nowIndex];
  169. }
  170. else{
  171. let highStories = await getHighlightsStories(highlightId);
  172. let totIndex = highStories.data.reels_media[0].items.length;
  173. username = highStories.data.reels_media[0].owner.username;
  174. target = highStories.data.reels_media[0].items[totIndex-nowIndex];
  175.  
  176. GL_dataCache.highlights[highlightId] = highStories;
  177. }
  178.  
  179. if(target.is_video){
  180. saveFiles(target.video_resources.at(-1).src,username,"highlights",timestamp,'mp4');
  181. }
  182. else{
  183. saveFiles(target.display_resources.at(-1).src,username,"highlights",timestamp,'jpg');
  184. }
  185. }
  186. else{
  187. // Add the stories download button
  188. let style = "position: absolute;right:-40px;top:15px;padding:5px;line-height:1;background:#fff;border-radius: 5px;cursor:pointer;";
  189. if(!$('.IG_DWHISTORY').length){
  190. $('body > div section._ac0a').append(`<div title="${_i18n("DW")}" class="IG_DWHISTORY" style="${style}"><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>`);
  191. return true;
  192. }
  193. }
  194. }
  195.  
  196. // Highlight Stories Thumbnail funcion
  197. async function onHighlightsStoryThumbnailDW(isDownload){
  198. if(isDownload){
  199. let date = new Date().getTime();
  200. let timestamp = Math.floor(date / 1000);
  201. let highlightId = location.href.replace(/\/$/ig,'').split('/').at(-1);
  202. let username = "";
  203. let nowIndex = $("body > div section._ac0a header._ac0k > ._ac3r ._ac3n ._ac3p[style]").length;
  204. let target = "";
  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 getHighlightsStories(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. saveFiles(target.display_resources.at(-1).src,username,"highlights",timestamp,'jpg');
  223. }
  224. else{
  225. if($('body > div section._ac0a video.xh8yej3').length){
  226. // Add the stories download button
  227. let style = "position: absolute;right:-40px;top:45px;padding:5px;line-height:1;background:#fff;border-radius: 5px;cursor:pointer;";
  228. if(!$('.IG_DWHISTORY_THUMBNAIL').length){
  229. $('body > div section._ac0a').append(`<div title="${_i18n("THUMBNAIL_INTRO")}" class="IG_DWHISTORY_THUMBNAIL" style="${style}"><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>`);
  230. }
  231. }
  232. else{
  233. $('.IG_DWHISTORY_THUMBNAIL').remove();
  234. }
  235. }
  236. }
  237.  
  238. // Stories funcion
  239. async function onStoryDW(isDownload,isForce){
  240. if(isDownload){
  241. let date = new Date().getTime();
  242. let timestamp = Math.floor(date / 1000);
  243. let username = $("body > div section._ac0a header._ac0k ._ac0l div ._ac0q > a").text();
  244.  
  245. if($('body > div section._ac0a video.xh8yej3').length){
  246. // Download stories if it is video
  247. let type = "mp4";
  248. let videoURL = "";
  249. let targetURL = location.href.replace(/\/$/ig,'').split("/").at(-1);
  250.  
  251. if(GL_dataCache.stories[username] && !isForce){
  252. console.log('Fetch from memory cache:', username);
  253. GL_dataCache.stories[username].data.reels_media[0].items.forEach(item => {
  254. if(item.id == targetURL){
  255. videoURL = item.video_resources[0].src;
  256. }
  257. });
  258.  
  259. if(videoURL.length == 0){
  260. console.log('Memory cache not found, try fetch from API:', username);
  261. onStoryDW(true,true);
  262. return;
  263. }
  264. }
  265. else{
  266. let userInfo = await getUserId(username);
  267. let userId = userInfo.user.pk;
  268. let stories = await getStories(userId);
  269.  
  270. stories.data.reels_media[0].items.forEach(item => {
  271. if(item.id == targetURL){
  272. videoURL = item.video_resources[0].src;
  273. }
  274. });
  275.  
  276. GL_dataCache.stories[username] = stories;
  277. }
  278.  
  279. if(videoURL.length == 0){
  280. alert(_i18n("NO_VID_URL"));
  281. }
  282. else{
  283. saveFiles(videoURL,username,"stories",timestamp,type);
  284. }
  285.  
  286. }
  287. else{
  288. // Download stories if it is image
  289. let srcset = $('section._ac0a ._aa64 img._aa63').attr('srcset').split(',')[0].split(' ')[0];
  290. let link = (srcset)?srcset:$('section._ac0a ._aa64 img._aa63').attr('src');
  291.  
  292. let downloadLink = link;
  293. let type = 'jpg';
  294.  
  295. saveFiles(downloadLink,username,"stories",timestamp,type);
  296. }
  297. }
  298. else{
  299. // Add the stories download button
  300. let style = "position: absolute;right:-40px;top:15px;padding:5px;line-height:1;background:#fff;border-radius: 5px;cursor:pointer;";
  301. if(!$('.IG_DWSTORY').length){
  302. GL_dataCache.stories = {};
  303. $('body > div section._ac0a').append(`<div title="${_i18n("DW")}" class="IG_DWSTORY" style="${style}"><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>`);
  304. return true;
  305. }
  306. }
  307. }
  308.  
  309. // Stories Thumbnail funcion
  310. async function onStoryThumbnailDW(isDownload,isForce){
  311. if(isDownload){
  312. // Download stories if it is video
  313. let date = new Date().getTime();
  314. let timestamp = Math.floor(date / 1000);
  315. let type = 'jpg';
  316. let username = $("body > div section._ac0a header._ac0k ._ac0l div ._ac0q > a").text();
  317. 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;';
  318. // Download thumbnail
  319. let targetURL = location.href.replace(/\/$/ig,'').split("/").at(-1);
  320. let videoThumbnailURL = "";
  321.  
  322.  
  323. if(GL_dataCache.stories[username] && !isForce){
  324. console.log('Fetch from memory cache:', username);
  325. GL_dataCache.stories[username].data.reels_media[0].items.forEach(item => {
  326. if(item.id == targetURL){
  327. videoThumbnailURL = item.display_url;
  328. }
  329. });
  330.  
  331. if(videoThumbnailURL.length == 0){
  332. console.log('Memory cache not found, try fetch from API:', username);
  333. onStoryThumbnailDW(true,true);
  334. return;
  335. }
  336. }
  337. else{
  338. let userInfo = await getUserId(username);
  339. let userId = userInfo.user.pk;
  340. let stories = await getStories(userId);
  341.  
  342. stories.data.reels_media[0].items.forEach(item => {
  343. if(item.id == targetURL){
  344. videoThumbnailURL = item.display_url;
  345. }
  346. });
  347. }
  348.  
  349. saveFiles(videoThumbnailURL,username,"thumbnail",timestamp,type);
  350. }
  351. else{
  352. if($('body > div section._ac0a video.xh8yej3').length){
  353. // Add the stories download button
  354. let style = "position: absolute;right:-40px;top:45px;padding:5px;line-height:1;background:#fff;border-radius: 5px;cursor:pointer;";
  355. if(!$('.IG_DWSTORY_THUMBNAIL').length){
  356. $('body > div section._ac0a').append(`<div title="${_i18n("THUMBNAIL_INTRO")}" class="IG_DWSTORY_THUMBNAIL" style="${style}"><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>`);
  357. }
  358. }
  359. else{
  360. $('.IG_DWSTORY_THUMBNAIL').remove();
  361. }
  362. }
  363. }
  364.  
  365. // Prepare promise to fetch user stories
  366. function getHighlightsStories(highlightId){
  367. return new Promise((resolve,reject)=>{
  368. 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`;
  369.  
  370. GM_xmlhttpRequest({
  371. method: "GET",
  372. url: getURL,
  373. onload: function(response) {
  374. let obj = JSON.parse(response.response);
  375. resolve(obj);
  376. },
  377. onerror: function(err){
  378. reject(err);
  379. }
  380. });
  381. });
  382. }
  383.  
  384. // Prepare promise to fetch user stories
  385. function getStories(userId){
  386. return new Promise((resolve,reject)=>{
  387. 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`;
  388.  
  389. GM_xmlhttpRequest({
  390. method: "GET",
  391. url: getURL,
  392. onload: function(response) {
  393. let obj = JSON.parse(response.response);
  394. resolve(obj);
  395. },
  396. onerror: function(err){
  397. reject(err);
  398. }
  399. });
  400. });
  401. }
  402.  
  403. // Prepare promise to fetch user id by username
  404. function getUserId(username){
  405. return new Promise((resolve,reject)=>{
  406. let getURL = `https://www.instagram.com/web/search/topsearch/?query=${username}`;
  407.  
  408. GM_xmlhttpRequest({
  409. method: "GET",
  410. url: getURL,
  411. onload: function(response) {
  412. let obj = JSON.parse(response.response);
  413. resolve(obj.users[0]);
  414. },
  415. onerror: function(err){
  416. reject(err);
  417. }
  418. });
  419. });
  420. }
  421.  
  422. // Prepare promise to cache article which contains blob media
  423. function getBlobMedia(postPath){
  424. return new Promise((resolve,reject)=>{
  425. if(!postPath) reject("NOPATH");
  426. let postShortCode = postPath.substring(3, postPath.length - 1);
  427. let getURL = `https://www.instagram.com/graphql/query/?query_hash=2c4c2e343a8f64c625ba02b2aa12c7f8&variables=%7B%22shortcode%22:%22${postShortCode}%22}`;
  428.  
  429. GM_xmlhttpRequest({
  430. method: "GET",
  431. url: getURL,
  432. onload: function(response) {
  433. let obj = JSON.parse(response.response);
  434. resolve(obj.data);
  435. },
  436. onerror: function(err){
  437. reject(err);
  438. }
  439. });
  440. });
  441. }
  442.  
  443. // Main function
  444. function onReadyMyDW(NoDialog){
  445. // Whether is Instagram dialog?
  446. if(!NoDialog){
  447. // Running if it is dialog
  448. $('article[role="presentation"]').each(function(){
  449. $(this).removeAttr('data-snig');
  450. $(this).unbind('click');
  451. });
  452. $('.SNKMS_IG_DW_MAIN,.SNKMS_IG_DW_MAIN_VIDEO').remove();
  453. }
  454. if(NoDialog == false){
  455. var repeat = setInterval(() => {
  456. if($('article[data-snig="canDownload"]').length > 0) clearInterval(repeat);
  457. createArtBtn();
  458. },250);
  459. }
  460. else{
  461. createArtBtn();
  462. }
  463. }
  464.  
  465. function createArtBtn(){
  466. // Add download icon per each posts
  467. $('article[role="presentation"]').each(function(){
  468. // If it is have not download icon
  469. if(!$(this).attr('data-snig')){
  470. console.log("Found article");
  471. var style = "position: absolute;right:15px;top:15px;padding:6px;line-height:1;background:#fff;border-radius: 50%;cursor:pointer;";
  472.  
  473. // Add the download icon
  474. let $childElement = $(this).children("div").children("div");
  475. $childElement.eq($childElement.length - 2).append(`<div title="${_i18n("DW")}" class="SNKMS_IG_DW_MAIN" style="${style}"><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>`);
  476.  
  477. // Running if user click the download icon
  478. $(this).on('click','.SNKMS_IG_DW_MAIN', async function(e){
  479. GL_username = $(this).parent().parent().parent().attr('data-username');
  480. GL_postPath = $(this).parent().parent().children("div:last-child").children("div").find("div > section").last().prev().find("a").attr("href");
  481.  
  482. // Create element that download dailog
  483. IG_createDM(GM_getValue('AutoDownload'));
  484.  
  485. $("#article-id").text(GL_postPath);
  486. var style = 'display:block;margin:5px 0px;padding:5px 0px;color:#111;font-size:1rem;line-height:1rem;text-align:center;border:1px solid #000;border-radius: 5px;';
  487.  
  488. // Find video/image element and add the download icon
  489. var s = 0;
  490. var multiple = $(this).parent().parent().find('._aap0 ._acaz').length;
  491. var pathname = window.location.pathname;
  492. var fullpathname = "/"+pathname.split('/')[1]+"/"+pathname.split('/')[2]+"/";
  493.  
  494. // If posts have more than one images or videos.
  495. if(multiple){
  496. var blob = false;
  497. $(this).parent().find('._aap0 ._acaz').each(function(){
  498. let element_videos = $(this).parent().parent().find('video._ab1d');
  499. if(element_videos && element_videos.attr('src') && element_videos.attr('src').match(/^blob:/ig)){
  500. blob = true;
  501. }
  502. });
  503.  
  504.  
  505. if(blob){
  506. createMediaCacheDOM(GL_postPath,".IG_SN_DIG .IG_SN_DIG_MAIN .IG_SN_DIG_BODY",style,_i18n("LOAD_BLOB_MULTIPLE"));
  507. }
  508. else{
  509. let blob = false;
  510. $(this).parent().find('._aap0 ._acaz').each(function(){
  511. s++;
  512. let element_videos = $(this).find('video._ab1d');
  513. let element_images = $(this).find('._aagv img');
  514. let imgLink = (element_images.attr('srcset'))?element_images.attr('srcset').split(" ")[0]:element_images.attr('src');
  515.  
  516. if(element_videos && element_videos.attr('src')){
  517. let video_image = element_videos.attr('poster');
  518. let video_url = element_videos.attr('src');
  519.  
  520. $('.IG_SN_DIG .IG_SN_DIG_MAIN .IG_SN_DIG_BODY').append(`<a data-needed="direct" data-name="IGTV" data-type="mp4" data-globalIndex="${s}" style="${style}" href="javascript:;" data-href="${video_url}"><img width="100" src="${video_image}" /><br/>- ${_i18n("VID")} ${s} -</a>`);
  521.  
  522. if(video_url.match(/^blob:/ig)) blob = true;
  523. }
  524. if(element_images && imgLink){
  525. $('.IG_SN_DIG .IG_SN_DIG_MAIN .IG_SN_DIG_BODY').append(`<a data-needed="direct" data-name="photo" data-type="jpg" data-globalIndex="${s}" style="${style}" href="javascript:;" data-href="${imgLink}"><img width="100" src="${imgLink}" /><br/>- ${_i18n("IMG")} ${s} -</a>`);
  526. }
  527.  
  528. });
  529.  
  530. if(blob){
  531. createMediaCacheDOM(GL_postPath,".IG_SN_DIG .IG_SN_DIG_MAIN .IG_SN_DIG_BODY",style,_i18n("LOAD_BLOB_RELOAD"));
  532. }
  533. }
  534. }
  535. else{
  536. s++;
  537. let element_videos = $(this).parent().parent().find('video._ab1d');
  538. let element_images = $(this).parent().parent().find('._aagv img');
  539. let imgLink = (element_images.attr('srcset'))?element_images.attr('srcset').split(" ")[0]:element_images.attr('src');
  540.  
  541.  
  542. if(element_videos && element_videos.attr('src')){
  543. let video_image = element_videos.attr('poster');
  544. let video_url = element_videos.attr('src');
  545.  
  546. if(element_videos.attr('src').match(/^blob:/ig)){
  547. createMediaCacheDOM(GL_postPath,".IG_SN_DIG .IG_SN_DIG_MAIN .IG_SN_DIG_BODY",style,_i18n("LOAD_BLOB_ONE"));
  548. }
  549. else{
  550. $('.IG_SN_DIG .IG_SN_DIG_MAIN .IG_SN_DIG_BODY').append(`<a data-needed="direct" data-name="video" data-type="mp4" data-globalIndex="${s}" style="${style}" href="javascript:;" data-href="${video_url}"><img width="100" src="${video_image}" /><br/>- ${_i18n("VID")} ${s} -</a>`);
  551. }
  552. }
  553. if(element_images && imgLink){
  554. $('.IG_SN_DIG .IG_SN_DIG_MAIN .IG_SN_DIG_BODY').append(`<a data-needed="direct" data-name="photo" data-type="jpg" data-globalIndex="${s}" style="${style}" href="javascript:;" href="" data-href="${imgLink}"><img width="100" src="${imgLink}" /><br/>- ${_i18n("IMG")} ${s} -</a>`);
  555. }
  556. }
  557.  
  558.  
  559. if(GM_getValue('AutoDownload')){
  560. var autoTimer = setInterval(()=>{
  561. if($('a[data-type]').length > 0){
  562. var GB_Index = 1;
  563.  
  564. if(!$('a[data-blob="true"]').length){
  565. let selectionArr = [...$(this).parent().find('._aamk').children('._acnb')];
  566.  
  567. let LeftButton = $(this).parent().parent().find('button._aahh').length;
  568. let RightButton = $(this).parent().parent().find('button._aahi').length;
  569.  
  570. if(LeftButton && !RightButton){ // Left Only
  571. GB_Index = 2;
  572. console.log('Left Only');
  573. }
  574. else if(!LeftButton && RightButton){ // Right Only
  575. GB_Index = 1;
  576. console.log('Right Only');
  577. }
  578. else if(!LeftButton && !RightButton){ // Both Not Exist
  579. GB_Index = 1;
  580. console.log('Both Not Exist');
  581. }
  582. else{ // Both Exist
  583. GB_Index = 2;
  584. console.log('Both Exist');
  585. }
  586. }
  587. else{
  588. console.log('Blob Media');
  589. let selectionArr = ($(this).parent().find('._aamk').children('._acnb').length)?[...$(this).parent().find('._aamk').children('._acnb')]:[...$(this).parent().find('._aamj').children('._acnb')];
  590. let idx = 0;
  591.  
  592. for(let element of selectionArr){
  593. idx++;
  594. if($(element).attr('class').match(/_acnf/g)){
  595. GB_Index = idx;
  596. }
  597. }
  598. }
  599.  
  600. let downloadLink = $('.IG_SN_DIG').find('a[data-globalindex="'+GB_Index+'"]').attr('data-href');
  601. let date = new Date().getTime();
  602. let timestamp = Math.floor(date / 1000);
  603. let type = $('.IG_SN_DIG').find('a[data-globalindex="'+GB_Index+'"]').attr('data-type');
  604. let name = $('.IG_SN_DIG').find('a[data-globalindex="'+GB_Index+'"]').attr('data-name');
  605.  
  606. saveFiles(downloadLink,GL_username,name,timestamp,type);
  607. $('.IG_SN_DIG').remove();
  608. clearInterval(autoTimer);
  609. }
  610. },500);
  611. }
  612. });
  613.  
  614. // Add the mark that download is ready
  615. var username = $(this).find("header > div:last-child > div:first-child span > a").first().text();
  616.  
  617. $(this).attr('data-snig','canDownload');
  618. $(this).attr('data-username',username);
  619. }
  620. });
  621. }
  622.  
  623. // Create media element from blob media
  624. async function createMediaCacheDOM(postURL,selector,style,message){
  625. $(`${selector} a`).remove();
  626. $(selector).append('<p style="text-align:center;font-size:20px;" id="_SNLOAD">'+ message +'</p>');
  627. let media = await getBlobMedia(postURL);
  628. let idx = 1;
  629. let resource = media.shortcode_media;
  630.  
  631. // GraphVideo
  632. if(resource.__typename == "GraphVideo" && resource.video_url){
  633. $(selector).append(`<a data-blob="true" data-needed="direct" data-name="video" data-type="mp4" data-username="${resource.owner.username}" data-globalIndex="${idx}" style="${style}" href="javascript:;" data-href="${resource.video_url}"><img width="100" src="${resource.display_resources[1].src}" /><br/>- ${_i18n("VID")} ${idx} -</a>`);
  634. idx++;
  635. }
  636. // GraphSidecar
  637. if(resource.__typename == "GraphSidecar" && resource.edge_sidecar_to_children){
  638. for(let e of resource.edge_sidecar_to_children.edges){
  639. if(e.node.__typename == "GraphVideo"){
  640. $(selector).append(`<a data-blob="true" data-needed="direct" data-name="video" data-type="mp4" data-username="${resource.owner.username}" data-globalIndex="${idx}" style="${style}" href="javascript:;" data-href="${e.node.video_url}"><img width="100" src="${e.node.display_resources[1].src}" /><br/>- ${_i18n("VID")} ${idx} -</a>`);
  641. }
  642.  
  643. if(e.node.__typename == "GraphImage"){
  644. $(selector).append(`<a data-blob="true" data-needed="direct" data-name="photo" data-type="jpg" data-username="${resource.owner.username}" data-globalIndex="${idx}" style="${style}" 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>`);
  645. }
  646. idx++;
  647. }
  648. }
  649. $("#_SNLOAD").remove();
  650. }
  651.  
  652. // Create the download dialog element funcion
  653. function IG_createDM(a){
  654. let style = (!a)?"position: fixed;left: 0px;right: 0px;bottom: 0px;top: 0px;":"display:none;";
  655. $('body').append('<div class="IG_SN_DIG" style="'+style+';z-index: 500;"><div class="IG_SN_DIG_BG" style="'+style+'z-index:502;background: rgba(0,0,0,.75);"></div><div class="IG_SN_DIG_MAIN" style="z-index: 510;padding:10px 15px;top:7%;position: absolute;left: 50%;transform: translateX(-50%);width: 500px;background:#fff;border-radius: 7px;"><div class="IG_SN_DIG_TITLE"></div><div style="min-height: 100px;max-height: 80vh;overflow-y:auto;" class="IG_SN_DIG_BODY"></div></div></div>');
  656. $('.IG_SN_DIG .IG_SN_DIG_MAIN .IG_SN_DIG_TITLE').append('<div style="position:relative;height:36px;text-align:center;"><div style="position:absolute;left:0px;line-height: 18px;">Alt+Q ['+_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;" 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>');
  657. }
  658.  
  659. // Download and rename files
  660. function saveFiles(downloadLink,username,index,timestamp,type){
  661. fetch(downloadLink).then(res => {
  662. return res.blob().then(dwel => {
  663. const a = document.createElement("a");
  664. const name = username+'-'+index+'-'+timestamp+'.'+type;
  665. a.href = URL.createObjectURL(dwel);
  666. a.setAttribute("download", name);
  667. a.click();
  668. a.remove();
  669. });
  670. });
  671. }
  672.  
  673. // Supported language list
  674. function translateText(lang){
  675. return {
  676. "zh-TW": {
  677. "CLOSE": "關閉",
  678. "IMG": "相片",
  679. "VID": "影片",
  680. "DDL": "快速下載",
  681. "DDL_INTRO": "勾選後將直接下載點選當下位置的相片/影片",
  682. "DW": "下載",
  683. "THUMBNAIL_INTRO": "下載影片縮圖",
  684. "LOAD_BLOB_ONE": "正在載入二進位大型物件...",
  685. "LOAD_BLOB_MULTIPLE": "正在載入多個二進位大型物件...",
  686. "LOAD_BLOB_RELOAD": "正在重新載入二進位大型物件...",
  687. "NO_VID_URL": "找不到影片網址"
  688. },
  689. "zh-CN": {
  690. "CLOSE": "关闭",
  691. "IMG": "图像",
  692. "VID": "视频",
  693. "DDL": "便捷下载",
  694. "DDL_INTRO": "勾选后将直接下载點擊當下位置的图像/视频",
  695. "DW": "下载",
  696. "THUMBNAIL_INTRO": "下载视频缩略图",
  697. "LOAD_BLOB_ONE": "正在载入大型媒体对象...",
  698. "LOAD_BLOB_MULTIPLE": "正在载入多个大型媒体对象...",
  699. "LOAD_BLOB_RELOAD": "正在重新载入大型媒体对象...",
  700. "NO_VID_URL": "找不到视频网址"
  701. },
  702. "en-US": {
  703. "CLOSE": "Close",
  704. "IMG": "Image",
  705. "VID": "Video",
  706. "DDL": "Quick Download",
  707. "DDL_INTRO": "Checking it will direct download current photo/media in the posts.",
  708. "DW": "Download",
  709. "THUMBNAIL_INTRO": "Download video thumbnail.",
  710. "LOAD_BLOB_ONE": "Loading Blob Media...",
  711. "LOAD_BLOB_MULTIPLE": "Loading Blob Media and others...",
  712. "LOAD_BLOB_RELOAD": "Detect Blob Media, now reloading...",
  713. "NO_VID_URL": "Can not find video url."
  714. }
  715. };
  716. }
  717.  
  718. // Translate display text to user country
  719. function _i18n(text){
  720. let userLang = (lang)?lang:"en-US";
  721. let translate = {
  722. "zh-TW": function(){
  723. return translateText()["zh-TW"];
  724. },
  725. "zh-HK": function(){
  726. return translateText()["zh-TW"];
  727. },
  728. "zh-MO": function(){
  729. return translateText()["zh-TW"];
  730. },
  731. "zh-CN": function(){
  732. return translateText()["zh-CN"];
  733. },
  734. "en-US": function(){
  735. return translateText()["en-US"];
  736. }
  737. }
  738.  
  739. try{
  740. return translate[lang]()[text];
  741. }
  742. catch{
  743. return translate["en-US"]()[text];
  744. }
  745. }
  746.  
  747. // Running if document is ready
  748. $(function(){
  749. // Close the download dialog if user click the close icon
  750. $('body').on('click','.IG_SN_DIG_BTN,.IG_SN_DIG_BG',function(){
  751. $('.IG_SN_DIG').remove();
  752. });
  753.  
  754. // Hot key [Alt+Q] to close the download dialog
  755. $(window).keydown(function(e){
  756. if (e.keyCode == '81' && e.altKey){
  757. $('.IG_SN_DIG').remove();
  758. e.preventDefault();
  759. }
  760. });
  761. $('body').on('click','.AutoDownload',function(){
  762. if($('.AutoDownload:checked').length){
  763. GM_setValue('AutoDownload',true);
  764. }
  765. else{
  766. GM_setValue('AutoDownload',false);
  767. }
  768. });
  769.  
  770. $('body').on('click','a[data-needed="direct"]',function(){
  771. let date = new Date().getTime();
  772. let timestamp = Math.floor(date / 1000);
  773. let username = ($(this).attr('data-username')) ? $(this).attr('data-username') : GL_username;
  774.  
  775. saveFiles($(this).attr('data-href'),username,$(this).attr('data-name'),timestamp,$(this).attr('data-type'));
  776. });
  777.  
  778. // Running if user left-click download icon in stories
  779. $('body').on('click','.IG_DWSTORY',function(){
  780. onStoryDW(true);
  781. });
  782.  
  783. // Running if user left-click download icon in stories
  784. $('body').on('click','.IG_DWSTORY_THUMBNAIL',function(){
  785. onStoryThumbnailDW(true);
  786. });
  787.  
  788. // Running if user left-click download icon in profile
  789. $('body').on('click','.IG_DWPROFILE',function(e){
  790. e.stopPropagation();
  791. onProfileDW(true);
  792. });
  793.  
  794. // Running if user left-click download icon in highlight stories
  795. $('body').on('click','.IG_DWHISTORY',function(){
  796. onHighlightsStoryDW(true);
  797. });
  798.  
  799. // Running if user left-click download icon in highlight stories
  800. $('body').on('click','.IG_DWHISTORY_THUMBNAIL',function(){
  801. onHighlightsStoryThumbnailDW(true);
  802. });
  803. });
  804.  
  805. })(jQuery);

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址