github.com/aarzilli/tools@v0.0.0-20151123112009-0d27094f75e0/net/http/proxy1/tamper-monkey-popup-menu-3.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  https://raw.githubusercontent.com/pbberlin/tools/master/net/http/proxy1/tamper-monkey-popup-menu-3.js
     9  // @updateURL    https://raw.githubusercontent.com/pbberlin/tools/master/net/http/proxy1/tamper-monkey-popup-menu-3.js  //serving the head with possibly new version
    10  // // https://developer.chrome.com/extensions/match_patterns
    11  // @match        *://*.welt.de/*
    12  // @match        *://*.handelsblatt.com/*  
    13  // @match        *://*.focus.de/*
    14  // @match        *://*.economist.com/*
    15  // // @include     /^https?:\/\/www.flickr.com\/.*/
    16  // // @require      http://cdn.jsdelivr.net/jquery/2.1.3/jquery.min.js
    17  // @require      https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js
    18  // @grant        none
    19  // @noframes
    20  // @run-at      document-end
    21  // ==/UserScript==
    22  
    23  
    24  // fallback http://encosia.com/3-reasons-why-you-should-let-google-host-jquery-for-you/
    25  if (typeof jQuery === 'undefined') {
    26      console.log("CDN blocked by Iran or China?");
    27      document.write(unescape('%3Cscript%20src%3D%22/path/to/your/scripts/jquery-2.1.4.min.js%22%3E%3C/script%3E'));
    28  }
    29  
    30  (function ($, undefined) {
    31      $(function () {
    32  
    33          //isolated jQuery start;
    34          console.log("about to add hover popups; " + $.fn.jquery + " Version");
    35  
    36  
    37  var hOffs  = 10 ;  // horizontal offset
    38  
    39  var vPaddingOffs = 5 ; // invisible padded ridge
    40  var vShadowOffs  = 6 ; // 
    41  
    42  
    43  
    44  function PopupContent(obj){
    45  
    46            
    47      var parAnchor = obj.closest("a");  // includes self
    48  
    49      
    50      if (parAnchor.length<=0) {
    51          return "";
    52      }
    53  
    54      var domainY = location.host;
    55      var protocolY = location.protocol;
    56  
    57      var href = parAnchor.attr('href');
    58      if (href.indexOf('/') === 0) {
    59          href = domainY + href;
    60      }
    61  
    62      var text = obj.text();
    63      if (text.length > 100){
    64          var textSh = "";
    65          textSh  = text.substr(0,50); // start, end
    66          textSh += " ... ";
    67          textSh += text.substr(text.length-50,text.length-1);
    68          text = textSh;
    69      }
    70      
    71      var formHtml = "";
    72      var prox01 = "http://localhost:8085/dedup";
    73      var prox02 = "https://libertarian-islands.appspot.com/dedup";
    74      var proxURL = prox02;
    75      
    76      formHtml += "<form action='"+proxURL+"' method='post' ";
    77      formHtml += "    target='proxy-window' >";
    78      formHtml += "<input type='hidden'  name='prot'  value='"+protocolY+"' >";
    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+"&prot="+protocolY+"' >" + "localhost" + "</a><br>";
    85      html += "<a target='proxy-window'  href='"+proxURL + "?url-x="+ href+"&prot="+protocolY+"' >" + text + "</a>";
    86      html += formHtml;
    87  
    88      return html;
    89  
    90  }
    91  
    92  
    93  function AddCSS(){
    94  
    95      if ($('#css-hover-popup').length <= 0) {
    96  
    97          var s =  '';
    98          s += '<style type="text/css"  id="css-hover-popup" >';
    99          s += '';
   100          s += '.hc-b2{';
   101          s += '    position:absolute;';
   102          s += '    z-index: 10;';
   103          s += '    left:-10px;   top:-10px;';
   104          s += '    width:  240px;  ';
   105          s += '    /* dont fix the height - use jQuery.outerHeight() for computations */ ';
   106          s += '    height: auto ;';
   107          s += '';
   108          // s += '    border-left: solid 2px #d22; ';
   109          // s += '    border-right:solid 2px #d22; ';
   110          // s += '    border: 1px solid #d22; ';
   111          s += '    margin:         0px !important; ' ;
   112          s += '    /* alternating upon top-bottom, adapt vPaddingOffs accordingly */ ';
   113          s += '    padding:        0px; ';
   114          s += '    padding-left:   0px !important; ';
   115          s += '    padding-right:  0px !important;' ;
   116          s += '';
   117          s += '    display:none;';
   118          s += '}';
   119          s += '';
   120          s += '.hc-b3{';
   121          s += '    text-align:left; ';
   122          s += '    font-family:Sans-serif !important; font-size:12px !important; ';
   123          s += '    color:#666 !important; line-height:1.5em;';
   124          s += '    background:url("arrow-desc.png") no-repeat scroll center 0 transparent;';
   125          s += '    background-image: none;';
   126          s += '    background-color: #ddd !important;  ';
   127          s += '    border:solid 1px #ddd; ';
   128          s += '    border-radius:4px;';
   129          s += '    box-shadow:3px 3px 3px #888;';
   130          s += '    /* skipping the old stuff';
   131          s += '      -moz-border-radius:3px;-webkit-border-radius:3px;';
   132          s += '      -moz-box-shadow:5px 5px 5px #888;-webkit-box-shadow:5px 5px 5px #888;  */';
   133          s += '    margin:         0px !important; ' ;
   134          s += '    padding:        5px 8px !important;  ';
   135          s += '}';
   136          s += '';
   137  
   138  
   139          s += '</style>';
   140  
   141          $(s).appendTo('head');
   142  
   143  
   144          var popupUpScaffold = '<div id="popup1" class="hc-b2" ><div id="pop1inner" class="hc-b3" >content popup1</div></div>'; 
   145          $('body').after(popupUpScaffold);  //Append after selected element
   146  
   147      }
   148  
   149  }
   150  
   151  
   152  
   153  
   154  
   155  function HidePop(obj){
   156      var popup = $('#popup1');
   157      // popup.hide();
   158      popup.stop(true, true).fadeOut(300, function () {
   159          console.log("fadeout-callback");
   160      });
   161  }
   162  
   163  // 
   164  // hovercard.js made the anchor overlay the popup with
   165  //          wrapper.css("zIndex", "200");
   166  //          anchor.css("zIndex", "100");
   167  //          popup.css("zIndex", "50");
   168  // And resetting the all to zero on mouseleave.
   169  // 
   170  // But this does not work, when both are children of different z-index-layers.
   171  // Hovercard binds popup and anchor together.
   172  // But this destroys parent-child links for instance in image gallery plugins.
   173  // It also prevents popup from floating above all layers,
   174  // since anchor might be locked into a lower layer.
   175  // 
   176  // We therefore take the global approach.
   177  // There remains the problem of syncing mouseenter-mouseleave
   178  // but we solved that by mediating timer.
   179  function ShowPop(obj){
   180      
   181  
   182      var popup = $('#popup1');
   183      var inner = $('#pop1inner');
   184  
   185      var html = PopupContent(obj);
   186      inner.html(html); // setting content => force sizing
   187  
   188  
   189      if ( obj.outerWidth() <= 240 ) {
   190          popup.css({
   191              width: 240 + 'px',
   192          });
   193      } else if ( obj.outerWidth() > 480 ) {
   194          popup.css({
   195              width: 480 + 'px',
   196          });
   197      } else {
   198          popup.css({
   199              width: obj.outerWidth() + 2*hOffs + 'px',
   200          });
   201      }
   202  
   203  
   204  
   205      var objAbsTL = obj.offset();
   206  
   207      var scrollTop  = $('body').scrollTop();   // How many pixel is document scrolled in the viewport
   208      var scrollLeft = $('body').scrollLeft();   
   209  
   210      var viewportTop  = objAbsTL.top  - scrollTop;
   211      var viewportLeft = objAbsTL.left + obj.outerWidth() - scrollLeft;
   212  
   213      var vert = "lower";
   214      if (viewportTop < $(window).height() / 2) {
   215          vert = "upper";
   216      }
   217      var horz = "right";
   218      if (viewportLeft < $(window).width() * 0.75) {
   219          horz = "left";
   220      }
   221  
   222      console.log("quadrant", vert, horz);
   223  
   224  
   225      if ( vert == "upper" ) {
   226          // show below
   227          popup.css({
   228              'padding-top':    vPaddingOffs + 'px',
   229              'padding-bottom': '0px',
   230              'background-position': 'center top',
   231          });
   232          popup.css({
   233              left: objAbsTL.left - hOffs + 'px',
   234              top:  objAbsTL.top   +  obj.outerHeight() + 'px',
   235          });
   236  
   237      } else {
   238          // show above
   239          popup.css({
   240              'padding-top':    '0px',
   241              'padding-bottom':  vPaddingOffs  +  'px',
   242              'background-position': 'center bottom',
   243          });
   244          popup.css({
   245              left: objAbsTL.left  - hOffs + 'px',
   246              top:  objAbsTL.top   - popup.outerHeight() + vPaddingOffs - vShadowOffs + 'px',
   247          });
   248          // popup.css({
   249          //     top:     'auto',
   250          //     bottom:  $( document ).height() -  objAbsTL.top  - vPaddingOffs + 'px',
   251          // });
   252      }
   253  
   254  
   255  
   256  
   257  
   258      inner.css({
   259          'text-align':  'left',
   260      });
   261      if ( horz == "right" ) {
   262          popup.css({
   263              left:  objAbsTL.left + obj.outerWidth() - popup.outerWidth() + hOffs +  'px',
   264              right: 'auto',
   265          });
   266          inner.css({
   267              'text-align':  'right',
   268          });
   269          console.log("righted",objAbsTL.left + obj.outerWidth());
   270      }
   271  
   272  
   273  
   274      inner.show();
   275  
   276      popup.stop(true, true).delay(10).fadeIn(500);
   277  
   278  
   279  }
   280  
   281  
   282  $( document ).ready(function() {
   283  
   284      var wh = $( window ).height();            // Returns height of browser viewport
   285      var dh = $( document ).height();          // Returns height of HTML document
   286      var scrollVert = $('body').scrollTop();   // How many pixel is document scrolled in the viewport
   287      var obj = $('body').find('*').last();
   288      var os1 = obj.offset();                   // Our object's position relative to document root
   289      var os2 = obj[0].getBoundingClientRect(); // Our object's position relative to document root - MINUS scroll pos
   290      console.log("height window - doc:",wh,dh," sctp:", scrollVert , " offs1",os1.top, " offs2", os2.top);
   291      // console.log("offs1-l", os1.left, " offs2-l",  os2.left);
   292  
   293      AddCSS();
   294  
   295  
   296      // We have to couple mouseenter-mouseleave
   297      // for anchor and popup.
   298      // 
   299      // This is fun, since both have a GAP between.
   300  	//
   301  	// We choose an event driven mediation    
   302  	// 
   303      // anchor-mouseenter => showPop
   304      // anchor-mouseleave-popup-mouseenter => keep going
   305      // anchor-mouseleave, popup-mouseleave => hidePop
   306  
   307      var hidingCountdownID = 0;
   308  
   309  
   310      $( 'a' ).on( "mouseenter", function(evt) {
   311          if (hidingCountdownID > 0){
   312          	clearTimeout(hidingCountdownID); // cancel
   313  	        hidingCountdownID = 0;
   314          }
   315          var obj = $(evt.target);
   316          ShowPop(obj);
   317      });
   318  
   319      $( 'a' ).on( "mouseleave", function(evt) {
   320      	hidingCountdownID = setTimeout( function(){
   321  		        var obj = $(evt.target);
   322  		        HidePop(obj);
   323  		        hidingCountdownID = 0;
   324  			}, 400);
   325      });
   326  
   327  
   328      $( '#popup1' ).on( "mouseenter", function(evt) {
   329          if (hidingCountdownID > 0){
   330          	clearTimeout(hidingCountdownID); // cancel
   331  	        hidingCountdownID = 0;
   332          }
   333      });
   334      $( '#popup1' ).on( "mouseleave", function(evt) {
   335          var obj = $(evt.target);
   336          HidePop(obj);
   337      });
   338  
   339  
   340  
   341      $( 'a' ).on( "focusin", function(evt) {
   342          var obj = $(evt.target);
   343          ShowPop(obj);
   344      });
   345  
   346      $( '#popup1' ).on( "focusout", function(evt) {
   347          var obj = $(evt.target);
   348          HidePop(obj);
   349      });
   350  
   351      console.log( "document ready completed" );
   352  });
   353          
   354          
   355          
   356          console.log("hover popups handler added");
   357          //isolated jQuery end;
   358      });
   359  })(window.jQuery.noConflict(true));
   360  
   361