My Function library

enter something useful

目前為 2016-09-24 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/9160/149090/My%20Function%20library.js

  1. "use strict";
  2. //// ==UserScript==
  3. // @name My Function library
  4. // @namespace http://use.i.E.your.homepage/
  5. // @version 0.45
  6. // @description enter something useful
  7. // @grant GM_getValue
  8. // @grant GM_setValue
  9. // @grant GM_deleteValue
  10. // @grant GM_listValues
  11. // @grant GM_xmlhttpRequest
  12. // @run-at document-start
  13.  
  14. // @created 2015-04-06
  15. // @released 2014-00-00
  16. // @updated 2014-00-00
  17. // @history @version 0.25 - first version: public@released - 2015-04-12
  18. // @history @version 0.30 - Second version: public@released - 2015-12-10
  19. // @history @version 0.35 - Second version: public@released - 2016-03-04
  20. // @history @version 0.45 - Second version: public@released - 2016-09-24
  21. // @compatible Greasemonkey, Tampermonkey
  22. // @license GNU GPL v3 (http://www.gnu.org/copyleft/gpl.html)
  23. // @copyright 2014+, Magnus Fohlström
  24. // ==/UserScript==
  25.  
  26. /*global $, jQuery*/
  27. /*jshint -W014, -W030, -W082*/
  28. // -W014, laxbreak, Bad line breaking before '+'
  29. // -W030, Expected assignment or function call instead saw an expression
  30. // -W082, a function declaration inside a block statement
  31.  
  32. /*
  33. $("li").not(function() {
  34. // returns true for those elements with at least one span as child element
  35. return $(this).children('span').length > 0
  36. }).each(function() { /* ... })
  37. */
  38. //noinspection JSUnresolvedFunction,JSUnresolvedVariable,BadExpressionStatementJS
  39. performance;
  40.  
  41. function setGM(){
  42. console.log('setGM' );
  43. window.GM_getValue = function( key, def ){
  44. return localStorage[key] || def;
  45. };
  46. window.GM_setValue = function( key, value ){
  47. localStorage[key] = value;
  48. };
  49. window.GM_deleteValue = function( key ){
  50. delete localStorage[ key ];
  51. };
  52. window.GM_listValues = function( ){
  53. return Object.keys( localStorage );
  54. };
  55. window.GM_lister = function( remove, item ){
  56. var keys = window.GM_listValues(), i = 0, val;
  57. for ( i; i <= keys.length; i++ ) {
  58. val = keys[i];
  59. val !== undefined && (
  60. console.log( 'GM_ListItem: ' + val + ':', window.GM_getValue( val ) ),
  61. ( ( item !== undefined && val.inElem( item ) ) || item === undefined )
  62. && Boolean.parse( remove ) && window.GM_deleteValue( val ) );
  63. }
  64. };
  65. }
  66.  
  67. console.log('isFunction( GM_getValue() )', $.isFunction( window.GM_getValue ) );
  68.  
  69. !!$.isFunction( window.GM_getValue ) || setGM();
  70.  
  71. (function(){
  72.  
  73. var eventMatchers = {
  74. 'HTMLEvents': /^(?:load|unload|abort|error|select|change|submit|reset|focus|blur|resize|scroll)$/,
  75. 'MouseEvents': /^(?:click|mouse(?:down|up|over|move|enter|out))$/
  76. };
  77.  
  78. var defaultOptions = {
  79. pointerX: 0,
  80. pointerY: 0,
  81. button: 0,
  82. ctrlKey: false,
  83. altKey: false,
  84. shiftKey: false,
  85. metaKey: false,
  86. bubbles: true,
  87. cancelable: true
  88. };
  89.  
  90. jQuery.fn.simulate = function(eventName) {
  91. var element = this[0];
  92. var options = $.extend(true, defaultOptions, arguments[2] || { });
  93. var oEvent, eventType = null;
  94.  
  95. for (var name in eventMatchers) {
  96. if (eventMatchers[name].test(eventName)) { eventType = name; break; }
  97. }
  98.  
  99. if (!eventType)
  100. throw new SyntaxError('Only HTMLEvents and MouseEvents interfaces are supported');
  101.  
  102. if (document.createEvent) {
  103. oEvent = document.createEvent(eventType);
  104. if (eventType == 'HTMLEvents') {
  105. oEvent.initEvent(eventName, options.bubbles, options.cancelable);
  106. }
  107. else {
  108. oEvent.initMouseEvent(eventName, options.bubbles, options.cancelable, document.defaultView,
  109. options.button, options.pointerX, options.pointerY, options.pointerX, options.pointerY,
  110. options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, options.button, element);
  111. }
  112. element.dispatchEvent(oEvent);
  113. }
  114. else {
  115. options.clientX = options.pointerX;
  116. options.clientY = options.pointerY;
  117. oEvent = $.extend(document.createEventObject(), options);
  118. element.fireEvent('on' + eventName, oEvent);
  119. }
  120. return element;
  121. };
  122. })();
  123.  
  124. window.onerror = function (errorMsg, url, lineNumber, column, errorObj) {
  125. console.debug('Error: ' + errorMsg + '\nScript: ' + url + '\nLine: ' + lineNumber
  126. + '\nColumn: ' + column + '\nStackTrace: ' + errorObj);
  127. };
  128. /**
  129. * @namespace waitUntilExists_Intervals
  130. */
  131. $.fn.waitUntilExists = function (handler, shouldRunHandlerOnce, isChild){
  132. var found = 'found',
  133. $this = $(this.selector),
  134. $elements = $this.not(function () { return $(this).data(found); }).each(handler).data(found, true);
  135. if( !isChild ) {
  136. (window.waitUntilExists_Intervals = window.waitUntilExists_Intervals || {})[this.selector] =
  137. window.setInterval(function () {
  138. $this.waitUntilExists(
  139. handler, shouldRunHandlerOnce, true);
  140. }, 500);
  141. }
  142. else if (shouldRunHandlerOnce && $elements.length){
  143. window.clearInterval(window.waitUntilExists_Intervals[this.selector]);
  144. }
  145. return $this;
  146. };
  147.  
  148. $.extend( $.easing,{
  149. easeOutBounceSmall : function(x, t, b, c, d) {
  150. var ts=(t/=d)*t;
  151. var tc=ts*t;
  152. return b+c*(-16.195*tc*ts + 49.935*ts*ts + -53.785*tc + 21.795*ts + -0.75*t);
  153. },
  154. easeOutSmoothBounce : function(x, t, b, c, d) {
  155. var ts=(t/=d)*t;
  156. var tc=ts*t;
  157. return b+c*(-19.4293*tc*ts + 53.3838*ts*ts + -49.8485*tc + 15.8081*ts + 1.08586*t);
  158. }
  159. });
  160.  
  161. $.extend( $.fn, {
  162. // Name of our method & one argument (the parent selector)
  163. /**
  164. * Suppress all rules containing "unused" in this
  165. * class
  166. *
  167. * @SuppressWarnings("unused")
  168. */
  169. //noinspection JSUnusedProperty
  170. within: function( pSelector ) {
  171. // Returns a subset of items using jQuery.filter
  172. return this.filter(function(){
  173. // Return truthy/falsey based on presence in parent
  174. return $(this).closest( pSelector ).length;
  175. });
  176. /* Example
  177. $("li").within(".x").css("background", "red");
  178.  
  179. This selects all list items on the document, and then filters to only
  180. those that have .x as an ancestor. Because this uses jQuery internally,
  181. you could pass in a more complicated selector:
  182.  
  183. $("li").within(".x, .y").css("background", "red");
  184.  
  185. http://stackoverflow.com/questions/2389540/jquery-hasparent
  186. http://stackoverflow.com/a/2389549
  187. */
  188. }
  189. });
  190.  
  191. $.fn.extend({
  192. exists : function () {
  193. return !!this.length;
  194. },
  195. Exists : function ( callback ) {
  196. var self = this;
  197. var wrapper = (function(){
  198. function notExists(){}
  199. //noinspection JSPotentiallyInvalidConstructorUsage
  200. notExists.prototype.ExistsNot = function( fallback ){
  201. !self.length && fallback.call(); };
  202. //noinspection JSPotentiallyInvalidConstructorUsage
  203. return new notExists;
  204. })();
  205. self.length && callback.call();
  206. return wrapper;
  207.  
  208. /* And now i can write code like this -
  209. $("#elem").Exists(function(){
  210. alert ("it exists");
  211. }).ExistsNot(function(){
  212. alert ("it doesn't exist");
  213. });
  214. */
  215. },
  216. ifExists : function ( fn ) {
  217. this.length && fn( this );
  218. /*
  219. $("#element").ifExists( function( $this ){
  220. $this.addClass('someClass').animate({marginTop:20},function(){alert('ok')});
  221. });
  222. */
  223. },
  224. swapClass : function ( replace, newClass) {
  225. this.className.replace(replace, newClass);
  226. },
  227. toggleClasses : function ( add, remove, if_none) {
  228. var $this = $(this.selector);
  229. if_none !== undefined && !$this.hasClass(add) && !$this.hasClass(remove) && $this.addClass(if_none);
  230. $this.addClass(add).removeClass(remove);
  231. },
  232. thisCompStyle : function ( cssStyle ) {
  233. return cssStyle !== undefined ? this.getComputedStyle().getPropertyValue( cssStyle ) : this.getComputedStyle();
  234. },
  235. refreshElement : function ( speed, parentBoolean ) {
  236. var $elem = parentBoolean ? this.parent() : this, data = $elem.html();
  237. $elem.hide( speed / 2 ).empty().html( data ).fadeIn( speed );
  238. },
  239. hasId : function ( id ) {
  240. return id === this.attr('id');
  241. },
  242. hasClasses : function ( classes, any ) {
  243. classes = classes.split( classes.inElem(',') ? ',' : ' ' );
  244. var check = 0, i = 0;
  245. for ( i; i < classes.length; i++ ) {
  246. this.hasClass( classes[ i ] ) && check++;
  247. if ( any !== undefined && check !== 0 ) return true;
  248. }
  249. return check === classes.length;
  250. },
  251. hasNoChildren : function ( selection ) {
  252. return $( this ).filter( function(){
  253. return $( this ).children( selection ).length === 0;
  254. });
  255. },
  256. /* hasParent : function( parentSelection ){
  257. return parentSelection.inElem('#')
  258. ? this.parent().hasId( parentSelection.split('#').shift() )
  259. : this.parent().hasClass( parentSelection.split('.').shift() );
  260. },
  261. */
  262. hasAncestor : function ( Ancestor ) {
  263. return this.filter(function() {
  264. return !!$( this ).closest( Ancestor ).length;
  265. });
  266. //$('.element').hasAncestor('.container').myAction();
  267. },
  268. hasParent : function ( selection ) {
  269. return !!$( this ).parent( selection ).length;
  270. },
  271. hasParents : function ( selection ) {
  272. return !!$( this ).parents( selection ).length;
  273. },
  274. hasQuery : function ( query ) {
  275. return d.querySelector(query).length;
  276. },
  277. hasAttr : function ( name, val ) {
  278. var thisAttr = $( this ).attr( name );
  279. thisAttr =
  280. val !== undefined
  281. ? thisAttr === val
  282. : thisAttr;
  283. return ( typeof thisAttr !== "undefined" && thisAttr !== false && thisAttr !== null );
  284.  
  285. //return val !== undefined
  286. // ? attrName === val
  287. // : typeof( attrName ) !== 'undefined'; //$( this )[0].hasAttribute( name );
  288. },
  289. isTag : function ( tag ) {
  290. var e = this[0] || $('<undefined/>');
  291. //noinspection JSValidateTypes
  292. return e.nodeName !== undefined && e.nodeName.toLowerCase() === tag.toLowerCase();
  293. },
  294. isNode : function ( node ) {
  295. var e = this[0] || $('<undefined/>');
  296. //noinspection JSValidateTypes
  297. return e.nodeName !== undefined && e.nodeName.toLowerCase() === node.toLowerCase();
  298. },
  299. searchAttr : function ( search, type, chkLen ) { //bool name value length or 1 2 3 4
  300. var Attributes = this[0].attributes;
  301. c.i('Attributes', Attributes);
  302. if ( arguments.length === 0 ) {
  303. var obj = {};
  304. $.each( Attributes, function() {
  305. this.specified && ( obj[ this.name ] = this.value );
  306. });
  307. return obj;
  308. } else if( search !== undefined ) {
  309. var name = '', val = '';
  310. //noinspection FunctionWithInconsistentReturnsJS
  311. $.each( Attributes, function() {
  312. if( this.specified && type == 'length' ) {
  313. if( this.name.length > chkLen ) {
  314. name = this.name;
  315. return false;
  316. }
  317. }
  318. else if( this.specified && this.name.inElem( search ) ) {
  319. name = this.name;
  320. val = this.value;
  321. return false;
  322. }
  323. });
  324. return ( type == 'bool' || type == 1 ) ? name.length ? true : false :
  325. ( type == 'name' || type == 2 ) ? name :
  326. ( type == 'value' || type == 3 ) ? val :
  327. ( type == 'length' || type == 4 ) && name;
  328. }
  329. },
  330. findClass : function ( Class ) {
  331. return this.find('.' + Class);
  332. },
  333. href : function ( newURL ) {
  334. return arguments.length === 0 ? this.attr('href') : this.attr('href', newURL);
  335. },
  336. src : function ( newSRC ) {
  337. return arguments.length === 0 ? this.attr('src') : this.attr('src', newSRC);
  338. },
  339. equals : function ( compareTo ) {
  340. if (!compareTo || this.length != compareTo.length)
  341. return false;
  342.  
  343. for (var i = 0; i < this.length; ++i) {
  344. if (this[i] !== compareTo[i])
  345. return false;
  346. }
  347. return true;
  348. },
  349. justText : function ( newText, prepend ) {
  350. var $children = null,
  351. placement = prepend === undefined ? 'prepend':'append';
  352. if ( newText ) {
  353. $children = $( this )
  354. .children()
  355. .clone();
  356. $( this )
  357. .children()
  358. .remove()
  359. .end()
  360. .text( newText )
  361. [ placement ]( $children );
  362. return $(this);
  363. }
  364. else {
  365. return $.trim( $( this )
  366. .clone()
  367. .children()
  368. .remove()
  369. .end()
  370. .text());
  371. }
  372. },
  373. uncomment : function ( recurse ) {
  374. // <!-- hidden --> // this will reveal, whats inside. In this case it will bi the word hidden
  375. $( this ).contents().each(function() {
  376. if ( recurse && this.hasChildNodes() ) {
  377. $( this ).uncomment(recurse);
  378. } else if ( this.nodeType == 8 ) {
  379. // Need to "evaluate" the HTML content,
  380. // otherwise simple text won't replace
  381. var e = $('<span>' + this.nodeValue + '</span>');
  382. $( this ).replaceWith( e.contents() );
  383. }
  384. });
  385. // $('#uncomment').uncomment( true );
  386. // http://stackoverflow.com/a/22439787
  387. },
  388. getComment : function ( getValue ) {
  389. var COMMENT_NODE = this.contents().filter(function(){
  390. return this.nodeType == Node.COMMENT_NODE;
  391. });
  392. return getValue ?
  393. COMMENT_NODE.each(function(){
  394. return $( this ).nodeValue.str2html();
  395. })
  396. : COMMENT_NODE;
  397. },
  398. //$.get('defaultComp/foot.html', function(dataContent) {$('#foot').replaceWith(dataContent); });
  399. getURL : function ( url, how ) {
  400. how = how || 'html';
  401. var that = this;
  402. $.get( url, function(dataContent) {
  403. $(that)[ how ](dataContent);
  404. });
  405. return this;
  406. },
  407.  
  408. hasEvent : function ( event ) {
  409. var eventHandlerType;
  410. $( this ).on( event, clickEventHandler ).triggerHandler( event );
  411. function clickEventHandler( e ) {
  412. eventHandlerType = e.type;
  413. }
  414. return eventHandlerType === event;
  415. },
  416. scrollTune : function ( opt ){
  417. // $("body").scrollTune({ pps: 1700, pageY: config.pageY, easing:'easeOutSmoothBounce', hsc: true }).promise()
  418. // .done( function() { setTimeout(function(){ toggleClassState( config, 'fullPlayer', type ); },100); })
  419.  
  420. console.log('scrollTune');
  421.  
  422. var position,
  423. defaults = {
  424. tune: 0,
  425. speed: 0,
  426. pps: false, // pixel per second
  427. ppsM: 1000,
  428. pageYmini: 0,
  429. pageY: false, //{ pps: 300, pageY: event.pageY }
  430. hsc: false, // height speed compensation
  431. animate: false,
  432. // require http://gsgd.co.uk/sandbox/jquery/easing/jquery.easing.1.3.js or jQueryUI - if other than ' swing or linear '
  433. easing: "easeInOutCubic", // easeInOutCubic easeInOutQuad easeInOutElastic http://easings.net/
  434. delay: 0,
  435. varStore: '',
  436. varAltStore:false,
  437. name: false,
  438. start: false,
  439. startTime: 0,
  440. step: false,
  441. stepTime: 0,
  442. complete: false,
  443. completeTime: 0,
  444. done: false,
  445. doneTime: 0,
  446. goTo: $('body')
  447. },
  448. heightSpeedComp = function(){ return opt.hsc ? 1 + ( ( $(document).height() / opt.pageY ) / 1.4 ) : 1 ; },
  449. varStore = function( action, step ){
  450. opt.name !== false
  451. ? opt.varAltStore !== false
  452. ? (console.log('Store'), opt.varAltStore[opt.name][ action ](step))
  453. : (console.log('Store false'), opt.name[ action ](step))
  454. : opt.pageYmini < opt.pageY || opt.varStore === config
  455. ? (console.log('config'), opt.varStore[ action ](step))
  456. : (console.log('config false'), opt.varStore(step));
  457. };
  458.  
  459. console.log('opt.pageY',opt.pageY);
  460. opt = $.extend( {}, defaults, opt );
  461. position = ( $( this ).offset().top + opt.tune ) + 'px';
  462.  
  463. opt.pps !== false || opt.animate !== false || ( typeof opt.speed === 'string' ? opt.speed.length !== 0 : opt.speed !== 0 )
  464. ? (
  465. opt.speed = opt.pps !== false ? parseInt( ( opt.pageY / opt.pps * heightSpeedComp() ) * opt.ppsM ) : opt.speed,
  466. opt.goTo.delay( opt.delay ).animate(
  467. { scrollTop : position },
  468. { duration : opt.speed, easing: opt.easing,
  469. start : function(){
  470. opt.start && setTimeout(function(){
  471. console.log('start');
  472. varStore('start');
  473. }, opt.startTime );
  474. },
  475. step : function(i){
  476. opt.step && setTimeout(function(){
  477. console.log('step',i);
  478. varStore('step',i);
  479. }, opt.stepTime );
  480. },
  481. complete : function(){
  482. opt.complete && setTimeout(function(){
  483. console.log('complete');
  484. varStore('complete');
  485. }, opt.completeTime );
  486. },
  487. done : function(){
  488. opt.done && setTimeout(function(){
  489. console.log('done');
  490. varStore('done');
  491. }, opt.doneTime );
  492. }
  493. }
  494. )
  495. )
  496. : opt.goTo.scrollTop( position );
  497.  
  498. return this; // for chaining...
  499.  
  500. },
  501. scrollTuneOld : function ( opt ){
  502.  
  503. var position,
  504. defaults = {
  505. tune: 0,
  506. speed: 512,
  507. animate: false,
  508. goTo: $('html, body')
  509. };
  510.  
  511. opt = $.extend( {}, defaults, opt );
  512. position = ( $( this ).offset().top + opt.tune ) + 'px';
  513.  
  514. opt.animate !== false || ( typeof opt.speed === 'string' ? opt.speed.length !== 0 : opt.speed !== 0 )
  515. ? opt.goTo.animate({ scrollTop: position }, opt.speed )
  516. : opt.goTo.scrollTop( position );
  517.  
  518. return this; // for chaining...
  519. },
  520. qUnWrap : function (){
  521. $( this ).find(':first-child').unwrap();
  522. }
  523. });
  524.  
  525. $.extend({
  526. confirm: function (title, message, yesText, noText, yesCallback) {
  527. //dialog needs jQueryUI
  528. /*
  529. $.confirm(
  530. "CONFIRM", //title
  531. "Delete " + filename + "?", //message
  532. "Delete", //button text
  533. deleteOk //"yes" callback
  534. );
  535. */
  536. $("<div></div>").dialog( {
  537. buttons: [{
  538. text: yesText,
  539. click: function() {
  540. yesCallback();
  541. $( this ).remove();
  542. }
  543. },
  544. {
  545. text: noText,
  546. click: function() {
  547. $( this ).remove();
  548. }
  549. }
  550. ],
  551. close: function (event, ui) { $(this).remove(); },
  552. resizable: false,
  553. title: title,
  554. modal: true
  555. }).text(message).parent().addClass("alert");
  556. }
  557. });
  558. $.extend( $.expr[":"], {
  559. ldata: function(el, idx, selector) {
  560. var attr = selector[3].split(", ");
  561. return el.dataset[attr[0]] === attr[1];
  562. },
  563. value: function(el, idx, selector) {
  564. return el.value === selector[selector.length - 1];
  565. },
  566. isEmptyTrimmed: function(el){
  567. return !$.trim($(el).html());
  568. },
  569. data: $.expr.createPseudo
  570. ? $.expr.createPseudo(function( dataName ) {
  571. return function( elem ) {
  572. return !!$.data( elem, dataName );
  573. };
  574. })
  575. // support: jQuery <1.8
  576. : function( elem, i, match ) {
  577. return !!$.data( elem, match[ 3 ] );
  578. }
  579. });
  580.  
  581. $.isInArray = function( item, array ) {
  582. return !!~$.inArray(item, array);
  583. };
  584. $.allVar = function( array, value, all, atLeast ) {
  585. var count = 0,
  586. arrLength = array.length,
  587. isBool = typeof value === 'boolean';
  588.  
  589. $.each( array, function( i, e ){
  590. value === ( isBool ? !!e : e ) && count++;
  591. });
  592.  
  593. return all ? count === arrLength : count >= atLeast;
  594. };
  595. /*
  596. Object.defineProperty(HTMLMediaElement.prototype, 'playing', {
  597. //$( selector ).get(0).playing;
  598. get: function(){
  599. return !!( this.currentTime > 0 && !this.paused && !this.ended && this.readyState > 2 );
  600. }
  601. });
  602. */
  603. /* Object.prototype.hasAttribute = function( attrName, val ){
  604. var thisAttr =
  605. this.attr
  606. ? val !== undefined
  607. ? this.attr( attrName ) === val
  608. : this.attr( attrName )
  609. : this.getAttribute( attrName );
  610. return ( typeof thisAttr !== "undefined" && thisAttr !== false && thisAttr !== null );
  611. };
  612. */
  613. Array.prototype.findArrayObj = function( findKey, exactValue ){
  614. return $.grep( this, function( obj ){
  615. return obj[ findKey ] === exactValue;
  616. })[0];
  617. //This prototype doesn't modify the array,
  618. // it gets the element that contains key with correct value and
  619. // the returns that element
  620. };
  621. Array.prototype.removeArrayObj = function( findKey, exactValue ){
  622. //my own example test: http://jsfiddle.net/aPH7m/82/
  623. //This prototype doesn't modify the array, needs to be overwritten or put into new var array
  624. return $.grep( this, function( obj ) {
  625. return obj[ findKey ] !== exactValue;
  626. });
  627. };
  628. Array.prototype.addKeyToArrayObj = function( findKey, exactValue, newKey, newValue ){
  629. return $.grep( this, function( obj ){
  630. return obj[ findKey ] === exactValue ? obj[ newKey ] = newValue : obj[ findKey ] !== exactValue;
  631. });
  632. // This prototype doesn't modify the array,
  633. // it gets the element that contains key with correct value and
  634. // adds a new key with its value to that element
  635. };
  636.  
  637. Array.prototype.filterArrayObj = function( doWhat, findKey, exactValue, newKey, newValue ){
  638. return doWhat === 'remove'
  639. ? this.filter(function( obj ) {
  640. return obj[ findKey ] !== exactValue;
  641. })
  642. : doWhat === 'addKey'
  643. ? this.filter(function( obj ){
  644. return obj[ findKey ] === exactValue
  645. ? obj[ newKey ] = newValue
  646. : obj[ findKey ] !== exactValue;
  647. })
  648. : doWhat === 'find'
  649. && this.filter(function( obj ){
  650. return obj[ findKey ] === exactValue;
  651. })[0];
  652. };
  653. Array.prototype.grepArrayObj = function( doWhat, idKey, uniqueValue, theKey, theValue ){
  654. doWhat = doWhat === 'updateKey' ? 'addKey' : doWhat;
  655. return doWhat === 'remove'
  656. ? $.grep( this, function( obj ){
  657. return obj[ idKey ] !== uniqueValue;
  658. })
  659. : doWhat === 'addKey'
  660. ? $.grep( this, function( obj ){
  661. return obj[ idKey ] === uniqueValue
  662. ? (
  663. obj[ theKey ] = theValue,
  664. obj[ idKey ] === uniqueValue
  665. )
  666. : obj[ idKey ] !== uniqueValue;
  667. })
  668. : doWhat === 'find'
  669. ? $.grep( this, function( obj ){
  670. return obj[ idKey ] === uniqueValue;
  671. })[0]
  672. : doWhat === 'deleteKey'
  673. && $.grep( this, function( obj ){
  674. return obj[ idKey ] === uniqueValue
  675. ? (
  676. delete obj[ theKey ],
  677. obj[ idKey ] === uniqueValue
  678. )
  679. : obj[ idKey ] !== uniqueValue;
  680. });
  681. };
  682. Array.prototype.sortObjArray = function( key, reverse ){
  683. this.sort(function(a, b){
  684. return ( reverse || false ) ? b[key] - a[key] : a[key] - b[key];
  685. });
  686. };
  687.  
  688. //noinspection JSPrimitiveTypeWrapperUsage
  689. Boolean.parse = function(val) {
  690. // http://stackoverflow.com/a/24744599
  691. var falsely = /^(?:f(?:alse)?|no?|0+)$/i;
  692. return !falsely.test(val) && !!val;
  693. };
  694.  
  695.  
  696. /*
  697. Object.prototype.isObjectType = function( type, showType ){ //"[object Number]"
  698. var objectString = Object.prototype.toString.call( this );
  699. return showType === 'show' ? objectString :
  700. showType === 'exact' ? objectString === type :
  701. showType === 'search' && objectString.toLowerCase().inElem( type.toLowerCase() );
  702. };
  703. */
  704.  
  705. String.prototype.splitEvery = function( splitter, every ){
  706. var array = this.split( splitter ), returnString = '';
  707. $.each( array, function( index, elem ){
  708. returnString += elem + ( index < array.length - 1 || index % every === 0 ) ? '' : splitter;
  709. });
  710. return returnString;
  711. };
  712. String.prototype.advSplit = function( chr, nbr ){
  713. var str = this.split(chr),
  714. strLen = str.length,
  715. chrLen = chr.length,
  716. returnStr = ['',''],
  717. newArr = [];
  718.  
  719. $.each( str, function( index ){
  720. returnStr[ index < nbr ? 0 : 1 ] += str[ index ] + chr;
  721. });
  722.  
  723. $.each( returnStr, function( index ){
  724. returnStr[ index ] = returnStr[ index ].slice(0, - chrLen);
  725. returnStr[ index ].length > 0 && newArr.push( returnStr[ index] );
  726. });
  727.  
  728. return newArr;
  729. };
  730. String.prototype.advSplitJoin = function( chr, nbr, ips ){
  731.  
  732. var str = this.split(chr),
  733. strLen = str.length,
  734. ipsLen = ips.length,
  735. returnStr = '',
  736. returnStrLen;
  737.  
  738. $.each( str, function( index ) {
  739. var add = index < strLen - 1
  740. ? chr
  741. : '';
  742. returnStr += index + 1 === nbr
  743. ? str[index] + ips
  744. : str[index] + add;
  745. });
  746.  
  747. returnStrLen = returnStr.length;
  748. returnStr.slice( returnStrLen - ipsLen ) === ips
  749. && ( returnStr = returnStr.slice( 0, returnStrLen - ipsLen ) );
  750.  
  751. return returnStr;
  752. };
  753. String.prototype.extract = function( start, end, inside, newWay ){
  754. var str = this,
  755. myArray = [ true, 1, 'yes', 'inside' ],
  756. startCharIndex = str.indexOf( start ),
  757. endCharIndex = str.indexOf( end );
  758.  
  759. newWay = newWay !== undefined ? $.inArray( newWay, myArray ) !== -1 : false;
  760. inside = inside !== undefined ? $.inArray( inside, myArray ) !== -1 : false;
  761.  
  762. function simpler() {
  763. return inside
  764. ? str.split( start ).pop().split( end ).shift()
  765. : start + str.split( start ).pop().split( end ).shift() + end;
  766. }
  767. function older() {
  768. return inside //old buggy way, some old scripts may depends on it
  769. ? str.replace( start, '').replace( end, '')
  770. : str.substr( startCharIndex, endCharIndex );
  771. }
  772. return newWay ? simpler() : older()
  773. };
  774. String.prototype.extractNew = function( start, end, inside ){
  775. var str = this;
  776. return inside !== undefined && inside
  777. ? str.split( start ).pop().split( end ).shift()
  778. : inside
  779. || start + str.split( start ).pop().split( end ).shift() + end;
  780. };
  781.  
  782. String.prototype.charTrim = function( char ){
  783. // alert("...their.here.".charTrim('.'));
  784. var first_pos = 0,
  785. last_pos = this.length- 1, i ;
  786. //find first non needle char position
  787. for( i = 0; i < this.length; i++ ){
  788. if( this.charAt( i ) !== char ){
  789. first_pos = ( i == 0 ? 0 : i );
  790. break;
  791. }
  792. }
  793. //find last non needle char position
  794. for( i = this.length - 1; i > 0; i-- ){
  795. if( this.charAt( i ) !== char ){
  796. last_pos = ( i == this.length ? this.length: i + 1 );
  797. break;
  798. }
  799. }
  800. return this.substring( first_pos, last_pos );
  801. };
  802. String.prototype.reduceWhiteSpace = function(){
  803. return this.replace(/\s+/g, ' ');
  804. };
  805. String.prototype.formatString = function(){
  806.  
  807. var inputStr = this.toString().reduceWhiteSpace()
  808. .split('!').join(' !').split('!;').join("!important;")
  809. .split(/\s+/g).join(' ')
  810. .split('{').join('{\n\t')
  811. .split('; ').join(';')
  812.  
  813.  
  814.  
  815. .split('( ').join('(')
  816. .split(' )').join(')')
  817.  
  818. .split(' :').join(':')
  819.  
  820. .split(';').join(';\n\t')
  821. .split('*/').join('*/\n')
  822. .split(')*(').join(') * (')
  823. .split('}').join('}\n'),
  824. returnStr = '\t', pop;
  825.  
  826. $.each( inputStr.split('\n'), function ( i, elem ) {
  827.  
  828. elem.search( '{' ) === -1 && elem.search( ': ' ) === -1
  829. && ( elem.search( ':' ) > 1
  830. ? ( pop = elem.split(': ').join(':').split( ':' ).pop(), elem = elem.split( pop ).shift() + ' ' + pop )
  831. : elem.search(':') === 1 && ( elem = elem.split(': ').join(':').split( ':' ).join( ': ' ) ) );
  832. // : elem.search( '{' ) === 1 && ( elem.search( ': ' ) !== -1 || elem.search( ' :' ) !== -1 || elem.search( ' : ' ) !== -1 )
  833. // && ( elem = elem.split( ': ' ).join( ' :' ).split( ' :' ).join( ':' ).split( ' : ' ).join( ': ' ) );
  834.  
  835. returnStr += elem + '\n\t';
  836. });
  837. returnStr = returnStr.split('>').join(' > ').split(' > ').join(' > ').split( ': //' ).join( '://' ).split( ':url' ).join( ': url' );
  838. return returnStr.slice( 0, returnStr.lastIndexOf('}') ) + '}';
  839. };
  840.  
  841. /**
  842. * Parses mixed type values into booleans. This is the same function as filter_var in PHP using boolean validation
  843. * //@returny {Boolean|Null}
  844. */
  845. String.prototype.parseBooleanStyle = function( nullOnFailure ){
  846. nullOnFailure = nullOnFailure || false;
  847. var bool, $this = this.toString().toLowerCase();
  848. switch( $this ){
  849. case 'true':
  850. case '1':
  851. case parseInt( $this ) > 0:
  852. case 'on':
  853. case 'yes':
  854. bool = true;
  855. break;
  856. case 'false':
  857. case '0':
  858. case 'off':
  859. case 'no':
  860. bool = false;
  861. break;
  862. default:
  863. bool = nullOnFailure ? null : false;
  864. break;
  865. }
  866. return bool;
  867. };
  868. String.prototype.parseBool = function(){
  869. var thisStr = parseInt( this ) ? this === 0 ? 'false' : 'true' : '' + this,
  870. trueArray = [ 'on', 'yes','y', 'j', 'true', true ],
  871. falseArray = [ 'off', 'no', 'n', 'false', false ];
  872.  
  873. thisStr = thisStr.toLowerCase().trim();
  874.  
  875. return $.inArray( thisStr, trueArray ) !== -1 ? true :
  876. $.inArray( thisStr, falseArray ) !== -1 ? false : this;
  877. };
  878. String.prototype.isType = function( type ){
  879. return !!$.type( this ) === type;
  880. };
  881. String.prototype.undef = function( replace ){
  882. return this === undefined ? replace : this;
  883. };
  884. String.prototype.isUndefined = function( state, replace ){
  885. state = state !== undefined ? state : true;
  886. replace = replace !== undefined ? replace : true;
  887. return state ? this === undefined ? replace : this : state;
  888. };
  889.  
  890. String.prototype.inURL = function(){
  891. var winLoc = window.location.href;
  892. return winLoc.search(this) !== -1;
  893. };
  894. String.prototype.inString = function( string ){
  895. return string !== undefined ? string.search(this) !== -1 : false;
  896. };
  897. String.prototype.inElem = function( search ){
  898. return this !== undefined ? this.search(search) !== -1 : false;
  899. };
  900. String.prototype.count = function( char, UpperCase ){
  901. var numberOf = this.toString().match( new RegExp( char, ( UpperCase ? "gi" : "g" ) ) );
  902. return numberOf != null ? numberOf.length : 0;
  903. };
  904. String.prototype.startsWith = function( str ){
  905. return this.slice(0, str.length) == str;
  906. };
  907. String.prototype.removeStarts = function( many ){
  908. return this.substring( many - 1, this.length );
  909. };
  910. String.prototype.removeEnds = function( many ){
  911. return this.substring( 0, this.length - many );
  912. };
  913. String.prototype.endsWith = function( str ){
  914. return this.slice( -str.length ) == str;
  915. };
  916. String.prototype.capitalizeFirst = function(){
  917. return this.charAt(0).toUpperCase() + this.slice(1);
  918. };
  919. String.prototype.lpad = function( padString, length ){
  920. var str = this;
  921. while ( str.length < length ) {
  922. str = padString + str; }
  923. return str;
  924. };
  925.  
  926. // use full to convert String URL, so that you can use location commands
  927. String.prototype.toLocation = function(){
  928. var a = document.createElement('a');
  929. a.href = this;
  930. return a;
  931. };
  932. String.prototype.str2html = function(){
  933. return $('<div/>').html( this ).contents();
  934. };
  935. String.prototype.toStyle = function( styleId ){
  936. var cssID,
  937. cssSplit = this.split('¤'),
  938. cssStyled = cssSplit.pop().formatString();
  939. styleId = styleId !== undefined ? styleId : cssSplit.shift();
  940. cssID = $( 'head #' + styleId );
  941. cssID.length
  942. ? cssID.html( cssStyled )
  943. : $( $( '<style/>',{ id: styleId, class:'mySuperStyles', html: cssStyled } ) ).appendTo('head');
  944. };
  945.  
  946. //HTMLObjectElement.prototype.obj2Str = function(){var objArr = $.makeArray( this ); return objArr[0].outerHTML;};
  947. /*
  948. String.prototype.replaceAll = function( target, replacement ) {
  949. return this.split(target).join(replacement);
  950. };
  951. */
  952. function ScrollZoomTune( selection, zooms, tune, ani, speed ){
  953. //ScrollZoomTune("div.thumb .title a",1,-25,1,'slow');
  954. var body = $('body'), sel = $( selection), position;
  955. //noinspection JSValidateTypes
  956. sel.size() !== 0 && (
  957. body.css('zoom',zooms),
  958. position = sel.position().top + tune,
  959. ani === 1
  960. ? body.animate({ scrollTop: position * zooms }, speed )
  961. : body.scrollTop( position * zooms )
  962. );
  963. }
  964. function refreshElement( elem , speed ){ //refreshElement('.videoPlayer','slow');
  965. var $elem = $( elem ), data = $elem.html();
  966. $elem.empty().html( data ).fadeIn( speed );
  967. }
  968. function autoCopyToClipboard( input ){
  969. var $copyThis = $( '#copyThis' );
  970. $( 'body' ).append( $('<textarea/>',{ id:'copyThis', rows:"4", cols:"50", type:"text", value: input }) );
  971. $copyThis.focus().select();
  972. document.execCommand("copy");
  973. $copyThis.remove();
  974. }
  975. function toStyle( styleId, str ){
  976. var $id = $( 'head #' + styleId ),
  977. cssID = str.formatString();
  978. $id.length
  979. ? $id.html( cssID )
  980. : $( $( '<style/>',{ id: styleId, class:'mySuperStyles', html: cssID } ) ).appendTo('head');
  981. }
  982. function obj2Str( obj ){
  983. var objArr = $.makeArray(obj);
  984. return objArr[0].outerHTML;
  985. }
  986.  
  987. function sortBy(key, reverse) {
  988. // Usage: array.sort( sortBy( key, reverse ) )
  989.  
  990. // Move smaller items towards the front
  991. // or back of the array depending on if
  992. // we want to sort the array in reverse
  993. // order or not.
  994. var moveSmaller = reverse ? 1 : -1;
  995. // Move larger items towards the front
  996. // or back of the array depending on if
  997. // we want to sort the array in reverse
  998. // order or not.
  999. var moveLarger = reverse ? -1 : 1;
  1000. /**
  1001. * @param {*} a
  1002. * @param {*} b
  1003. * @return {Number}
  1004. */
  1005. return function (a, b) {
  1006. if (a[key] < b[key]) {
  1007. return moveSmaller;
  1008. }
  1009. if (a[key] > b[key]) {
  1010. return moveLarger;
  1011. }
  1012. return 0;
  1013. };
  1014. }
  1015.  
  1016. function VideoTitleA( elem , state ){
  1017. //VideoTitleA("div.thumb .title a",'on');
  1018. $( elem ).each(function(){
  1019. var $this = $(this),
  1020. strTitle = $this.attr('title'),
  1021. strText = $this.attr('data-text'),
  1022. strHtml = $this.text();
  1023. state === 'on' ? $this.text(strTitle).attr('data-text',strHtml) : $this.text(strText);
  1024. });
  1025. }
  1026. /**
  1027. * @return {string}
  1028. */
  1029. function MultiString( f ){
  1030. return f.toString().split('\n').slice(1, -1).join('\n');
  1031. }
  1032. function wrapWithTag( tag, text, selection ){
  1033. var thisAttr = selection !== undefined && selection.startsWith('.') ? 'class' : selection.startsWith('#') && 'id',
  1034. thisTag = $('<' + tag + '/>', { text: text });
  1035. return thisAttr.length ? thisTag.attr( thisAttr, selection.splice( 1 ) ) : thisTag;
  1036. }
  1037.  
  1038. function clean( node ) {
  1039. /*
  1040. So to clean those unwanted nodes from inside the <body> element, you would simply do this:
  1041.  
  1042. clean(document.body);
  1043. Alternatively, to clean the entire document, you could do this:
  1044.  
  1045. clean(document);
  1046. */
  1047. for( var n = 0; n < node.childNodes.length; n ++ ){
  1048. var child = node.childNodes[ n ];
  1049. ( child.nodeType === 8 || ( child.nodeType === 3 && !/\S/.test( child.nodeValue ) ) )
  1050. ? (
  1051. node.removeChild( child ),
  1052. n --
  1053. )
  1054. : child.nodeType === 1 && clean( child );
  1055. }
  1056. }
  1057. function commentsCleaner( array ){
  1058. array = array === undefined ? [ '*', document, 'html' ] : array;
  1059.  
  1060. // https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType
  1061. // http://stackoverflow.com/a/2364760
  1062. $.each( array, function( i, e ) {
  1063. $( e ).contents().each( function() {
  1064. this.nodeType === Node.COMMENT_NODE && $( this ).remove(); }); });
  1065. }
  1066.  
  1067. function newUrlNoReload( title, url ){
  1068. window.history.pushState("string", title, url );
  1069. }
  1070. function inURL( search, exact ){
  1071. exact = exact || false;
  1072. var winLoc = window.location.href;
  1073.  
  1074. return exact ? winLoc === search : winLoc.search( search ) !== -1;
  1075. }
  1076. function loadDoc( href ){
  1077. $( location ).attr('href', href );
  1078. }
  1079.  
  1080. function isPrimitiveType( value ){
  1081. // will return true if the value is a primitive value
  1082. switch ( typeof value ) {
  1083. case 'string': case 'number': case 'boolean': case 'undefined': {
  1084. return true;
  1085. }
  1086. case 'object': {
  1087. return !value;
  1088. }
  1089. }
  1090. return false;
  1091. }
  1092. function checkDividedIsInteger( num, div ){
  1093. return ( num % div === 0 );
  1094. }
  1095. function isEven( value ){
  1096. return ( value % 2 === 0 );
  1097. }
  1098. function inDom( array, onElement ) {
  1099. var found = false,
  1100. isArrayFN = function(){
  1101. $.each( array, function( i, value ) {
  1102. value = onElement !== undefined
  1103. ? onElement + value
  1104. : value;
  1105. if( $( value ).length ) {
  1106. found = true;
  1107. return false;
  1108. }
  1109. });
  1110. };
  1111.  
  1112. $.isArray( array )
  1113. ? isArrayFN()
  1114. : $( array ).length && ( found = true );
  1115.  
  1116. /**
  1117. * @return {boolean}
  1118. */
  1119. return found;
  1120. }
  1121. function isHTMLObject( obj ){
  1122. obj = typeof obj == 'string' ? obj : $( obj );
  1123. return obj.length
  1124. ? !!( obj instanceof HTMLElement || obj[0] instanceof HTMLElement)
  1125. : !!( obj && ( obj.nodeName || ( obj.prop && obj.attr && obj.find ) ) );
  1126. }
  1127.  
  1128. function isFunction( functionToCheck ){
  1129. var getType = {};
  1130. return functionToCheck && getType.toString.call( functionToCheck ) === '[object Function]';
  1131. }
  1132. function isNumeric( value ){
  1133. return /^\d+$/.test( value );
  1134. }
  1135.  
  1136. function parseBoolean( Boolean , Type ) {
  1137. // http://stackoverflow.com/a/24744599
  1138. Type = Type || false;
  1139. var falsely = /^(?:f(?:alse)?|no?|0+)$/i,
  1140. truely = /^(?:t(?:rue)?|yes?|1+)$/i;
  1141. return Type ? !truely.test( Boolean ) && !!Boolean : !falsely.test( Boolean ) && !!Boolean;
  1142. }
  1143. function parseBooleanStyle( str, nullOnFailure ){
  1144. nullOnFailure = nullOnFailure || false;
  1145. var bool, $this = str.toString().toLowerCase();
  1146. $this = parseInt( $this ) > 0 ? parseInt( $this ) : $this;
  1147. console.log('parseBooleanStyle',$this);
  1148. switch( $this ){
  1149. case 'true':
  1150. case '1':
  1151. case parseInt( $this ) > 0:
  1152. case 'on':
  1153. case 'yes':
  1154. bool = true;
  1155. break;
  1156. case 'false':
  1157. case '0':
  1158. case 'off':
  1159. case 'no':
  1160. bool = false;
  1161. break;
  1162. default:
  1163. bool = nullOnFailure ? null : false;
  1164. break;
  1165. }
  1166. return bool;
  1167. }
  1168. function getsComputedStyle( style, elem ) {
  1169. elem = elem || 'body';
  1170. return window.getComputedStyle( document[ elem ] )[ style ] !== undefined;
  1171. }
  1172. function isPropertySupported( property, elem ){
  1173. elem = elem || 'body';
  1174. return property in document[ elem ].style;
  1175. }
  1176. function cssPropertyValueSupported( prop, value, elem ) {
  1177. //cssPropertyValueSupported('width', '1px');
  1178. elem = elem || 'div';
  1179. var d = document.createElement( elem );
  1180. d.style[ prop ] = value;
  1181. return d.style[ prop ] === value;
  1182. }
  1183.  
  1184. var cssSupports = (function(){
  1185. // http://code.tutsplus.com/tutorials/quick-tip-detect-css3-support-in-browsers-with-javascript--net-16444
  1186. var div = document.createElement('div'),
  1187. vendors = 'Khtml Ms O Moz Webkit'.split(' '),
  1188. len = vendors.length;
  1189.  
  1190. return function(prop) {
  1191. if ( prop in div.style ) return true;
  1192.  
  1193. prop = prop.replace(/^[a-z]/, function(val) {
  1194. return val.toUpperCase();
  1195. });
  1196.  
  1197. while(len--) {
  1198. if ( vendors[len] + prop in div.style ) {
  1199. // browser supports box-shadow. Do what you need.
  1200. // Or use a bang (!) to test if the browser doesn't.
  1201. return true;
  1202. }
  1203. }
  1204. return false;
  1205. };
  1206. })();
  1207.  
  1208. function toggleClassState( config, Class, state, elem ){
  1209.  
  1210. config === undefined ? window.config = {} : config;
  1211. config = config || ( window.config = {} );
  1212. config[ Class ] = typeof state === 'string' ? !config[ Class ] : state;
  1213. $( elem || 'html' )[ config[ Class ] ? 'addClass' : 'removeClass' ]( Class );
  1214.  
  1215. }
  1216. function filterClick( e, $this ){
  1217. return e.which == 1 && e.target == $this;
  1218. }
  1219. function dispatchEventResize() {
  1220. //noinspection JSClosureCompilerSyntax,JSUnresolvedFunction
  1221. window.dispatchEvent(new Event('resize'));
  1222. }
  1223.  
  1224. /**
  1225. * @return {string}
  1226. */
  1227. function Undefined( check, replace ){
  1228. return check === undefined ? replace.toString() : check.toString();
  1229. }
  1230.  
  1231. function getGlobal(){
  1232. return (function(){
  1233. return this;
  1234. })();
  1235. }
  1236. function GM_lister( remove, item ){
  1237. var keys = GM_listValues();
  1238. for (var i = 0, key = null; key = keys[i]; i++) {
  1239. GM_listValues()[i] !== undefined && (
  1240. c.i('GM_ListItem: ' + GM_listValues()[i] + ':', GM_getValue(key)),
  1241. ( ( item !== undefined && GM_listValues()[i].inElem( item ) ) || item === undefined )
  1242. && ( remove === true || remove === 'yes' || remove === 1 ) && GM_deleteValue(key));
  1243. }
  1244. }
  1245.  
  1246. function roundFloat( num, dec ){
  1247. var d = 1;
  1248. for ( var i=0; i<dec; i++ ){
  1249. d += "0";
  1250. }
  1251. return Math.round(num * d) / d;
  1252. }
  1253. function randomFloatBetween( min, max, dec ){
  1254. dec = typeof( dec ) == 'undefined' ? 2 : dec;
  1255. return parseFloat( Math.min( min + ( Math.random() * ( max - min ) ), max ).toFixed( dec ) );
  1256. }
  1257. function random( max ) {
  1258. var min = 1,
  1259. rand = function(){
  1260. return Math.floor( Math.random() * ( max - min + 1 ) + min );
  1261. },
  1262. num1 = rand(),
  1263. num2 = rand();
  1264.  
  1265. return ( num1 > num2 ? num2/num1 : num1/num2 ) * max;
  1266. }
  1267. function roundNearPeace( number, peaces, dec ) {
  1268. return ( Math.round( number * peaces ) / peaces ).toFixed( dec );
  1269. }
  1270.  
  1271. var w = window,
  1272. glob = w,
  1273. $w = $( w ),
  1274. $l = $( location ),
  1275. locDoc = window.location.href,
  1276. d = document,
  1277. $d = $( d ),
  1278.  
  1279. c = {
  1280. defaultState: 3,
  1281. cute : function( type, msg, color ) {
  1282. color = color || "black";
  1283. var newColor, bgc = "White";
  1284. switch ( color ) {
  1285. case "success": newColor = "Green"; bgc = "LimeGreen"; break;
  1286. case "info": newColor = "DodgerBlue"; bgc = "Turquoise"; break;
  1287. case "error": newColor = "Red"; bgc = "Black"; break;
  1288. case "start": newColor = "OliveDrab"; bgc = "PaleGreen"; break;
  1289. case "warning": newColor = "Tomato"; bgc = "Black"; break;
  1290. case "end": newColor = "Orchid"; bgc = "MediumVioletRed"; break;
  1291. default: //noinspection SillyAssignmentJS
  1292. newColor = color;
  1293. }
  1294.  
  1295. typeof msg == "object" ?
  1296. window.console[ type ]( msg )
  1297. : typeof color == "object" ? (
  1298. window.console[ type ]("%c" + msg, "color: PowderBlue;font-weight:bold; background-color: RoyalBlue;"),
  1299. window.console[ type ]( newColor )
  1300. ) :
  1301. window.console[ type ]("%c" + msg, "color:" + newColor + "; background-color: " + bgc + ";");
  1302. },
  1303. show: function( showThis, type ){
  1304. var State = GM_getValue( type + 'StateValue' ) || this.defaultState;
  1305. return showThis !== 0 && State !== 0 && State === ( showThis || State ) || State === 'all';
  1306. },
  1307. pre: function( type, name, fn, line, color ){
  1308. line = line == undefined ? '' : line + ': ';
  1309. /**
  1310. * @return {string}
  1311. */
  1312. var Fn = function(){ return fn !== undefined ? fn : ''; };
  1313. typeof fn == "object"
  1314. ? window.console[ type ]( name, Fn() )
  1315. : c.cute( type, line + name + ': ' + Fn(), color );
  1316. },
  1317. type: function( type, name, fn, line, color, showThis ){
  1318. this.show( showThis, type ) && this.pre( type, name, fn, line, color );
  1319. },
  1320. l: function( name, fn, line, color, showThis ){
  1321. this.type( 'log', name, fn, line, color, showThis );
  1322. },
  1323. h: function( name, fn, line, color, showThis ){
  1324. this.type( 'handled', name, fn, line, color, showThis );
  1325. },
  1326. //c.l('name', 'fn'=='fn', 'line', 'blue')
  1327. i: function( name, fn, line, color, showThis ){
  1328. this.type( 'info', name, fn, line, color, showThis );
  1329. },
  1330. d: function( name, fn, line, color, showThis ){
  1331. this.type( 'debug', name, fn, line, color, showThis );
  1332. }
  1333. },
  1334. localStorageObj = {
  1335. name : 'setName', //important
  1336. localArray : function(){
  1337. return window.localStorage.getItem( this.name );
  1338. },
  1339. getArray : function(){
  1340. return this.localArray() === null ? [] : JSON.parse( this.localArray() );
  1341. },
  1342. stringify : function( array ){
  1343. c.i('[stringify]', JSON.stringify(array) );
  1344. window.localStorage.setItem( this.name, JSON.stringify(array) );
  1345. },
  1346. check : function( key, value ){
  1347. var array = this.getArray();
  1348. return array.grepArrayObj('find', key, value );
  1349. },
  1350. viewConsole : function( json ){
  1351. var array = this.getArray();
  1352. // $.toJSON() --- https://github.com/Krinkle/jquery-json/blob/master/src/jquery.json.js
  1353. // array.join('\n');
  1354. c.d( this.name, array ); //debug mode important to make this work
  1355. c.i( this.name, json ? isFunction( $.toJSON() ) ? $.toJSON( array ) : JSON.stringify( array ) : array.toSource() );
  1356. },
  1357. addTo : function() {
  1358. },
  1359. add : function( key, value, obj ){
  1360. var array = this.getArray(),
  1361. inA = array;
  1362. c.i('[check array 1]', inA );
  1363. this.check( array, key, value )
  1364. || (
  1365. array.push( obj ),
  1366. c.i('[Added Object into array]', obj ),
  1367. c.i('[check array 2]', array )
  1368. );
  1369. this.stringify( array );
  1370. },
  1371. remove : function( key, value, obj ){
  1372. var array = this.getArray();
  1373. this.check( array, key, value )
  1374. && (
  1375. array = array.grepArrayObj('remove', key, value ),
  1376. c.i('[Removed Object from array]', obj )
  1377. );
  1378. this.stringify( array );
  1379. }
  1380. },
  1381. booleanTimer = {
  1382. timers : [],
  1383. start : function( name, ms ){
  1384. var that = this, value = name,
  1385. stop = setTimeout(function(){
  1386. that.stop( value );
  1387. }, ms );
  1388. this.timers.push( { 'name': value, stop:stop } );
  1389. //setTimeout(function(){that.stop( value );}, ms );
  1390. },
  1391. check : function( value ){
  1392. return this.timers.grepArrayObj( 'find', 'name', value ) !== undefined;
  1393. },
  1394. stop : function( value ){
  1395. this.timers = this.timers.grepArrayObj( 'remove', 'name', value );
  1396. }
  1397. },
  1398. advTimer = {
  1399. timers : [],
  1400. start : function( name, ms ){
  1401. var that = this, value = name,
  1402. stop = setTimeout(function(){
  1403. that.stop( value );
  1404. }, ms );
  1405. //noinspection JSUnresolvedVariable
  1406. this.timers.push({ 'name':value, start:window.performance.now(), timeout:ms, stop:stop });
  1407. },
  1408. check : function( value ){
  1409. var findObj = this.timers.grepArrayObj( 'find', 'name', value );
  1410. //noinspection JSUnresolvedVariable
  1411. return findObj !== undefined
  1412. ? findObj.timeout - ( window.performance.now() - findObj.start )
  1413. : false;
  1414. },
  1415. stop : function( value ){
  1416. this.timers = this.timers.grepArrayObj( 'remove', 'name', value );
  1417. }
  1418. },
  1419. lap = {
  1420. data : {},
  1421. tid : function(){
  1422. //noinspection JSUnresolvedVariable
  1423. return performance.now();
  1424. },
  1425. set : function(name){
  1426. this.data[name] = this.tid();
  1427. },
  1428. get : function(name){
  1429. return this.tid() - this.data[name];
  1430. },
  1431. end : function(name){
  1432. this.print(name);
  1433. this.del(name);
  1434. },
  1435. del : function(name){
  1436. delete this.data[name];
  1437. },
  1438. print: function(name){
  1439. var get = this.get( name );
  1440. c.i( 'Lap: ' + name, isNaN( get ) ? 'There is no such a name ' : get + 'ms' );
  1441. }
  1442. },
  1443. timer = {
  1444. ms : 0,
  1445. set : function(ms){
  1446. var that = this;
  1447. this.ms = ms;
  1448. setTimeout(function(){
  1449. that.ms = 0;
  1450. }, ms );
  1451. }
  1452. },
  1453. counter = {
  1454. data : {},
  1455. set : function(name, start, base){
  1456. this.data[name] = start || 0;
  1457. this.data[name+'Default'] = base || 1;
  1458. },
  1459. get : function(name){
  1460. return this.data[name];
  1461. },
  1462. is : function(name, count){
  1463. return this.data[name] === count;
  1464. },
  1465. lessThen: function(name, count, equal ){
  1466. return ( equal || false ) ? this.data[name] <= count : this.data[name] < count;
  1467. },
  1468. moreThen: function(name, count, equal){
  1469. return ( equal || false ) ? this.data[name] >= count : this.data[name] > count;
  1470. },
  1471. del : function(name){
  1472. delete this.data[name];
  1473. },
  1474. exists : function(name) {
  1475. return this.data[name] !== undefined;
  1476. },
  1477. plus : function(name, num){
  1478. this.exists(name) || this.set(name);
  1479. this.data[name] = this.data[name] + ( num === undefined ? this.data[name+'Default'] : num );
  1480. },
  1481. minus : function(name, num){
  1482. this.exists(name) || this.set(name);
  1483. this.data[name] = this.data[name] - ( num === undefined ? this.data[name+'Default'] : num );
  1484. },
  1485. increase: function (name, num) {
  1486. this.plus(name, num);
  1487. },
  1488. decrease: function (name, num) {
  1489. this.minus(name, num);
  1490. },
  1491. '+' : function (name, num) {
  1492. this.plus(name, num);
  1493. },
  1494. '-' : function (name, num) {
  1495. this.minus(name, num);
  1496. },
  1497. up : function (name, num) {
  1498. this.plus(name, num);
  1499. },
  1500. down : function (name, num) {
  1501. this.minus(name, num);
  1502. },
  1503. add : function (name, num) {
  1504. this.plus(name, num);
  1505. },
  1506. subtract: function (name, num) {
  1507. this.minus(name, num);
  1508. },
  1509. withdraw: function (name, num) {
  1510. this.minus(name, num);
  1511. }
  1512. },
  1513. g = {
  1514. locDoc : window.location.href,
  1515. ms : 0,
  1516. timer : function(ms){
  1517. g.ms = ms;
  1518. setTimeout(function(){ g.ms = 0; }, ms );
  1519. },
  1520.  
  1521. GM : {
  1522. engine : function( mode, val, range ){
  1523. switch (mode){
  1524. case 'set': GM_setValue( val.name, val.default );
  1525. break;
  1526. case 'get': range ? config[ val.name ] = GM_getValue( val.name ):
  1527. ui.config[ val.name ] = GM_getValue( val.name );
  1528. break;
  1529. case 'del': GM_deleteValue( val.name );
  1530. }
  1531. },
  1532. manager : function( mode, array, range ){
  1533. $.each( array, function( i, val ){ this.engine( mode, val, range === undefined ); });
  1534. mode === 'del' && ( GM_deleteValue( 'firstRun' ), GM_deleteValue( 'yourVer' ) );
  1535. }
  1536. }
  1537. },
  1538.  
  1539. testPerformance = function( name, fn, testCycles ) {
  1540. lap.set( name );
  1541. var i = 0;
  1542. for ( i ; i < testCycles; i++ ){
  1543. fn();
  1544. }
  1545. lap.end( name );
  1546. },
  1547. perf = function (testName, fn) {
  1548. var startTime = new Date().getTime();
  1549. fn();
  1550. var endTime = new Date().getTime();
  1551. console.log(testName + ": " + (endTime - startTime) + "ms");
  1552. };
  1553.  
  1554. $(document).on('click','*',function(e){
  1555. this == e.target && c.i('target:', $(this)[0]
  1556. ); });
  1557.  
  1558. c.i('my Function Library ö');
  1559. /*
  1560. isScrolledIntoView = (elem) ->
  1561. docViewTop = $(window).scrollTop()
  1562. docViewBottom = docViewTop + $(window).height()
  1563. elemTop = $(elem).offset().top
  1564. elemBottom = elemTop + $(elem).height()
  1565.  
  1566. (elemBottom - 200 < docViewBottom) and (elemBottom + $(elem).height() > docViewBottom )
  1567. */
  1568.