Advertisement
Guest User

Untitled

a guest
Nov 17th, 2011
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name Hide objects until double click (Flash block)
  3. // @author TarquinWJ
  4. // @namespace http://www.howtocreate.co.uk/
  5. // @version 2.1
  6. // @description  Hides all embedded objects until you double click on
  7. //          the page.
  8. // @ujs:category general: enhancements
  9. // @ujs:published 2005-10-29 20:00
  10. // @ujs:modified 2005-10-29 23:01
  11. // @ujs:documentation http://userjs.org/scripts/general/enhancements/hide-objects
  12. // @ujs:download http://userjs.org/scripts/download/general/enhancements/hide-objects.js
  13. // ==/UserScript==
  14.  
  15.  
  16. /*
  17.  * Please see
  18.  * http://www.howtocreate.co.uk/operaStuff/userJavaScript.html#terms
  19.  * for License and Terms of Use
  20.  */
  21.  
  22. (function () {
  23.  
  24. //specify elements to hide
  25. var typesToHide = ['object','embed','applet'];
  26.  
  27. //timeout to hide notification (in milliseconds) - 0 for no notification
  28. var hideNoticeIn = 5000;
  29.  
  30. //should it show a placeholder for each hidden element - can be clicked to re-show
  31. var showPlaceholder = true;
  32.  
  33. //should it show the address of the blocked object when you hold your mouse over the placeholders
  34. var showTooltip = true;
  35.  
  36. // ***stop editing***
  37.  
  38. var hiddenAlready = [], hasDoneOneReshow, hasHiddenSomething = 0, hasAllowedSomething = [], foo, fooInt, oProtectNum = -1, oProtectType = /^(image\/|text\/|application\/.*xml$)/;
  39.  
  40. for( var i = 0; typesToHide[i]; i++ ) {
  41.     //dynamic objects - Opera will keep these updated
  42.     if( typesToHide[i] == 'object' ) { oProtectNum = i; }
  43.     typesToHide[i] = document.getElementsByTagName(typesToHide[i]);
  44.     hiddenAlready[i] = 0;
  45.     hasAllowedSomething[i] = 0;
  46. }
  47.  
  48. function titleCas(oWord) {
  49.     return oWord.charAt(0).toUpperCase()+oWord.substr(1).toLowerCase();
  50. }
  51.  
  52. function checkForMoreHide() {
  53.     //check if any more have been added since we last checked
  54.     for( var x = 0, y; y = typesToHide[x]; x++ ) {
  55.         for( var z = hiddenAlready[x] + hasAllowedSomething[x], oEl, oAt; oEl = y[z]; z++ ) {
  56.             //do not hide objects if they are being viewed on their own
  57.             if( !z && ( document.body.firstChild == document.body.lastChild ) && ( oEl == document.body.firstChild ) ) { return; }
  58.             if( ( x == oProtectNum ) && ( oAt = oEl.getAttribute('type') ) && oAt.match(oProtectType) ) {
  59.                 //allow certain data types of objects
  60.                 hasAllowedSomething[x]++;
  61.                 continue;
  62.             }
  63.             if( showPlaceholder ) {
  64.                 //make a clickable placeholder
  65.                 var oPH = document.createElement('hidobplhld');
  66.                 oPH.appendChild(document.createTextNode(titleCas(oEl.tagName)+' schovan. Kliknete zde pro zobrazeni.'));
  67.                 oPH.onclick = function () {
  68.                     //show this, and if it is an object, show child embeds as well
  69.                     if( this.actobj.tagName.toLowerCase() == 'object' ) {
  70.                         //objects fall back. so do other elements, but objects are special imho, so fallback children will be shown
  71.                         for( var n = 0, m = ['embed','object'], l; l = m[n]; n++ ) {
  72.                             for( var i = 0, j = this.actobj.getElementsByTagName(l), k; k = j[i]; i++ ) {
  73.                                 if( k.style.wasDsspMod ) { k.style.noticelm.onclick(); }
  74.                             }
  75.                         }
  76.                     }
  77.                     this.actobj.style.display = this.actobj.style.olddisplay;
  78.                     this.actobj.style.wasDsspMod = false;
  79.                     if(showTooltip) { document.body.removeChild(this.fauxtitle); }
  80.                     this.parentNode.removeChild(this);
  81.                 };
  82.                 //try to find appropriate size - offsetFoo reports 0 if falling back or if parent is set to display:none;
  83.                 var oTmpSty = oEl.getAttribute('height');
  84.                 oTmpSty = 'height:' + ( ( oTmpSty ? parseInt( oTmpSty ) : ( oEl.offsetHeight ? oEl.offsetHeight : 50 ) ) - 2 ) + 'px;';
  85.                 var oTmpSty2 = oEl.getAttribute('width');
  86.                 oTmpSty2 = 'width:' + ( ( oTmpSty2 ? parseInt( oTmpSty2 ) : ( oEl.offsetWidth ? oEl.offsetWidth : 150 ) ) - 2 ) + 'px;';
  87.                 oPH.setAttribute('style','display:inline-block;cursor:crosshair;overflow:hidden;border:1px solid #000;background-color:#ddd;color:#000;'+oTmpSty+oTmpSty2);
  88.                 if( showTooltip ) {
  89.                     //try to get the address of the blocked item
  90.                     var oTitle = oEl.getAttribute('src') ? oEl.getAttribute('src') : ( oEl.getAttribute('data') ? oEl.getAttribute('data') : oEl.getAttribute('movie') );
  91.                     if( !oTitle && oEl.tagName.toLowerCase() == 'object' ) {
  92.                         for( var i = 0, j; j = oEl.childNodes[i]; i++ ) {
  93.                             if( !j.tagName || j.tagName.toLowerCase() != 'param' ) { continue; }
  94.                             var pName = j.getAttribute('name').toLowerCase();
  95.                             if( ( pName == 'data' ) || ( pName == 'movie' ) || ( pName == 'src' ) ) {
  96.                                 oTitle = j.getAttribute('value');
  97.                                 if( oTitle ) { break; }
  98.                             }
  99.                         }
  100.                     }
  101.                     if( !oTitle && ( oEl.codebase || oEl.code ) ) {
  102.                         oTitle = ( oEl.codebase ? oEl.codebase : '' ) + ( oEl.code ? oEl.code : '' );
  103.                     }
  104.                     if( oTitle ) {
  105.                         if( oTitle.length > 70 ) {
  106.                             oTitle = oTitle.substr(0,45) + ' ... ' + oTitle.substr(oTitle.length - 25);
  107.                         }
  108.                         //title attribute ignored on custom elements, so using DHTML
  109.                         oPH.fauxtitle = document.createElement('hidobplhldtoolt');
  110.                         oPH.fauxtitle.setAttribute('style','position: absolute;top: 0px;left: 0px;visibility: hidden;border: 1px solid #000;font-size: smaller;color: #000;background-color: #ffffe1;padding: 1px;white-space: nowrap;');
  111.                         oPH.fauxtitle.appendChild(document.createTextNode(oTitle));
  112.                         oPH.addEventListener('mousemove',function (e) {
  113.                             var tY = e.pageY + 22, tX = e.pageX + 10;
  114.                             if( tX + this.fauxtitle.offsetWidth + 20 > window.innerWidth + window.pageXOffset ) { tX = window.innerWidth + window.pageXOffset - ( this.fauxtitle.offsetWidth + 20 ); }
  115.                             if( tY + this.fauxtitle.offsetHeight + 20 > window.innerHeight + window.pageYOffset ) { tY = e.pageY - ( this.fauxtitle.offsetHeight + 20 ); }
  116.                             this.fauxtitle.style.top = tY + 'px';
  117.                             this.fauxtitle.style.left = tX + 'px';
  118.                             if( !this.isO ) {
  119.                                 this.fauxtitle.style.visibility = 'visible';
  120.                                 this.isO = true;
  121.                             }
  122.                         },false);
  123.                         oPH.addEventListener('mouseout',function () {
  124.                             this.fauxtitle.style.visibility = 'hidden';
  125.                             this.fauxtitle.style.top = '0px';
  126.                             this.fauxtitle.style.left = '0px';
  127.                             this.isO = false;
  128.                         },false);
  129.                         document.body.appendChild(oPH.fauxtitle);
  130.                     }
  131.                 }
  132.                 oEl.parentNode.insertBefore(oPH,oEl);
  133.                 //preserve pointers between elements so that download-embeds script does not conflict
  134.                 oPH.actobj = oEl;
  135.                 oEl.style.noticelm = oPH;
  136.             }
  137.             //hide the element
  138.             oEl.style.wasDsspMod = true;
  139.             oEl.style.olddisplay = oEl.style.display ? oEl.style.display : '';
  140.             oEl.style.display = 'none';
  141.             hasHiddenSomething++;
  142.         }
  143.         hiddenAlready[x] = z;
  144.     }
  145. }
  146. var oIntR = setInterval(checkForMoreHide,200);
  147.  
  148. document.addEventListener('load',function () {
  149.     clearInterval(oIntR); //stop checking
  150.     checkForMoreHide(); //one last time
  151.     if( !hasHiddenSomething ) { return; }
  152.     document.documentElement.addEventListener('dblclick',function () {
  153.         //add a double click listener to make all elements reappear
  154.         if( hasDoneOneReshow ) { return; }
  155.         hasDoneOneReshow = true;
  156.         for( var x = 0, y; y = typesToHide[x]; x++ ) {
  157.             for( var z = 0, oEl; z < hiddenAlready[x] && ( oEl = y[z]) ; z++ ) {
  158.                 if( oEl.style.wasDsspMod ) {
  159.                     oEl.style.display = oEl.style.olddisplay;
  160.                     if( showPlaceholder ) { if(showTooltip) { document.body.removeChild(oEl.style.noticelm.fauxtitle); } oEl.parentNode.removeChild(oEl.style.noticelm); }
  161.                 }
  162.             }
  163.         }
  164.     },false);
  165.     if( hideNoticeIn ) {
  166.         //show a notice if required
  167.         foo = document.createElement('sayaboutembeds');
  168.         foo.appendChild(document.createTextNode((hasHiddenSomething--)+' element'+(hasHiddenSomething?'u':'')+' zablokovano. Udelejte dvojklik na stranku pro zobrazeni.'));
  169.         foo.setAttribute('style','position:absolute;top:0px;right:0px;padding:2px;border:2px dotted red;background-color:#fff;color:red;cursor:crosshair;');
  170.         foo.onclick = function () { clearTimeout(fooInt); this.parentNode.removeChild(this); };
  171.         document.body.appendChild(foo);
  172.         fooInt = setTimeout(function () { foo.parentNode.removeChild(foo); },hideNoticeIn);
  173.     }
  174. },false);
  175.  
  176. })();
  177.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement