ImageFAQs

Converts plain-text image and webm URLs into their embedded form

目前为 2015-04-25 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name ImageFAQs
  3. // @namespace FightingGames@gfaqs
  4. // @include http://www.gamefaqs.com
  5. // @include http://www.gamefaqs.com*
  6. // @description Converts plain-text image and webm URLs into their embedded form
  7. // @version 1.03
  8. // @grant GM_addStyle
  9. // @grant GM_getResourceText
  10. // @grant GM_log
  11. // @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js
  12. // @require https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/jquery-ui.min.js
  13. // @resource jqueryuibaseCSS https://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery.ui.base.css
  14. // @resource jqueryuithemeCSS https://code.jquery.com/ui/1.11.4/themes/cupertino/jquery-ui.css
  15. // ==/UserScript==
  16.  
  17. /*
  18. The MIT License (MIT)
  19.  
  20. This greasemonkey script for GameFAQs converts image and webm
  21. links into their embedded form. It can be considered as a spiritual successor
  22. to text-to-image for GameFAQs. Many of its features are inspired from appchan x
  23. by zixaphir at http://zixaphir.github.io/appchan-x/.
  24. Copyright (c) 2015 FightingGames@gamefaqs <adrenalinebionicarm@gmail.com>
  25.  
  26. Permission is hereby granted, free of charge, to any person obtaining a copy
  27. of this software and associated documentation files (the "Software"), to deal
  28. in the Software without restriction, including without limitation the rights
  29. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  30. copies of the Software, and to permit persons to whom the Software is
  31. furnished to do so, subject to the following conditions:
  32.  
  33. The above copyright notice and this permission notice shall be included in
  34. all copies or substantial portions of the Software.
  35.  
  36. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  37. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  38. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  39. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  40. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  41. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  42. THE SOFTWARE.
  43.  
  44. */
  45.  
  46.  
  47.  
  48. GM_addStyle (GM_getResourceText("jqueryuibaseCSS"));
  49. GM_addStyle (GM_getResourceText("jqueryuithemeCSS"));
  50. this.$ = this.jQuery = jQuery.noConflict(true);
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58. /*this top portion of the script handles the settings box and local storage*/
  59.  
  60. var thumbnailImgWidth = 150;
  61. var thumbnailImgHeight = 150;
  62. var settingsJSON = localStorage.getItem("imagefaqs");
  63. var settings; /*key-value array of JSON*/
  64. var sessionSpoilersHotkey;
  65.  
  66. /*if imageFAQs was freshly installed*/
  67. if (settingsJSON == null)
  68. {
  69. settings = {
  70. enable_imagefaqs: true,
  71. enable_sigs: false,
  72. thumbnailWidth: thumbnailImgWidth,
  73. thumbnailHeight: thumbnailImgHeight,
  74. imgContainerBackgroundColor: "#DFE6F7",
  75. isSpoilersMode: false,
  76. spoilersHotkey: {115: 115}, /*F4*/
  77. blacklistStr: ""
  78. };
  79. localStorage.setItem("imagefaqs", JSON.stringify(settings)); /*save default settings*/
  80. }
  81. else
  82. {
  83. settings = jQuery.parseJSON(settingsJSON);
  84. }
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91. function keyCodeToString(keyCode)
  92. {
  93. keyCode = parseInt(keyCode);
  94. if (keyCode == 16)
  95. {
  96. return "shift";
  97. }
  98. else if (keyCode == 17)
  99. {
  100. return "ctrl";
  101. }
  102. else if (keyCode == 18)
  103. {
  104. return "alt";
  105. }
  106. else if (keyCode >= 112 && keyCode <= 123) /*F keys*/
  107. {
  108. return "F" + (keyCode - 111);
  109. }
  110. else
  111. {
  112. return String.fromCharCode(keyCode);
  113. }
  114. }
  115.  
  116.  
  117. function keyCodeKVArrayToString(array)
  118. {
  119. var string = "";
  120. for (var key in array)
  121. {
  122. string += keyCodeToString(key) + " ";
  123. }
  124. return string;
  125. }
  126.  
  127.  
  128. function cloneKVArray(KVArray)
  129. {
  130. var newKVArray = {};
  131. for (var key in KVArray)
  132. {
  133. newKVArray[key] = KVArray[key];
  134. }
  135. return newKVArray;
  136. }
  137.  
  138.  
  139.  
  140. sessionSpoilersHotkey = cloneKVArray(settings.spoilersHotkey);
  141.  
  142.  
  143.  
  144.  
  145. /*event where user opens up settings menu*/
  146. $("body").on("click", "#imagefaqs_settings_but", function(event) {
  147.  
  148. var popupBox = $("#imagefaqs_settings_popup");
  149. event.preventDefault();
  150. if (popupBox.length == 0)
  151. {
  152. $("body").append(
  153. "<div id='imagefaqs_settings_popup' title='ImageFAQs Settings. Version "+ GM_info.script.version+"'>" +
  154. "Update history: <a target='_blank' href='http://fightinggames.bitbucket.org/imagefaqs/'>http://fightinggames.bitbucket.org/imagefaqs/</a>." +
  155. "<br/>" +
  156. "Feedback and bug reporting: <a target='_blank' href='http://www.gamefaqs.com/boards/565885-blood-money'>http://www.gamefaqs.com/boards/565885-blood-money</a>." +
  157. "<br/><br/>" +
  158. "<label><input id='enableImagefaqs_checkbox' type='checkbox'>Enable ImageFAQs</label>" +
  159. "<br/>" +
  160. "<label><input id='enable_sigs_checkbox' type='checkbox'>Enable embedding in signatures</label>" +
  161. "<br/><br/>" +
  162. "<label><input id='thumbnailImgWidth_text' type='text'>Thumbnail max-width (150 default)</label>" +
  163. "<br/>" +
  164. "<label><input id='thumbnailImgHeight_text' type='text'>Thumbnail max-height (150 default)</label>" +
  165. "<br/><br/>" +
  166. "<label><input id='imgContainerBackgroundColor_text' type='text'>Image container background color (#DFE6F7 default)</label>" +
  167. "<br/><br/>" +
  168. "<label>Newline separated list of blacklisted image URLs</label>" +
  169. "<br/>" +
  170. "<textarea id='blacklist_text'></textarea>" +
  171. "<br/><br/>" +
  172. "<span>Tip: You can blacklist an entire domain (e.g. pbsrc.com)</span>" +
  173. "<br/><br/>" +
  174. "<label><input id='spoilersHotkey_text' type='text'>Toggle spoilers hotkey (F4 default)</label>" +
  175. "<br/><br/>" +
  176. "<span>Tip: At any time, you can toggle spoilers mode. This can be done before or after<br/> entering a topic.</span>" +
  177. "<br/><br/>" +
  178. "<button id='saveImagefaqsSettingsBut' type='button'>Save</button> " +
  179. "<button id='cancelImagefaqsSettingsBut' type='button'>Cancel</button> " +
  180. "<br/><br/>" +
  181. "<span id='responseSpan'>&nbsp;</span>" + /*for reporting success or failure*/
  182. "</div>"
  183. );
  184. popupBox = $("#imagefaqs_settings_popup");
  185. }
  186.  
  187. popupBox.find("#enableImagefaqs_checkbox").prop("checked", settings.enable_imagefaqs);
  188. popupBox.find("#enable_sigs_checkbox").prop("checked", settings.enable_sigs);
  189. popupBox.find("#thumbnailImgWidth_text").val(settings.thumbnailWidth);
  190. popupBox.find("#thumbnailImgHeight_text").val(settings.thumbnailHeight);
  191. popupBox.find("#imgContainerBackgroundColor_text").val(settings.imgContainerBackgroundColor);
  192. popupBox.find("#spoilersHotkey_text").val( keyCodeKVArrayToString(settings.spoilersHotkey) );
  193. popupBox.find("#blacklist_text").val( settings.blacklistStr );
  194. $("#imagefaqs_settings_popup").children("#responseSpan").html("&nbsp;");
  195.  
  196. $("#imagefaqs_settings_popup").dialog({
  197. width:'auto'
  198. });
  199. });
  200.  
  201.  
  202.  
  203.  
  204.  
  205. $("body").on("click", "#saveImagefaqsSettingsBut", function(event) {
  206.  
  207. event.preventDefault();
  208. var settingsPopup = $("#imagefaqs_settings_popup");
  209. /*if valid thumbnail dimensions*/
  210. if (
  211. !isNaN($("#thumbnailImgWidth_text").val()) && $("#thumbnailImgWidth_text").val() >= 1 &&
  212. !isNaN($("#thumbnailImgHeight_text").val()) && $("#thumbnailImgHeight_text").val() >= 1)
  213. {
  214. settings.thumbnailWidth = $("#thumbnailImgWidth_text").val();
  215. settings.thumbnailHeight = $("#thumbnailImgHeight_text").val();
  216. }
  217. else
  218. {
  219. reportResponseMsgInImagefaqsSettingsPopupBox("Error: Invalid thumbnail dimensions. Use natural numbers only.", settingsPopup);
  220. return;
  221. }
  222. /*if valid image container background color*/
  223. if (settingsPopup.find("#imgContainerBackgroundColor_text").val().search(/^#[a-zA-Z0-9]{6}$/i) >= 0)
  224. {
  225. settings.imgContainerBackgroundColor = settingsPopup.find("#imgContainerBackgroundColor_text").val();
  226. }
  227. else
  228. {
  229. reportResponseMsgInImagefaqsSettingsPopupBox("Error: Invalid image container background color.", settingsPopup);
  230. return;
  231. }
  232. settings.enable_imagefaqs = settingsPopup.find("#enableImagefaqs_checkbox").prop("checked");
  233. settings.enable_sigs = settingsPopup.find("#enable_sigs_checkbox").prop("checked");
  234. settings.spoilersHotkey = cloneKVArray(sessionSpoilersHotkey);
  235. settings.blacklistStr = settingsPopup.find("#blacklist_text").val();
  236. localStorage.setItem("imagefaqs", JSON.stringify(settings));
  237. reportResponseMsgInImagefaqsSettingsPopupBox("Settings saved. Please refresh page for changes to take effect.", settingsPopup);
  238. });
  239.  
  240.  
  241.  
  242. /*
  243. @param msg :: message string to show to the user
  244. @param box :: $("#imagefaqs_settings_popup")
  245. */
  246. function reportResponseMsgInImagefaqsSettingsPopupBox(msg, box)
  247. {
  248. var msgBox = box.children("#responseSpan");
  249. msgBox.html(msg);
  250. msgBox.effect("highlight");
  251. }
  252.  
  253.  
  254.  
  255. $("body").on("click", "#cancelImagefaqsSettingsBut", function(event) {
  256.  
  257. event.preventDefault();
  258. $("#imagefaqs_settings_popup").dialog("close");
  259. });
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266. /*begin main scripting*/
  267. (function(){
  268.  
  269. var enable_imagefaqs = settings.enable_imagefaqs;
  270. var isSpoilersMode = settings.isSpoilersMode;
  271. var thumbnailImgWidth = settings.thumbnailWidth;
  272. var thumbnailImgHeight = settings.thumbnailHeight;
  273. var blacklist = settings.blacklistStr.split("\n"); /*array of URLs to block*/
  274. var currentURL;
  275. var pageType; /* "topic" or "postPreview" or "other" */
  276. var allowImgInSig = settings.enable_sigs;
  277. var isFloatingImgExist = false;
  278. var floatingImgWidth;
  279. var floatingImgRightOffset = 50;
  280. var floatingImgBorder = 10;
  281. var currentHoveredThumbnail = null;
  282. var imgAnchorTags = [];
  283.  
  284.  
  285.  
  286.  
  287.  
  288. var heldDownKeys = {};
  289. $("body").on("keydown", "#imagefaqs_settings_popup #spoilersHotkey_text", function(event){
  290. heldDownKeys[event.which] = event.which;
  291. sessionSpoilersHotkey = cloneKVArray(heldDownKeys);
  292. $(this).val(
  293. keyCodeKVArrayToString(sessionSpoilersHotkey)
  294. );
  295. return false;
  296. });
  297.  
  298.  
  299. $("body").on("keyup", "#imagefaqs_settings_popup #spoilersHotkey_text", function(event){
  300. delete heldDownKeys[event.which];
  301. return false;
  302. });
  303.  
  304. $("body").on("keypress", "#imagefaqs_settings_popup #spoilersHotkey_text", function(event){
  305. event.preventDefault();
  306. });
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314. $("body").keydown(function(event){
  315.  
  316. var desiredAction;
  317. heldDownKeys[event.which] = event.which;
  318.  
  319. /*are every spoilers hotkey pressed?*/
  320. for (var key in settings.spoilersHotkey)
  321. {
  322. if (settings.spoilersHotkey[key] != heldDownKeys[key])
  323. {
  324. return;
  325. }
  326. }
  327. desiredAction = "toggleSpoilersMode";
  328. if (desiredAction == "toggleSpoilersMode")
  329. {
  330. event.preventDefault();
  331. isSpoilersMode = !isSpoilersMode;
  332.  
  333. if (isSpoilersMode)
  334. {
  335. if (pageType != "other")
  336. {
  337. /*for every embedded image*/
  338. $(".embeddedImg").each(function(index, element) {
  339. /*if image is in thumbnail mode, add spoilers class*/
  340. if ($(this).hasClass("thumbnailImg"))
  341. {
  342. $(this).addClass("thumbnailImg_spoilersExt");
  343. $(this).parent().addClass("embeddedImgAnchor_spoilersExt");
  344. }
  345. });
  346. }
  347. showNotification("ImageFAQs: Spoilers mode is now on.");
  348. }
  349. else
  350. {
  351. if (pageType != "other")
  352. {
  353. /*for every embedded image*/
  354. $(".embeddedImg").each(function(index, element) {
  355. /*if image is in thumbnail mode, remove spoilers class*/
  356. if ($(this).hasClass("thumbnailImg"))
  357. {
  358. $(this).removeClass("thumbnailImg_spoilersExt");
  359. $(this).parent().removeClass("embeddedImgAnchor_spoilersExt");
  360. }
  361. });
  362. }
  363. showNotification("ImageFAQs: Spoilers mode is now off.");
  364. }
  365. settings.isSpoilersMode = isSpoilersMode;
  366. localStorage.setItem("imagefaqs", JSON.stringify(settings));
  367. }
  368. });
  369.  
  370.  
  371.  
  372.  
  373. $("body").keyup(function(event){
  374. delete heldDownKeys[event.which];
  375. });
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383. /*
  384. Display a notification on the bottom-right of the browser. Notification will
  385. disappear in a few seconds.
  386.  
  387. @param msg :: string message to display
  388. */
  389. function showNotification(msg)
  390. {
  391. var notificationBox = $("#imagefaqs_notificationBox");
  392. notificationBox.remove();
  393. $("body").append(
  394. "<div id='imagefaqs_notificationBox'>" +
  395. "<span style='color: black'>" +
  396. msg +
  397. "</span>" +
  398. "</div>"
  399. );
  400. notificationBox = $("#imagefaqs_notificationBox");
  401.  
  402. notificationBox.css({
  403. "background-color": "white",
  404. "position": "absolute",
  405. "top": ($(window).scrollTop() + $(window).height() - notificationBox.height())+"px",
  406. "left": ($(window).scrollLeft() + $(window).width() - notificationBox.find("span").width())+"px"
  407. });
  408.  
  409. notificationBox.effect("highlight").delay(1000).fadeOut(500);
  410. }
  411.  
  412.  
  413.  
  414.  
  415. if (enable_imagefaqs == false)
  416. {
  417. return;
  418. }
  419.  
  420.  
  421.  
  422. if (blacklist.length == 1 && blacklist[0] == "")
  423. {
  424. blacklist = [];
  425. }
  426. else
  427. {
  428. /*trim white space*/
  429. for (var i = 0; i < blacklist.length; i++)
  430. {
  431. blacklist[i].trim();
  432. }
  433. }
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441. function isURLmatchBlacklistedURL(url, blacklistedURL)
  442. {
  443. blacklistedURL = blacklistedURL.trim();
  444. /*escape everything*/
  445. var blacklistedURLregex =
  446. new RegExp( blacklistedURL.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&") );
  447.  
  448. if (blacklistedURLregex.exec(url) != null)
  449. {
  450. return true;
  451. }
  452. else
  453. {
  454. return false;
  455. }
  456. }
  457.  
  458.  
  459. /*
  460. Is the url string in the blacklist?
  461.  
  462. @url :: string of the url to check
  463. @blacklist :: array of blacklisted urls
  464.  
  465. Returns true or false
  466. */
  467. function isURLinBlacklist(url, blacklist)
  468. {
  469. for (var i = 0; i < blacklist.length; i++)
  470. {
  471. if (isURLmatchBlacklistedURL(url, blacklist[i]))
  472. {
  473. return true;
  474. }
  475. }
  476. return false;
  477. }
  478.  
  479.  
  480.  
  481.  
  482.  
  483.  
  484.  
  485.  
  486. currentURL = window.location.href;
  487. pageType = currentURL.search(/.*gamefaqs\.com\/boards\/.*\/.*/i);
  488. if (currentURL.search(/.*gamefaqs\.com\/boards\/.*\/.*/i) >= 0)
  489. {
  490. pageType = "topic";
  491. }
  492. else if (currentURL.search(/.*gamefaqs\.com\/boards\/post/i) >= 0)
  493. {
  494. pageType = "postPreview";
  495. }
  496. else if (currentURL.search(/.*gamefaqs\.com\/user/i) >= 0)
  497. {
  498. /*add settings link*/
  499. $(".systems").append("<li><a id='imagefaqs_settings_but' href='#'>ImageFAQs Options</a> - Change thumbnail size, enable signature embedding, and more.</li>");
  500. }
  501. else
  502. {
  503. pageType = "other";
  504. }
  505.  
  506.  
  507.  
  508.  
  509. if (pageType == "other")
  510. {
  511. return;
  512. }
  513.  
  514.  
  515.  
  516.  
  517.  
  518.  
  519.  
  520. GM_addStyle ("\
  521. .embeddedImgContainer {\
  522. background: "+settings.imgContainerBackgroundColor+";\
  523. }\
  524. .embeddedImgAnchor {\
  525. display: inline-block;\
  526. }\
  527. .embeddedImgAnchor_placeholderHeight {\
  528. height: "+thumbnailImgHeight+"px;\
  529. }\
  530. .embeddedImgAnchor_spoilersExt {\
  531. background: black;\
  532. }\
  533. .thumbnailImg {\
  534. display: inline-block; /*prevents parent anchor from stretching horizontally*/\
  535. vertical-align: bottom;\ /*eliminates small gap between bottom of image and parent div*/\
  536. }\
  537. .thumbnailImg_spoilersExt {\
  538. opacity: 0.0;\
  539. }\
  540. .expandedImg {\
  541. display: inline-block;\
  542. vertical-align: bottom;\
  543. }\
  544. ");
  545.  
  546. $("body").css("position", "relative");
  547.  
  548. if (pageType == "topic")
  549. {
  550. /*get all user posts*/
  551. var posts = $(".msg_body");
  552. }
  553. else if (pageType == "postPreview")
  554. {
  555. var posts = $("div.body td:eq(1)");
  556. }
  557.  
  558. /*for each user post*/
  559. posts.each(function(index, element) {
  560. var postHtml;
  561. var unanchoredImgURLMatches;
  562. var sigStrIdx;
  563. var anchorTags;
  564. postHtml = $(this).html();
  565. /*if in post preview mode, need to manually parse through the preview message body and
  566. replace every plain-text image URL in its anchor form*/
  567. if (pageType == "postPreview")
  568. {
  569. postHtml = postHtml.replace(/https?:\/\/[a-zA-Z0-9\[\]\-\.\_\~\:\/\?\#\[\]\%\@\!\$\&\'\(\)\*\+\,\;\=]*?\.(?:png|jpg|gif|jpeg|webm|gifv)[a-zA-Z0-9\[\]\-\.\_\~\:\/\?\#\[\]\%\@\!\$\&\'\(\)\*\+\,\;\=]*/ig, "<a href='$&'>$&</a>");
  570. $(this).html(postHtml);
  571. }
  572. /*get index of sig separator*/
  573. sigStrIdx = postHtml.indexOf("<br>---<br>");
  574. /*get all anchor tags*/
  575. anchorTags = $(this).find("a");
  576. /*filter in anchor tags that refer to images*/
  577. anchorTags.each(function(index, element) {
  578. var anchorURL = $(this).attr("href");
  579. var anchorTagStrIdx = postHtml.indexOf(anchorURL);
  580.  
  581. if (anchorURL.search(/https?:\/\/[a-zA-Z0-9\[\]\-\.\_\~\:\/\?\#\[\]\%\@\!\$\&\'\(\)\*\+\,\;\=]*?\.(?:png|jpg|gif|jpeg|webm|gifv)/i) >= 0)
  582. {
  583. /*if sig exists, anchor is part of sig, and sig shouldn't embedded images*/
  584. if (sigStrIdx != -1 && anchorTagStrIdx > sigStrIdx && !allowImgInSig)
  585. {
  586. return;
  587. }
  588. imgAnchorTags.push($(this));
  589. }
  590. });
  591. });
  592.  
  593.  
  594.  
  595. /*for each valid image URL anchor tag, replace with template div for embedded image*/
  596. for (var imgAnchor of imgAnchorTags)
  597. {
  598. var imgURL = imgAnchor.html();
  599. var imgSize;
  600. var imgInfoEleStr;
  601. var postID;
  602. var isBlacklisted = isURLinBlacklist(imgURL, blacklist);
  603. if (pageType == "topic")
  604. {
  605. postID = imgAnchor.parent().attr("name");
  606. }
  607. else /*if in post preview mode, need to provide missing message ID*/
  608. {
  609. imgAnchor.parent().attr("name", 0);
  610. postID = 0;
  611. }
  612. imgInfoEleStr =
  613. "<span style='margin-right: 5px'>" +
  614. "<a href="+imgURL+">"+imgURL+"</a>" +
  615. "</span>";
  616. imgGoogleReverse =
  617. "<a target='_blank' title='Reverse image search on general images' style='margin-right: 5px' href='https://www.google.com/searchbyimage?image_url="+imgURL+"'>" +
  618. "google" +
  619. "</a>";
  620. imgIQDBreverse =
  621. "<a target='_blank' title='Reverse image search on weeb images' style='margin-right: 5px' href='http://iqdb.org/?url="+imgURL+"'>" +
  622. "iqdb" +
  623. "</a>";
  624. if (isBlacklisted)
  625. {
  626. imgBlacklistToggle =
  627. "<a class='imgBlacklistToggle' href='#' title='Whitelist image' style='margin-right: 5px'>whitelist</a>";
  628. }
  629. else
  630. {
  631. imgBlacklistToggle =
  632. "<a class='imgBlacklistToggle' href='#' title='Blacklist image' style='margin-right: 5px'>blacklist</a>";
  633. }
  634. imgCloseAll =
  635. "<a class='imgCloseAll' href='#' title='Close all images in page' style='margin-right: 5px'>&lt;&lt;</a>";
  636. imgClosePost =
  637. "<a class='imgClosePost' href='#' data-postID='"+postID+"' title='Close all images in post' style='margin-right: 5px'>&lt;</a>";
  638. imgExpandPost =
  639. "<a class='imgExpandPost' href='#' data-postID='"+postID+"' title='Expand all images in post' style='margin-right: 5px'>&gt;</a>";
  640. imgExpandAll =
  641. "<a class='imgExpandAll' href='#' title='Expand all images in page' style='margin-right: 5px'>&gt;&gt;</a>";
  642. imgAnchor.replaceWith(
  643. "<div class='embeddedImgContainer'>" +
  644. imgInfoEleStr +
  645. imgGoogleReverse +
  646. imgIQDBreverse +
  647. imgBlacklistToggle +
  648. imgCloseAll +
  649. imgClosePost +
  650. imgExpandPost +
  651. imgExpandAll +
  652. "<br/>" +
  653. "<a class='embeddedImgAnchor embeddedImgAnchor_placeholderHeight' href='" + imgURL + "' data-backup-href='" + imgURL + "'>" +
  654. "</a>" +
  655. "</div>"
  656. );
  657. if (isBlacklisted)
  658. {
  659. var embeddedImgAnchor = $(".embeddedImgAnchor[href='"+imgURL+"']");
  660. embeddedImgAnchor.hide();
  661. embeddedImgAnchor.addClass("blacklisted");
  662. }
  663. }
  664.  
  665.  
  666. /*for each template div, insert an embedded image*/
  667. $(".embeddedImgContainer").each(function(index, element) {
  668. var curEmbeddedImgContainer = $(this);
  669. var curEmbeddedImgAnchor = $(this).find(".embeddedImgAnchor");
  670. var imgURL = curEmbeddedImgAnchor.attr("href");
  671. var imgType; /*e.g. "jpg"*/
  672. var image = new Image();
  673. var embeddedImgSpoilersClass;
  674. if (isSpoilersMode)
  675. {
  676. embeddedImgSpoilersClass = "thumbnailImg_spoilersExt";
  677. curEmbeddedImgAnchor.addClass("embeddedImgAnchor_spoilersExt");
  678. }
  679. else
  680. {
  681. embeddedImgSpoilersClass = "";
  682. }
  683. var postID = curEmbeddedImgContainer.parent().attr("name");
  684. imageType = imgURL.split(".").pop();
  685. /*if not a webm video (i.e. image)*/
  686. if (imageType.search(/(webm|gifv)/i) == -1)
  687. {
  688. /*on event that image cannot be loaded, post the error*/
  689. image.onerror = function() {
  690. curEmbeddedImgAnchor.html("Image cannot be loaded.");
  691. };
  692. /*on event that image loaded successfully in memory*/
  693. $(image).load(function() {
  694. /*write down image's natural dimensions*/
  695. curEmbeddedImgContainer.find("span").append(
  696. " (" + image.naturalWidth + "x" + image.naturalHeight + ")"
  697. );
  698. curEmbeddedImgAnchor.removeClass("embeddedImgAnchor_placeholderHeight");
  699. });
  700. image.src = imgURL;
  701. $(image).addClass("embeddedImg thumbnailImg "+embeddedImgSpoilersClass+"");
  702. $(image).attr("data-postID", postID);
  703. $(image).attr("alt", "");
  704. $(image).css("max-width", thumbnailImgWidth + "px");
  705. $(image).css("max-height", thumbnailImgHeight + "px");
  706. curEmbeddedImgAnchor.html(image);
  707. }
  708. else /*if image is actually a webm file*/
  709. {
  710. if (imageType == "gifv")
  711. {
  712. imgURL = imgURL.replace("gifv", "webm");
  713. }
  714. htmlVideoSrcTag =
  715. "<video id='embeddedImg_"+index+"' data-postID='"+postID+"' " +
  716. "class='embeddedImg webmExt thumbnailImg "+embeddedImgSpoilersClass+"' " +
  717. "style='max-width: "+thumbnailImgWidth+"px; max-height: "+thumbnailImgHeight+"px;'>" +
  718. "<source src='" + imgURL + "' type='video/webm'>" +
  719. "</video>";
  720. curEmbeddedImgAnchor.html(
  721. htmlVideoSrcTag
  722. );
  723. curEmbeddedImgAnchor.removeClass("embeddedImgAnchor_placeholderHeight");
  724. /*when video's metadata has been loaded, record its natural width and resolution*/
  725. $("#embeddedImg_"+index+"")[0].onloadedmetadata = function() {
  726. var video = this;
  727. $(video).parent().parent().find("span").append(
  728. " (" + video.videoWidth + "x" + video.videoHeight + ")"
  729. );
  730. };
  731. }
  732. });
  733.  
  734.  
  735. /*
  736. Get the natural width of an embedded image.
  737.  
  738. @image :: jQuery object with the class "embeddedImg"
  739. */
  740. function getNatWidthOfEmbeddedImg(image)
  741. {
  742. if (image.hasClass("webmExt"))
  743. {
  744. return image[0].videoWidth;
  745. }
  746. else
  747. {
  748. return image[0].naturalWidth;
  749. }
  750. }
  751.  
  752.  
  753.  
  754. /*
  755. Get the natural width of an embedded image.
  756.  
  757. @image :: jQuery object with the class "embeddedImg"
  758. */
  759. function getNatHeightOfEmbeddedImg(image)
  760. {
  761. if (image.hasClass("webmExt"))
  762. {
  763. return image[0].videoHeight;
  764. }
  765. else
  766. {
  767. return image[0].naturalHeight;
  768. }
  769. }
  770.  
  771.  
  772.  
  773. /*if cursor is hovering inside a thumbnail image, display expanded image as floating div*/
  774. $(document).on("mousemove", function(event) {
  775. var floatingImgLeft;
  776. var floatingImgTop;
  777. var floatingImgWidth;
  778. var floatingImgHeight;
  779. var floatingImgStyleStr;
  780. /*if user is hovering over thumbnail, prepare floating image size and position*/
  781. if (currentHoveredThumbnail != null)
  782. {
  783. floatingImgWidth = parseInt(getNatWidthOfEmbeddedImg(currentHoveredThumbnail));
  784.  
  785. floatingImgLeft = event.pageX + floatingImgRightOffset;
  786. /*if right of image exceeds beyond right of window, restrict max width*/
  787. if (floatingImgLeft + floatingImgWidth > $(window).scrollLeft() + $(window).width() - floatingImgBorder)
  788. {
  789. floatingImgWidth = $(window).scrollLeft() + $(window).width() - floatingImgBorder - floatingImgLeft;
  790. }
  791. floatingImgHeight = Math.round(getNatHeightOfEmbeddedImg(currentHoveredThumbnail) * (floatingImgWidth / getNatWidthOfEmbeddedImg(currentHoveredThumbnail)));
  792. floatingImgTop = event.pageY - (floatingImgHeight / 2);
  793. /*if bottom of image exceeds beyond the window, shift top upwards*/
  794. if (floatingImgTop + floatingImgHeight > $(window).scrollTop() + $(window).height() - floatingImgBorder)
  795. {
  796. floatingImgTop = $(window).scrollTop() + $(window).height() - floatingImgBorder - floatingImgHeight;
  797. }
  798. /*if top of image expands beyond top of window, lower top of image*/
  799. if (floatingImgTop < $(window).scrollTop() + floatingImgBorder)
  800. {
  801. floatingImgTop = $(window).scrollTop() + floatingImgBorder;
  802. }
  803. /*if bottom of image exceeds beyond the window, restrict max height*/
  804. if (floatingImgTop + floatingImgHeight > $(window).scrollTop() + $(window).height() - floatingImgBorder)
  805. {
  806. floatingImgHeight = $(window).scrollTop() + $(window).height() - floatingImgBorder - floatingImgTop;
  807. }
  808. /*if floating image doesn't exist, create it*/
  809. if (!isFloatingImgExist)
  810. {
  811. floatingImgStyleStr =
  812. "position: absolute;" +
  813. "left: " + floatingImgLeft + "px;" +
  814. "top: " + floatingImgTop + "px;" +
  815. "max-width: " + floatingImgWidth + "px;" +
  816. "max-height: " + floatingImgHeight + "px;" +
  817. "z-index: 100;";
  818. /*if webm video*/
  819. if (currentHoveredThumbnail.hasClass("webmExt"))
  820. {
  821. floatingImgSrc =
  822. "<video id='floatingImg' style='" + floatingImgStyleStr + "'>" +
  823. "<source src='" + currentHoveredThumbnail.children("source").attr("src") + "' type='video/webm'>" +
  824. "</video>";
  825. }
  826. else
  827. {
  828. floatingImgSrc =
  829. "<img id='floatingImg' style='" + floatingImgStyleStr + "' src='" + currentHoveredThumbnail.attr("src") + "'>";
  830. }
  831. isFloatingImgExist = true;
  832. /*create the floating image*/
  833. $("body").append(
  834. floatingImgSrc
  835. );
  836. /*if webm video*/
  837. if (currentHoveredThumbnail.hasClass("webmExt"))
  838. {
  839. var video = $("#floatingImg");
  840.  
  841. video.attr("loop", "");
  842. video[0].play();
  843. }
  844. }
  845. else /*if floating image already exists, update its size and position*/
  846. {
  847. $("#floatingImg").css({
  848. "left": floatingImgLeft + "px",
  849. "top": floatingImgTop + "px",
  850. "max-width": floatingImgWidth + "px",
  851. "max-height": floatingImgHeight + "px",
  852. });
  853. }
  854. }
  855. /*if user is not hovering over thumbnail and floating image still exists*/
  856. else if (currentHoveredThumbnail == null && isFloatingImgExist)
  857. {
  858. $("#floatingImg").remove();
  859. isFloatingImgExist = false;
  860. }
  861. });
  862.  
  863.  
  864.  
  865. /*if mouse hovers inside the image, change cursor to pointer*/
  866. $(document).on("mouseover", ".embeddedImg", function(event){
  867. $("html").css("cursor", "pointer");
  868. if (! $(this).hasClass("expandedImg"))
  869. {
  870. currentHoveredThumbnail = $(event.target);
  871. }
  872. });
  873.  
  874. /*if mouse hovers outside the image, restore cursor to default graphic*/
  875. $(document).on("mouseout", ".embeddedImg", function(){
  876. $("html").css("cursor", "default");
  877. currentHoveredThumbnail = null;
  878. });
  879.  
  880.  
  881.  
  882. /*if clicked on the image URL anchor that's surrounding the embedded image, prevent default
  883. behaviour of anchor, unless it's a middle click*/
  884. $("body").on("click", ".embeddedImgAnchor", function(event){
  885. /*left-click*/
  886. if (event.which == 1)
  887. {
  888. }
  889. else
  890. {
  891. $(this).attr("href", $(this).attr("data-backup-href"));
  892. }
  893. });
  894.  
  895. $("body").on("mousedown", ".embeddedImgAnchor", function(event){
  896. /*left-click*/
  897. if (event.which == 1)
  898. {
  899. event.preventDefault();
  900. }
  901. else
  902. {
  903. $(this).attr("href", $(this).attr("data-backup-href"));
  904. }
  905. });
  906.  
  907.  
  908. /*toggle image size on left-click*/
  909. $("body").on("click", ".embeddedImg", function(event){
  910. /*left-click*/
  911. if (event.which == 1)
  912. {
  913. var mouseRelativeY = event.pageY - $(this).offset().top;
  914. /*if expanded webm file, don't shrink to thumbnail if clicked on its controls*/
  915. if ($(this).hasClass("webmExt") && $(this).hasClass("expandedImg") &&
  916. (mouseRelativeY > ($(this).height() - 28)))
  917. {
  918. $(this).parent().attr("href", "javascript:void(0);");
  919. }
  920. else
  921. {
  922. toggleEmbeddedImgSize($(this));
  923. $(window).scrollTop( $(this).parent().parent().offset().top );
  924. return false;
  925. }
  926. }
  927. });
  928.  
  929.  
  930.  
  931. function toggleEmbeddedImgSize(embeddedImg)
  932. {
  933. if (isSpoilersMode)
  934. {
  935. /*if going to be expanded*/
  936. if (embeddedImg.hasClass("thumbnailImg"))
  937. {
  938. embeddedImg.removeClass("thumbnailImg_spoilersExt");
  939. embeddedImg.parent().removeClass("embeddedImgAnchor_spoilersExt");
  940. }
  941. else /*if going to be in thumbnail mode*/
  942. {
  943. embeddedImg.addClass("thumbnailImg_spoilersExt");
  944. embeddedImg.parent().addClass("embeddedImgAnchor_spoilersExt");
  945. }
  946. }
  947. embeddedImg.toggleClass("thumbnailImg expandedImg");
  948. /*if expanded*/
  949. if (embeddedImg.hasClass("expandedImg"))
  950. {
  951. currentHoveredThumbnail = null;
  952. $("#floatingImg").remove();
  953. isFloatingImgExist = false;
  954.  
  955. embeddedImg.css("max-width", getOptimalExpandedImgMaxWidth(embeddedImg));
  956. embeddedImg.css("max-height", "");
  957. /*if webm file, start playing it*/
  958. if (embeddedImg.hasClass("webmExt"))
  959. {
  960. embeddedImg[0].play();
  961. embeddedImg.attr("controls", "");
  962. }
  963. }
  964. else /*if shrinking image back to thumbnail*/
  965. {
  966. embeddedImg.css("max-width", thumbnailImgWidth + "px");
  967. embeddedImg.css("max-height", thumbnailImgHeight + "px");
  968. /*if webm file, stop playing it*/
  969. if (embeddedImg.hasClass("webmExt"))
  970. {
  971. embeddedImg[0].pause();
  972. embeddedImg.removeAttr("controls");
  973. }
  974. }
  975. }
  976.  
  977.  
  978.  
  979. /*
  980. Get the optimal max-width for an expanded image.
  981.  
  982. The width will either be the length between the left of the thumbnail and the
  983. right of the image container, or the length between the left of the thumbnail
  984. and right of the window size.
  985.  
  986. @param thumbnailImg :: jQuery object of the image to be expanded. It has the
  987. class "thumbnailImg"
  988.  
  989. The returned width will be the smaller of the two.
  990. */
  991. function getOptimalExpandedImgMaxWidth(thumbnailImg)
  992. {
  993. var toContainerWidth;
  994. var toWindowWidth;
  995. toContainerWidth =
  996. thumbnailImg.parent().parent().width();
  997. toWindowWidth =
  998. $(window).width() - thumbnailImg.offset().left;
  999. if (toContainerWidth > toWindowWidth)
  1000. {
  1001. return toWindowWidth;
  1002. }
  1003. else
  1004. {
  1005. return toContainerWidth;
  1006. }
  1007. }
  1008.  
  1009.  
  1010.  
  1011.  
  1012.  
  1013.  
  1014.  
  1015.  
  1016.  
  1017.  
  1018.  
  1019.  
  1020. $("body").on("click", ".imgCloseAll", function(event) {
  1021. event.preventDefault();
  1022. /*for every expanded embedded image*/
  1023. $(".embeddedImg.expandedImg").each(function(index, element) {
  1024. toggleEmbeddedImgSize($(this));
  1025. });
  1026. $(window).scrollTop( $(this).parent().offset().top );
  1027. });
  1028.  
  1029.  
  1030. $("body").on("click", ".imgClosePost", function(event) {
  1031. var postID = $(this).attr("data-postID");
  1032. event.preventDefault();
  1033. /*for every expanded embedded image with same post ID*/
  1034. $(".embeddedImg[data-postID="+postID+"].expandedImg").each(function(index, element) {
  1035. toggleEmbeddedImgSize($(this));
  1036. });
  1037. $(window).scrollTop( $(this).parent().offset().top );
  1038. });
  1039.  
  1040.  
  1041. $("body").on("click", ".imgExpandPost", function(event) {
  1042. var postID = $(this).attr("data-postID");
  1043.  
  1044. event.preventDefault();
  1045. /*for every embedded image with same post ID*/
  1046. $(".embeddedImg[data-postID='"+postID+"']").each(function(index, element) {
  1047. /*if image already expanded*/
  1048. if ($(this).hasClass("expandedImg"))
  1049. {
  1050. /*update its max-width*/
  1051. $(this).css("max-width", getOptimalExpandedImgMaxWidth($(this)))
  1052. }
  1053. else
  1054. {
  1055. toggleEmbeddedImgSize($(this));
  1056. }
  1057. });
  1058. $(window).scrollTop( $(this).parent().offset().top );
  1059. });
  1060.  
  1061.  
  1062.  
  1063. $("body").on("click", ".imgExpandAll", function(event) {
  1064. event.preventDefault();
  1065. /*for every embedded image*/
  1066. $(".embeddedImg").each(function(index, element) {
  1067. /*if image already expanded*/
  1068. if ($(this).hasClass("expandedImg"))
  1069. {
  1070. /*update its max-width*/
  1071. $(this).css("max-width", getOptimalExpandedImgMaxWidth($(this)))
  1072. }
  1073. else
  1074. {
  1075. toggleEmbeddedImgSize($(this));
  1076. }
  1077. });
  1078. $(window).scrollTop( $(this).parent().offset().top );
  1079. });
  1080.  
  1081.  
  1082.  
  1083.  
  1084.  
  1085.  
  1086.  
  1087.  
  1088.  
  1089.  
  1090. $("body").on("click", ".imgBlacklistToggle", function(event) {
  1091. event.preventDefault();
  1092. var url = $(this).siblings(".embeddedImgAnchor").attr("href");
  1093. url.trim();
  1094. /*if image isn't blacklisted (but going to blacklist)*/
  1095. if (! $(this).siblings(".embeddedImgAnchor").hasClass("blacklisted"))
  1096. {
  1097. blacklist.push(url);
  1098. /*for every embedded image anchor*/
  1099. $(".embeddedImgAnchor").each(function(index, element) {
  1100. /*if has URL that was just blacklisted, hide it*/
  1101. if (isURLmatchBlacklistedURL(url, $(this).attr("href")))
  1102. {
  1103. $(this).siblings(".imgBlacklistToggle").html("whitelist");
  1104. $(this).siblings(".imgBlacklistToggle").attr("title", "Whitelist image");
  1105. $(this).addClass("blacklisted");
  1106. $(this).hide();
  1107. }
  1108. });
  1109. }
  1110. else /*if image is blacklisted (but going to whitelist)*/
  1111. {
  1112. /*remove url from blacklist*/
  1113. var urlIdx = blacklist.indexOf(url);
  1114. if (urlIdx > -1)
  1115. {
  1116. blacklist.splice(urlIdx, 1);
  1117. }
  1118. /*for every embedded image anchor*/
  1119. $(".embeddedImgAnchor").each(function(index, element) {
  1120. /*if has URL that was just whitelisted, show it*/
  1121. if ($(this).attr("href") == url)
  1122. {
  1123. $(this).siblings(".imgBlacklistToggle").html("blacklist");
  1124. $(this).siblings(".imgBlacklistToggle").attr("title", "Blacklist image");
  1125. $(this).removeClass("blacklisted");
  1126. $(this).show();
  1127. }
  1128. });
  1129. }
  1130. settings.blacklistStr = blacklist.join("\n");
  1131. localStorage.setItem("imagefaqs", JSON.stringify(settings));
  1132. });
  1133.  
  1134.  
  1135.  
  1136. })(); /*end of greasemonkey script*/