Embed Image Helper

Makes tags for embed image with link to original (full size) image.

  1. //
  2. // Embed Image Helper
  3. // Copyright © 2013, 2014 Anton Chugunov
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. //
  18. // ==UserScript==
  19. // @name Embed Image Helper
  20. // @namespace trm81
  21. // @description Makes tags for embed image with link to original (full size) image.
  22. // @version 0.06
  23. // @include http://photo.qip.ru/users/*/*/*/*
  24. // @include https://photo.qip.ru/users/*/*/*/*
  25. // @include http://fotki.yandex.ru/users/*/album/*/share/*
  26. // @include https://fotki.yandex.ru/users/*/album/*/share/*
  27. // @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
  28. // @grant None
  29. // @copyright 2013, 2014 Anton Chugunov (https://greasyfork.org/en/users/6997-trm81)
  30. // @license GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
  31. // ==/UserScript==
  32.  
  33. var photo_qip_ru = 'photo.qip.ru';
  34. var fotki_yandex_ru = 'fotki.yandex.ru';
  35.  
  36. /////////////////////////////////////////////////////////////////////////////////////
  37. // photo.qip.ru
  38.  
  39. function replaceUrls(user, album, photo)
  40. {
  41. var pattern = 'users\\/' + user + '\\/' + album + '\\/' + photo + '\\/';
  42. var re = new RegExp(pattern, "igm");
  43.  
  44. $('#ff_allGetLinks').find('textarea').each(function() {
  45. var text = $(this).text();
  46. text = text.replace(re, 'photo/' + user + '/' + album + '/' + photo + '.jpg');
  47. $(this).text(text);
  48. });
  49. }
  50.  
  51. handler_onPhotopQipRu = function onPhotopQipRu()
  52. {
  53. var user = '', album = '', photo = '';
  54. var photoParamsRegex = /\/users\/([^\/]+)\/([^\/]+)\/([^\/]+)/i;
  55. var match = photoParamsRegex.exec(window.location.pathname);
  56. if (match != null)
  57. {
  58. user = match[1];
  59. album = match[2];
  60. photo = match[3];
  61. }
  62. unsafeWindow.getLink(user, album, photo);
  63. unsafeWindow.showDiv('ff_get_link');
  64. $('div.linksbox a.slink[href*="getLink"]').removeAttr('href').click(function(){
  65. unsafeWindow.showDiv('ff_get_link');
  66. replaceUrls(user, album, photo);
  67. });
  68. };
  69.  
  70.  
  71. /////////////////////////////////////////////////////////////////////////////////////
  72. // fotki.yandex.ru
  73.  
  74. function replaceText(viewUrlPattern, previewUrlPattern, removeLinkPattern, embUrls)
  75. {
  76. replacedText = [];
  77. for (var i = 0; i < embUrls.length; i++)
  78. {
  79. var string = embUrls[i].trim();
  80. if (!string.length)
  81. continue;
  82. if (string.indexOf('[url') != 0 && string.indexOf('<a href') != 0)
  83. continue;
  84. var baseUrl = null, size = null, ext = null;
  85. while(match = previewUrlPattern.exec(string))
  86. {
  87. baseUrl = match[1];
  88. size = match[2];
  89. ext = match[3];
  90. }
  91. if (size == 'orig')
  92. string = string.replace(removeLinkPattern, '');
  93. else
  94. string = string.replace(viewUrlPattern, baseUrl + '_orig' + ext);
  95. replacedText.push(string);
  96. }
  97. return replacedText.length ? replacedText : embUrls;
  98. }
  99.  
  100. function replaceHTMLCodeText(viewUrl, textarea)
  101. {
  102. var previewUrlPattern = new RegExp('src="(http.+)_(\\w+)(\\.[\\w\\d]+)', 'g');
  103. var viewUrlPattern = new RegExp(viewUrl + '[^\\"]+', 'ig');
  104. var removeLinkPattern = new RegExp('(<a[^>]+>|</a>)', 'ig');
  105. var embUrls = textarea.val().replace('<!--more-->', '<br/>').split('<br/>');
  106. var text = replaceText(viewUrlPattern, previewUrlPattern, removeLinkPattern, embUrls);
  107. textarea.val(text.join('<br/>'));
  108. textarea.select();
  109. }
  110.  
  111. function replaceBBCodeText(viewUrl, textarea)
  112. {
  113. var previewUrlPattern = new RegExp('img\\](http.+)_(\\w+)(\\.[\\w\\d]+)', 'g');
  114. var viewUrlPattern = new RegExp(viewUrl + '[^\\]]+', 'ig');
  115. var removeLinkPattern = new RegExp('(\\[url[^\\]]+\\]|\\[/url\\])', 'ig');
  116. var embUrls = textarea.val().split('\n');
  117. var text = replaceText(viewUrlPattern, previewUrlPattern, removeLinkPattern, embUrls);
  118. textarea.val(text.join('\n'));
  119. textarea.select();
  120. }
  121.  
  122. handler_onFotkiYandexRu = function onFotkiYandexRu()
  123. {
  124. var shareParamsElem = $('div.share.js-share');
  125. var shareParams = shareParamsElem.attr('onclick');
  126. shareParams = shareParams.replace(/^return/, '').replace(/;$/, '');
  127. var json = jQuery.parseJSON(shareParams);
  128.  
  129. var viewUrl = json['url'];
  130. var idx = viewUrl.indexOf('view');
  131. if (idx == -1)
  132. return;
  133. viewUrl = viewUrl.substring(0, idx);
  134. $('#codes_bbcode').click(function(){
  135. replaceBBCodeText(viewUrl, $(this));
  136. });
  137. $('#codes_html').click(function(){
  138. replaceHTMLCodeText(viewUrl, $(this));
  139. });
  140. };
  141.  
  142.  
  143. /////////////////////////////////////////////////////////////////////////////////////
  144.  
  145. $(document).ready(function()
  146. {
  147. var urlHandlers = {};
  148. urlHandlers[photo_qip_ru] = handler_onPhotopQipRu;
  149. urlHandlers[fotki_yandex_ru] = handler_onFotkiYandexRu;
  150.  
  151. var domain = window.location.hostname.replace('www.', '');
  152. handler = urlHandlers[domain];
  153. if(typeof handler === 'function')
  154. handler();
  155. });