Tsu Helper

Tsu script that adds a bunch of tweaks to make Tsu more user friendly.

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

  1. // ==UserScript==
  2. // @name Tsu Helper
  3. // @namespace tsu-helper
  4. // @description Tsu script that adds a bunch of tweaks to make Tsu more user friendly.
  5. // @include http://*tsu.co*
  6. // @include https://*tsu.co*
  7. // @version 1.0
  8. // @author Armando Lüscher
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. /**
  13. * For changelog see https://github.com/noplanman/tsu-helper/blob/master/CHANGELOG.md
  14. */
  15.  
  16. $( document ).ready(function () {
  17.  
  18. /***************
  19. HELPER FUNCTIONS
  20. ***************/
  21.  
  22. /**
  23. * Base64 library, just decoder: http://www.webtoolkit.info/javascript-base64.html
  24. * @param {string} e Base64 string to decode.
  25. */
  26. function base64_decode(e){var t="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";var n="";var r,i,s;var o,u,a,f;var l=0;e=e.replace(/[^A-Za-z0-9\+\/\=]/g,"");while(l<e.length){o=t.indexOf(e.charAt(l++));u=t.indexOf(e.charAt(l++));a=t.indexOf(e.charAt(l++));f=t.indexOf(e.charAt(l++));r=o<<2|u>>4;i=(u&15)<<4|a>>2;s=(a&3)<<6|f;n=n+String.fromCharCode(r);if(a!=64){n=n+String.fromCharCode(i)}if(f!=64){n=n+String.fromCharCode(s)}}return n}
  27.  
  28.  
  29. // Output console messages?
  30. var debug = false;
  31.  
  32. // Define the maximum number of hashtags and mentions allowed.
  33. var maxHashtags = 10;
  34. var maxMentions = 10;
  35.  
  36. // URL where to get the newest script.
  37. var scriptURL = 'https://greasyfork.org/scripts/6372-tsu-helper/code/Tsu%20Helper.user.js';
  38.  
  39. var localVersion = 1.0;
  40. var getVersionAPIURL = 'https://api.github.com/repos/noplanman/tsu-helper/contents/VERSION';
  41. // Check for remote version number.
  42. checkRemoteVersion();
  43.  
  44. // Set focus to message entry field on page load.
  45. $( '#message' ).focus();
  46.  
  47. // Auto-focus title entry field when adding title.
  48. $( 'body' ).on( 'click', '.create_post .options .add_title', function() {
  49. var $postTitle = $( this ).closest( '#create_post_form' ).find( '#title' );
  50. setTimeout( function() { $postTitle.focus(); }, 50);
  51. });
  52.  
  53. // Auto-focus message entry field when adding/removing image.
  54. $( 'body' ).on( 'click', '.create_post .options .filebutton, .cancel_icon_createpost', function() {
  55. var $postMessage = $( this ).closest( '#create_post_form' ).find( '#message' );
  56. setTimeout( function() { $postMessage.focus(); }, 50);
  57. });
  58.  
  59.  
  60. /**
  61. * Check for the maximum number of hashtags and mentions.
  62. * @param {string} message The message being posted.
  63. * @return {boolean} True = submit, False = cancel, Null = not too many
  64. */
  65. function checkMaximumHashtagsMentions( message ) {
  66.  
  67. // Get number of hashtags and mentions in the message.
  68. var nrOfHashtags = message.split( '#' ).length - 1;
  69. doLog( nrOfHashtags + ' Hashtags found.' );
  70. var nrOfMentions = message.split( '@' ).length - 1;
  71. doLog( nrOfMentions + ' Mentions found.' );
  72.  
  73. // If the limits aren't exeeded, just go on to post.
  74. if ( nrOfHashtags <= maxHashtags && nrOfMentions <= maxMentions ) {
  75. return null;
  76. }
  77.  
  78. // Set up warning message.
  79. var warning = 'Limits exceeded, check your message! Are you sure you want to continue?\n';
  80. if ( nrOfHashtags > maxHashtags ) {
  81. warning += '\n' + nrOfHashtags + ' #hashtags found. (Max. ' + maxHashtags + ')'
  82. doLog( 'Too many hashtags found! (' + nrOfHashtags + ')', 'w' );
  83. }
  84. if ( nrOfMentions > maxMentions ) {
  85. warning += '\n' + nrOfMentions + ' @mentions found. (Max. ' + maxMentions + ')'
  86. doLog( 'Too many mentions found! (' + nrOfMentions + ')', 'w' );
  87. }
  88.  
  89. // Last chance to make sure about hashtags and mentions.
  90. return confirm( warning );
  91. }
  92.  
  93. /**
  94. * Check if the social network sharing has been selected.
  95. * @param {jQuery} $form Form jQuery object of the form being submitted.
  96. * @return {boolean} True = submit, False = cancel, Null = all selected
  97. */
  98. function checkSocialNetworkSharing( $form ) {
  99.  
  100. var share_facebook = null;
  101. var share_twitter = null;
  102.  
  103. // Get all visible (connected) checkboxes. If any are not checked, show warning.
  104. $form.find( '.checkboxes_options_create_post input:visible' ).each(function() {
  105. switch ( $( this ).attr( 'id' ) ) {
  106. case 'facebook': share_facebook = $( this ).prop( 'checked' ); break;
  107. case 'twitter': share_twitter = $( this ).prop( 'checked' ); break;
  108. }
  109. });
  110.  
  111. if ( false !== share_facebook && false !== share_twitter ) {
  112. return null;
  113. }
  114.  
  115. var post_to = 'OK = Post to Tsu';
  116.  
  117. // Share to facebook?
  118. if ( true === share_facebook ) {
  119. post_to += ', Facebook';
  120. }
  121. // Share to twitter?
  122. if ( true === share_twitter ) {
  123. post_to += ', Twitter';
  124. }
  125.  
  126. // Last chance to enable sharing to social networks...
  127. return confirm( post_to + '\nCancel = Choose other social networks' );
  128. }
  129.  
  130. /**
  131. * Called on form submit.
  132. * @param {jQuery} $form Form jQuery object of the form being submitted.
  133. * @param {event} event The form submit event.
  134. */
  135. function formSubmit( $form, event ) {
  136. var $message = $form.find( '#message' );
  137. var message = $message.val();
  138.  
  139. // In case the post gets canceled, make sure the message field is focused.
  140. $message.focus();
  141.  
  142. // Make sure something was entered.
  143. // Check for the maximum number of hashtags and mentions,
  144. // and if the Social network sharing warning has been approved.
  145. if ( '' != message
  146. && false !== checkMaximumHashtagsMentions( message )
  147. && false !== checkSocialNetworkSharing( $form ) ) {
  148. return;
  149. }
  150.  
  151.  
  152. /**************************
  153. * CANCEL FORM SUBMISSION! *
  154. **************************/
  155.  
  156. // Prevent form post.
  157. event.preventDefault();
  158.  
  159. // Hide the loader wheel.
  160. $form.find( '.loading' ).hide();
  161.  
  162. // Make sure to enable the post button again. Give it some time, as Tsu internal script sets it to disabled.
  163. setTimeout(function(){
  164. $form.find( '#create_post_button' ).removeAttr( 'disabled' );
  165. }, 500 );
  166. }
  167.  
  168. // Remind to post to FB and Twitter in case forgotten to click checkbox.
  169. $( '#create_post_form' ).submit(function( event ) {
  170. return formSubmit( $( this ), event );
  171. });
  172.  
  173. // Are we busy waiting for the popup to appear?
  174. var busyWaiting = false;
  175.  
  176. /**
  177. * Wait for the fancybox popup to create a new post.
  178. */
  179. function waitForPopup() {
  180.  
  181. var $form = $( '.fancybox-overlay #create_post_form' );
  182. if ( $form.length ) {
  183. $form.find( '#message' ).focus();
  184. $form.submit(function( event ) {
  185. return formSubmit( $( this ), event );
  186. });
  187. busyWaiting = false;
  188. return;
  189. }
  190.  
  191. // Wait around for it longer...
  192. setTimeout(function() {
  193. waitForPopup();
  194. }, 500);
  195. }
  196.  
  197. // When using the "Create" button, wait for the post input form.
  198. $( '.create_post_popup' ).click(function() {
  199. if ( busyWaiting ) {
  200. return;
  201. }
  202. busyWaiting = true;
  203. waitForPopup();
  204. });
  205.  
  206. // Open post by double clicking header.
  207. $( 'body' ).on( 'dblclick', '.post_header_name, .share_header', function( event ) {
  208. //var post_id = $( this ).closest( '.post' ).data( 'post-id' );
  209. var $post = $( this ).closest( '.post' );
  210. var isShare = $post.find( '.share_header' ).length;
  211. var original = ! $( this ).hasClass( 'share_header' );
  212. $post.find( '#post_link_dropdown a' ).each(function() {
  213. var linkText = $( this ).text().trim().toLowerCase();
  214. if ( ( ! isShare && 'open' === linkText )
  215. || ( ! original && 'open' === linkText )
  216. || ( original && 'open original post' === linkText ) ) {
  217.  
  218. var url = $( this ).attr( 'href' );
  219. // If the shift key is pressed, open in new window / tab.
  220. if ( event.shiftKey ) {
  221. window.open( url, '_blank' ).focus();
  222. } else {
  223. window.location = url;
  224. }
  225. return;
  226. }
  227. });
  228. });
  229.  
  230. /**
  231. * Make a log entry if debug mode is active.
  232. * @param {string} logMessage Message to write to the log console.
  233. * @param {string} level Level to log ([l]og,[i]nfo,[w]arning,[e]rror).
  234. * @param {boolean} alsoAlert Also echo the message in an alert box.
  235. */
  236. function doLog( logMessage, level, alsoAlert ) {
  237. if ( debug ) {
  238. switch( level ) {
  239. case 'i': console.info( logMessage ); break;
  240. case 'w': console.warn( logMessage ); break;
  241. case 'e': console.error( logMessage ); break;
  242. default: console.log( logMessage );
  243. }
  244. if ( alsoAlert ) {
  245. alert( logMessage );
  246. }
  247. }
  248. }
  249.  
  250. /**
  251. * Get the remote version on GitHub and output a message if a newer version is found.
  252. */
  253. function checkRemoteVersion() {
  254. $.getJSON( getVersionAPIURL, function ( response ) {
  255. var remoteVersion = parseFloat( base64_decode( response.content ) );
  256. doLog( 'Versions: Local (' + localVersion + '), Remote (' + remoteVersion + ')', 'i' );
  257.  
  258. // Check if there is a newer version available.
  259. if ( remoteVersion > localVersion ) {
  260. // Change the background color of the name tab on the top right.
  261. $( '#navBarHead .tab.name' ).css( 'background-color', '#F1B054' );
  262.  
  263. // Make sure the update link doesn't already exist!
  264. if ( 0 === $( '#tsu-helper-menuitem-update' ).length ) {
  265. var $updateLink = $( '<a/>', {
  266. title: 'Update Tsu Helper script to the newest version (' + remoteVersion + ')',
  267. href: scriptURL,
  268. html: 'Update Tsu Helper!'
  269. })
  270. .attr( 'target', '_blank' ) // Open in new window / tab.
  271. .css( { 'background-color' : '#F1B054', 'color' : '#fff' } ) // White text on orange background.
  272. .click(function() {
  273. if ( ! confirm( 'Upgrade to the newest version (' + remoteVersion + ')?\n\n(refresh this page after the script has been updated)' ) ) {
  274. return false;
  275. }
  276. });
  277.  
  278. $( '<li/>', { 'id': 'tsu-helper-menuitem-update', html: $updateLink } )
  279. .appendTo( '#navBarHead .sub_nav' );
  280. }
  281.  
  282. }
  283. })
  284. .fail(function() { doLog( 'Couldn\'t get remote version number for Tsu Helper.', 'w' ); });
  285. }
  286.  
  287. })();