User:David Levy/file-toggle-transparency.js

if(typeof(window.FileToggleTransparencyBackgrounds)=='undefined') window.FileToggleTransparencyBackgrounds = [ '#f8fcff', 'white url("//upload.wikimedia.org/wikipedia/commons/5/5d/Checker-16x16.png") repeat', 'white url("//upload.wikimedia.org/wikipedia/commons/6/60/Transparency-tile.png") repeat', 'white', '#ccc', '#888',   '#444',    'black', '#00f', '#f0f' ];

var FileToggleTransparency = { rule:null,

changeBG:function{ FileToggleTransparency.rule.style.background=this.$background; },

onLoad:function{ var sel='.filehistory a img, #file img, #file img:hover'; var bg=FileToggleTransparencyBackgrounds[0]; try { if(document.createStyleSheet){ var s=document.createStyleSheet; s.addRule(sel, 'background:'+bg); FileToggleTransparency.rule = s.rules[s.rules.length-1]; } else { var s=document.createElement('STYLE'); s.type='text/css'; document.getElementsByTagName('HEAD')[0].appendChild(s); s=s.sheet; s.insertRule(sel+' {\nbackground:'+bg+'\n}', s.cssRules.length); FileToggleTransparency.rule = s.cssRules[s.cssRules.length-1]; }       } catch(e){ return; }       if(!FileToggleTransparency.rule) return;

var fm=$('.fullMedia'); if(fm && fm.length>0){ var p = document.createElement('p'); p.className='FileTransparencyToggle'; p.appendChild(document.createTextNode('Transparency fill:')); for(var i=0; i<FileToggleTransparencyBackgrounds.length; i++){ var s=document.createElement('span'); s.style.background=FileToggleTransparencyBackgrounds[i]; s.$background=FileToggleTransparencyBackgrounds[i]; $(s).click(FileToggleTransparency.changeBG); p.appendChild(s); }           fm[0].appendChild(p); }

var hist=$('#mw-imagepage-section-filehistory'); hist = (hist && hist.length) ? hist[0] : null; var b = hist ? hist.firstChild : null; while(b && b.nodeName!='TABLE') b=b.nextSibling; if(b){ var p = document.createElement('p'); p.className='FileTransparencyToggle'; p.appendChild(document.createTextNode('Transparency fill:')); for(var i=0; i<FileToggleTransparencyBackgrounds.length; i++){ var s=document.createElement('span'); s.style.background=FileToggleTransparencyBackgrounds[i]; s.$background=FileToggleTransparencyBackgrounds[i]; $(s).click(FileToggleTransparency.changeBG); p.appendChild(s); }           hist.insertBefore(p,b); }   } };

if(mw.config.get('wgNamespaceNumber')==6 && mw.config.get('wgTitle').match(/\.(svg|png|gif)$/i)){ importStylesheet('User:Anomie/file-toggle-transparency.css'); $(document).ready(FileToggleTransparency.onLoad); }