github.com/aarzilli/tools@v0.0.0-20151123112009-0d27094f75e0/net/http/proxy1/tamper-monkey-popup-menu-2-dyn.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.122
     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 domainY = location.host;
    57      var protocolY = location.protocol;
    58  
    59      var href = parAnchor.attr('href');
    60      if (href.indexOf('/') === 0) {
    61          href = domainY + 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 CreateAnchorWrapper(obj){
   139      if (!obj.parent().hasClass("hc-a")){
   140          obj.wrap('<div class="hc-a" />');
   141          obj.addClass("hc-b1");
   142          var specialPop = '<div class="hc-b2" >' + 'specialpop' + '</div>'; // Potentially a lot of DOM insertions over time
   143          obj.after(specialPop);  //Append after selected element
   144          
   145          var w = obj.width();
   146          if (w < 320) {
   147              w = 320;
   148          }
   149          obj.siblings(".hc-b2").eq(0).css({ 'width': w +'px' });
   150          return false;
   151      }
   152      return true;
   153  }
   154  
   155  function CreateAnchorWrappers(){
   156  
   157      var start = new Date().getTime();
   158  
   159      // Create parent wrappers
   160      // $( '.hovering-popup' ).each(function( index ) {
   161      $( 'a' ).each(function( index ) {
   162          // console.log( index + ": " + $( this ).text() );
   163          var obj = $(this);
   164  		CreateAnchorWrapper(obj);        
   165      });
   166  
   167      // Remember when we finished
   168      var end = new Date().getTime();
   169      console.log("creating wrappers took ",end - start);
   170  }
   171  
   172  
   173  function HidePop(obj){
   174      // var popup = $('.hc-b2');
   175      // popup.hide();
   176      obj.stop(true, true).fadeOut(300, function () {
   177          console.log("fadeout-callback");
   178      });
   179  }
   180  
   181  // A wrapping of a-tags is required,
   182  // to bind the a-tag and the popup 
   183  // into one single mouseenter-mouseleave-context.
   184  // 
   185  // The wrapping makes positioning of the popup relative.
   186  // At left:0, top:0; the popup overlays the original anchor-tag.
   187  // 
   188  // The wrapping must be done in advance, causing a lot of DOM overhead.
   189  // Approx. 3 ms per link.
   190  // Dynamic wrapping could be done like this:
   191  // Reacting to a-tag-mouseenter, 
   192  // creating wrapper,
   193  // calling parent.triggerHandler("mouseenter"),
   194  // calling it only once.
   195  // 
   196  // Horizonal positioning is centered.
   197  // We could also make it align right-or-left   - with left: auto; right:-10px;
   198  // 
   199  // hovercard.js made the anchor overlay the popup with
   200  //          wrapper.css("zIndex", "200");
   201  //          anchor.css("zIndex", "100");
   202  //          popup.css("zIndex", "50");
   203  // And resetting the all to zero on mouseleave.
   204  // This would only look good with align right-or-left.
   205  function ShowPop(obj){
   206      
   207      // CreateAnchorWrapper(obj)    // now dynamically created
   208  
   209      // var popup = $('.hc-b2');
   210      var popup = obj.siblings(".hc-b2").eq(0);
   211      if (popup.length<=0){
   212          popup = obj.find(".hc-b2").eq(0);
   213      }
   214  
   215      var objAbsTL = obj.offset();
   216      var objRelTL = obj[0].getBoundingClientRect();
   217  
   218      var new_left =  - (popup.width() / 2) + (obj.width() / 2) - hPadSimple;
   219      
   220      // prevent transgression to the left
   221      if (objRelTL.left + new_left < winBrdrLeft){
   222          new_left = -objRelTL.left + winBrdrLeft;
   223      }
   224      // prevent transgression to the right
   225      if (objRelTL.left + new_left + popup.width() + 2*hPadSimple > $(window).width() - winBrdrRight){
   226          new_left = -popup.width() - 2*hPadSimple - ( objRelTL.left - $(window).width() )  - winBrdrRight;
   227      }
   228  
   229  
   230  
   231      if (objRelTL.top < $(window).height() / 2) {
   232          popup.css({
   233              'padding-top':    '20px',
   234              'padding-bottom': '10px',
   235              left: new_left + 'px',
   236              top: obj.height() +  1*vDist + 'px',
   237              'background-position': 'center top',
   238              //'background-image': "url(arrow-desc.png)",
   239          });
   240      } else {
   241          console.log("ort>wh", objRelTL.top, $(window).height() / 2);
   242          popup.css({
   243              'padding-top':    '10px',
   244              'padding-bottom': '20px',
   245              left: new_left + 'px',
   246              top: - popup.height() - vPadOffs - 1*vDist + 'px',
   247              'background-position': 'center bottom',
   248              //'background-image':  "url(arrow-asc.png)",
   249          });
   250      }
   251  
   252      var anch = obj.siblings(".hc-b1").eq(0);
   253      if (anch.length<=0){
   254          anch = obj.find(".hc-b1").eq(0);
   255      }
   256      var html = PopupContent(anch);
   257      popup.html(html);
   258  
   259      // popup.show();
   260      popup.stop(true, true).delay(200).fadeIn(500);
   261  
   262  }
   263  
   264  
   265  $( document ).ready(function() {
   266  
   267      var wh = $( window ).height();            // Returns height of browser viewport
   268      var dh = $( document ).height();          // Returns height of HTML document
   269      var scrollVert = $('body').scrollTop();   // How many pixel is document scrolled in the viewport
   270      var obj = $('body').find('*').last();
   271      var os1 = obj.offset();                   // Our object's position relative to document root
   272      var os2 = obj[0].getBoundingClientRect(); // Our object's position relative to document root - MINUS scroll pos
   273      console.log("height window - doc:",wh,dh," sctp:", scrollVert , " offs1",os1.top, " offs2", os2.top);
   274      // console.log("offs1-l", os1.left, " offs2-l",  os2.left);
   275  
   276  
   277      var domainX = document.domain;
   278      var domainY = location.host;
   279      var protocolY = location.protocol;
   280      console.log("domainXY", domainX, domainY,protocolY);
   281  
   282  
   283  
   284  	AddCSS();
   285  
   286      // CreateAnchorWrappers(); // now dynamically created
   287  
   288      // Ad-hoc creation and handing off event
   289      $( 'a' ).on( "mouseenter", function(evt) {
   290          var obj = $(evt.target);
   291  		var alreadyExists = CreateAnchorWrapper(obj);
   292  	    if (!alreadyExists) {
   293  	    	obj.closest(".hc-a").triggerHandler("mouseenter"); // handing off event at first time
   294  			evt.preventDefault();
   295  	    }
   296      });
   297  
   298  
   299      // mouseover fires again for child elements.
   300      // it prevents click events 
   301      $(document).on( "mouseenter", '.hc-a' , function() {  // catches dynamially created nodes
   302      // $( '.hc-a' ).on( "mouseenter", function(e) {       // only so far created nodes
   303          var obj = $(this);
   304          ShowPop(obj);
   305      });
   306      $(document).on( "mouseleave", '.hc-a' , function() {
   307      // $( '.hc-a' ).on( "mouseleave", function() {
   308          var obj = $(this).find(".hc-b2").eq(0);
   309          HidePop(obj);
   310      });
   311  
   312  
   313  
   314      $( '.hc-a' ).on( "focusin", function() {
   315          // var obj = $(this);
   316          // ShowPop(obj);
   317      });
   318  
   319      $( '.hc-a' ).on( "focusout", function() {
   320          // var obj = $(this).find(".hc-b2").eq(0);
   321          // HidePop(obj)
   322      });
   323  
   324      console.log( "document ready completed" );
   325  });
   326  
   327  
   328          
   329  
   330          console.log("hover popups handler added");
   331          //isolated jQuery end;
   332      });
   333  })(window.jQuery.noConflict(true));
   334  
   335