github.com/aarzilli/tools@v0.0.0-20151123112009-0d27094f75e0/net/http/proxy1/tamper-monkey-popup-menu-1-static.js (about)

     1  // ==UserScript==
     2  // @name         popup-menu
     3  // @description  include jQuery and make sure window.$ is the content page's jQuery version, and this.$ is our jQuery version. 
     4  // @description  http://stackoverflow.com/questions/28264871/require-jquery-to-a-safe-variable-in-tampermonkey-script-and-console
     5  // @namespace    http://your.homepage/
     6  // @version      0.12
     7  // @author       iche
     8  // @downloadURL  http://localhost:8085/mnt01/tamper-monkey-popup-menu.js
     9  // @updateURL    http://localhost:8085/mnt01/tamper-monkey-popup-menu.js //serving the head with possibly new version
    10  // // https://developer.chrome.com/extensions/match_patterns
    11  // @match        *://www.welt.de/*
    12  // // @match        *://www.handelsblatt.com/*  // irritates the image gallery progress arrows
    13  // @match        *://www.focus.de/*
    14  // // @include     /^https?:\/\/www.flickr.com\/.*/
    15  // // @require      http://cdn.jsdelivr.net/jquery/2.1.3/jquery.min.js
    16  // @require      https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js
    17  // @grant        none
    18  // @noframes
    19  // @run-at      document-end
    20  // ==/UserScript==
    21  
    22  
    23  // fallback http://encosia.com/3-reasons-why-you-should-let-google-host-jquery-for-you/
    24  if (typeof jQuery === 'undefined') {
    25      console.log("CDN blocked by Iran or China?");
    26      document.write(unescape('%3Cscript%20src%3D%22/path/to/your/scripts/jquery-2.1.4.min.js%22%3E%3C/script%3E'));
    27  }
    28  
    29  (function ($, undefined) {
    30      $(function () {
    31  
    32          //isolated jQuery start;
    33          console.log("about to add hover popups; " + $.fn.jquery + " Version");
    34  
    35  
    36  var vPadOffs = 30; // padding offset; top & bottom are 10; one is 20
    37      vPadOffs += 2; // border with
    38  var hPadSimple = 10; // simple horizontal padding offset
    39  
    40  var winBrdrLeft = 4;                     // border to outer browser window
    41  var winBrdrRight = winBrdrLeft+2+2;        // popup border 2*1, shadow
    42  
    43  var vDist = 0;  // vertical distance to popup - cannot be set - any gap causes mouseleave event
    44  
    45  
    46  function PopupContent(obj){
    47  
    48            
    49      var parAnchor = obj.closest("a");  // includes self
    50  
    51      
    52      if (parAnchor.length<=0) {
    53      	return "";
    54      }
    55  
    56      var domainX = document.domain;
    57      console.log("domainX", domainX);
    58  
    59      var href = obj.attr('href');
    60      if (href.indexOf('/') === 0) {
    61          href = domainX + href;
    62      }
    63  
    64      var text = obj.text();
    65      if (text.length > 100){
    66          var textSh = "";
    67          textSh  = text.substr(0,50); // start, end
    68          textSh += " ... ";
    69          textSh += text.substr(text.length-50,text.length-1);
    70          text = textSh;
    71      }
    72      
    73      var formHtml = "";
    74      var prox01 = "http://localhost:8085/dedup";
    75      var prox02 = "https://libertarian-islands.appspot.com/dedup";
    76  
    77      formHtml += "<form action='"+prox01+"' method='post' ";
    78      formHtml += "    target='proxy-window' >";
    79      formHtml += "<input type='hidden'  name='url-x' value='"+href+"' >";
    80      formHtml += "<input type='submit'               value='subm'     >";
    81      formHtml += "</form>";
    82  
    83      var html = "";
    84      html += "<a target='proxy-window'  href='"+prox01 + "?url-x="+ href+"' >" + text + "</a>";
    85      html += formHtml;
    86  
    87      return html;
    88  
    89  }
    90  
    91  
    92  function AddCSS(){
    93  
    94  	if ($('#css-hover-popup').length <= 0) {
    95  
    96  		var s =  '';
    97  	    s += '<style type="text/css"  id="css-hover-popup" >';
    98  	    s += '.hc-a{';
    99  	    s += '    position: relative; ';
   100  	    s += '    display:inline; ';
   101  	    s += '}';
   102  	    s += '.hc-b1{}';
   103  	    s += '.hc-b2{';
   104  	    s += '    position:absolute;';
   105  	    s += '    z-index: 10;';
   106  	    s += '    left:-10px;   top:-10px;';
   107  	    s += '    /* fixed height required for positioning computations */';
   108  	    s += '    width:150px;  height:36px;';
   109  	    s += '    text-align:left; ';
   110  	    s += '    font-family:Sans-serif !important; font-size:12px !important; ';
   111  	    s += '    color:#666 !important; line-height:1.5em;';
   112  	    s += '';
   113  	    s += '    background:url("arrow-desc.png") no-repeat scroll center 0 transparent;';
   114  	    s += '    background-image: none;';
   115  	    s += '    background-color: #ddd !important;  ';
   116  	    s += '    border:solid 1px #ddd; ';
   117  	    s += '    border-radius:4px;';
   118  	    s += '    box-shadow:3px 3px 3px #888;';
   119  	    s += '    /* skipping the old stuff';
   120  	    s += '      -moz-border-radius:3px;-webkit-border-radius:3px;';
   121  	    s += '      -moz-box-shadow:5px 5px 5px #888;-webkit-box-shadow:5px 5px 5px #888;  */';
   122  	    s += '    margin: 0px !important;' ;
   123  	    s += '    padding:  10px; ';
   124  	    s += '    padding-left:  10px !important; ';
   125  	    s += '    padding-right: 10px !important;' ;
   126  	    s += '    /* alternating upon top-bottom, adapt vPadOffs accordingly */ ';
   127  	    s += '    aapadding-top: 20px;';
   128  	    s += '    display:none;';
   129  	    s += '}';
   130  	    s += '</style>';
   131  
   132  	    $(s).appendTo('head');
   133  
   134  	}
   135  
   136  }
   137  
   138  function CreateAnchorWrappers(){
   139  
   140      var start = new Date().getTime();
   141  
   142      // Create parent wrappers
   143      // $( '.hovering-popup' ).each(function( index ) {
   144      $( 'a' ).each(function( index ) {
   145          // console.log( index + ": " + $( this ).text() );
   146          var obj = $(this);
   147          if (!obj.parent().hasClass("hc-a")){
   148              obj.wrap('<div class="hc-a" />');
   149              obj.addClass("hc-b1");
   150              var specialPop = '<div class="hc-b2" >' + 'specialpop' + '</div>'; // Potentially a lot of DOM insertions over time
   151              obj.after(specialPop);  //Append after selected element
   152              
   153              var w = obj.width();
   154              if (w < 320) {
   155                  w = 320;
   156              }
   157              obj.siblings(".hc-b2").eq(0).css({ 'width': w +'px' });
   158          }
   159      });
   160  
   161      // Remember when we finished
   162      var end = new Date().getTime();
   163      console.log("creating wrappers took ",end - start);
   164  }
   165  
   166  
   167  function HidePop(obj){
   168      // var popup = $('.hc-b2');
   169      // popup.hide();
   170      obj.stop(true, true).fadeOut(300, function () {
   171          console.log("fadeout-callback");
   172      });
   173  }
   174  
   175  // A wrapping of a-tags is required,
   176  // to bind the a-tag and the popup 
   177  // into one single mouseover-mouseleave-context.
   178  // 
   179  // The wrapping makes positioning of the popup relative.
   180  // At left:0, top:0; the popup overlays the original anchor-tag.
   181  // 
   182  // The wrapping must be done in advance, causing a lot of DOM overhead.
   183  // Approx. 3 ms per link.
   184  // Dynamic wrapping could be done like this:
   185  // Reacting to a-tag-mouseover, 
   186  // creating wrapper,
   187  // calling parent.triggerHandler("mouseover"),
   188  // calling it only once.
   189  // 
   190  // Horizonal positioning is centered.
   191  // We could also make it align right-or-left   - with left: auto; right:-10px;
   192  // 
   193  // hovercard.js made the anchor overlay the popup with
   194  //          wrapper.css("zIndex", "200");
   195  //          anchor.css("zIndex", "100");
   196  //          popup.css("zIndex", "50");
   197  // And resetting the all to zero on mouseleave.
   198  // This would only look good with align right-or-left.
   199  function ShowPop(obj){
   200      
   201      // CreateAnchorWrappers()    // must be done in advance
   202  
   203      // var popup = $('.hc-b2');
   204      var popup = obj.siblings(".hc-b2").eq(0);
   205      if (popup.length<=0){
   206          popup = obj.find(".hc-b2").eq(0);
   207      }
   208  
   209      var objAbsTL = obj.offset();
   210      var objRelTL = obj[0].getBoundingClientRect();
   211  
   212      var new_left =  - (popup.width() / 2) + (obj.width() / 2) - hPadSimple;
   213      
   214      // prevent transgression to the left
   215      if (objRelTL.left + new_left < winBrdrLeft){
   216          new_left = -objRelTL.left + winBrdrLeft;
   217      }
   218      // prevent transgression to the right
   219      if (objRelTL.left + new_left + popup.width() + 2*hPadSimple > $(window).width() - winBrdrRight){
   220          new_left = -popup.width() - 2*hPadSimple - ( objRelTL.left - $(window).width() )  - winBrdrRight;
   221      }
   222  
   223  
   224  
   225      if (objRelTL.top < $(window).height() / 2) {
   226          popup.css({
   227              'padding-top':    '20px',
   228              'padding-bottom': '10px',
   229              left: new_left + 'px',
   230              top: obj.height() +  1*vDist + 'px',
   231              'background-position': 'center top',
   232              //'background-image': "url(arrow-desc.png)",
   233          });
   234      } else {
   235          console.log("ort>wh", objRelTL.top, $(window).height() / 2);
   236          popup.css({
   237              'padding-top':    '10px',
   238              'padding-bottom': '20px',
   239              left: new_left + 'px',
   240              top: - popup.height() - vPadOffs - 1*vDist + 'px',
   241              'background-position': 'center bottom',
   242              //'background-image':  "url(arrow-asc.png)",
   243          });
   244      }
   245  
   246      var anch = obj.siblings(".hc-b1").eq(0);
   247      if (anch.length<=0){
   248          anch = obj.find(".hc-b1").eq(0);
   249      }
   250      var html = PopupContent(anch);
   251      popup.html(html);
   252  
   253      // popup.show();
   254      popup.stop(true, true).delay(200).fadeIn(500);
   255  
   256  }
   257  
   258  
   259  $( document ).ready(function() {
   260  
   261      var wh = $( window ).height();            // Returns height of browser viewport
   262      var dh = $( document ).height();          // Returns height of HTML document
   263      var scrollVert = $('body').scrollTop();   // How many pixel is document scrolled in the viewport
   264      var obj = $('body').find('*').last();
   265      var os1 = obj.offset();                   // Our object's position relative to document root
   266      var os2 = obj[0].getBoundingClientRect(); // Our object's position relative to document root - MINUS scroll pos
   267      console.log("height window - doc:",wh,dh," sctp:", scrollVert , " offs1",os1.top, " offs2", os2.top);
   268      // console.log("offs1-l", os1.left, " offs2-l",  os2.left);
   269  
   270  
   271  	AddCSS();
   272  
   273      CreateAnchorWrappers();
   274  
   275  
   276      // Here popup would not be clickable.
   277      // It disappears as soon as cursor leaves obj.
   278      $( 'a' ).on( "mouseover", function() {
   279      });
   280  
   281  
   282      $(document).on( "click", "a" , function(e) {
   283      	// console.log("click1");
   284          // e.preventDefault();
   285      });
   286  
   287      // We have to latch the events onto the parent wrapper node.
   288      // Thus all parent nodes need to be created on documentload :(
   289  
   290      // mouseover fires again for child elements.
   291      // it prevents click events 
   292      // $( '.hc-a' ).on( "mouseover", function(e) {
   293  
   294      $( '.hc-a' ).on( "mouseenter", function(e) {
   295          var obj = $(this);
   296          ShowPop(obj);
   297      });
   298      $( '.hc-a' ).on( "mouseleave", function() {
   299          var obj = $(this).find(".hc-b2").eq(0);
   300          HidePop(obj);
   301      });
   302  
   303  
   304      $( '.hc-a' ).on( "focusin", function() {
   305          // var obj = $(this);
   306          // ShowPop(obj);
   307      });
   308  
   309      $( '.hc-a' ).on( "focusout", function() {
   310          // var obj = $(this).find(".hc-b2").eq(0);
   311          // HidePop(obj)
   312      });
   313  
   314      console.log( "document ready completed" );
   315  });
   316  
   317  
   318          
   319  
   320          console.log("hover popups handler added");
   321          //isolated jQuery end;
   322      });
   323  })(window.jQuery.noConflict(true));
   324  
   325