21 July, 2013

Call User JavaScript with a mouse gesture or a keyboard shortcut

 

  • If the JavaScript code is long, it may be difficult to put it in one line and use it as an Opera command (mouse gesture).
    So it may be useful to put the code in a User JavaScript file and call it with a mouse gesture or a keyboard shortcut
  • Change multiple websites, each one in a different way with one shortcut and one User JS.



The User JS should be in this form:

MyFunction=function(){
...
}

and a mouse gesture or keyboard shortcut should be in this form:

Ctrl G=Go to page,"javascript:MyFunction()"
or
GestureRight, GestureDown=Go to page,"javascript:MyFunction()"



For browsing with JavaScript turned off:

Ctrl G=Enable javascript & Delay, 10 & Go to page,"javascript:MyFunction()" & Delay,100 & Disable javascript


For instance, I have this function that goes directly to the print page on selected websites (for saving or readability reasons, especially while browsing with JavaScript turned off) and on Youtube it goes to the comments page.


Opera command for a mouse gesture:


GestureRight, GestureDown=Enable javascript & Delay, 10 & Go to page,"javascript:goPrint()" & Delay,800 & Disable javascript

User JavaScript:

goPrint=function(){
if (window.location.hostname.match('reuters.com'))
{var x = window.location.toString();x=window.location.toString().substring(x.indexOf('-id')+3,x.indexOf('-id')+22);
window.location='http://www.reuters.com/assets/print?aid=' + x;}
else if (window.location.host=='www.nytimes.com')
{if(window.location.search)
{window.location+='&pagewanted=print'}
else{window.location+='?pagewanted=print'};}
else if (window.location.host=='www.cnbc.com')
{window.location+= '/print/1/displaymode/1098/';}
else if (window.location.hostname.match('youtube.com'))
{var x = window.location.search.toString();
window.location='http://www.youtube.com/all_comments?v=' + x.substring(x.indexOf('v=')+2,x.indexOf('v=')+13)}
else if(window.location.host=='www.independent.co.uk')
{var x=window.location.toString();window.location=x + '?printService=print';}
else if (window.location.host.match('gazeta.pl'))
{var loc = window.location.toString();var x=loc.match(/[0-9,]{2,17}/g).toString().substring(2);
window.location='"http://wiadomosci.gazeta.pl/wiadomosci/2029020,'+ x +'.html'}
else if (window.location.host=='www.washingtonpost.com')
{window.location=window.location.toString().replace('story','print');}
}




Another example:
Center 'See all comments" section on YouTube with a User JavaScript
This User JavaScript may be executed when page is loaded ('DOMContentLoaded' or 'load'), or when you click anywhere on page after loading (change 'DOMContentLoaded' to 'click')

window.addEventListener('DOMContentLoaded',function()
 {if ((window.location.host=='www.youtube.com') 
&& (window.location.pathname.match('all_comments')))
 {
 {var newSS, styles='#watch-discussion{margin:auto !important;}';
 if(document.createStyleSheet)
 {document.createStyleSheet('javascript:'+styles);}
 else
 {newSS=document.createElement('link');
newSS.rel='stylesheet';newSS.href='data:text/css,'+escape(styles); 
document.getElementsByTagName('head')[0].appendChild(newSS)}}
 }
}
 ,false)


If one wants the CSS change to be called by a mouse gesture, the function should be changed to


GestureRight, GestureDown=Enable javascript & Delay, 10 & Go to page,"javascript:CSS_Change()"


CSS_Change=function(){
if ((window.location.host=='www.youtube.com') 
&& (window.location.pathname.match('all_comments')))
 {
 {var newSS, styles='#watch-discussion{margin:auto !important;}';
 if(document.createStyleSheet)
 {document.createStyleSheet('javascript:'+styles);}
 else
 {newSS=document.createElement('link');
newSS.rel='stylesheet';newSS.href='data:text/css,'+escape(styles); 
document.getElementsByTagName('head')[0].appendChild(newSS)}}
 }
}


Other websites can be added with the code:
else if (window.location.host=='x.org'){...}

So with one shortcut and one User JS you can change multiple websites, each one in a different way.

No comments:

Post a Comment