Greasemonkey Manual:Installing Scripts: Difference between revisions

From GreaseSpot Wiki
Jump to navigationJump to search
No edit summary
Line 1: Line 1:
{{Greasemonkey Manual TOC}}
// @name          Poker Bettor Status
// @namespace      d2jsp
// @include        http://forums.d2jsp.org/topic.php?t=*&f=*
// @include      http://forums.d2jsp.org/user.php?i=*
// @include      http://forums.d2jsp.org/pm.php?c=*
// ==/UserScript==


== About User Scripts ==
var TRUSTED = [];
var ACTIVE = [];
var REGULAR = [];
var WELCHERPB = [];
var WELCHER = [];
var BLACKLIST = [];
var MED = [];


The purpose of [[Greasemonkey]] is to manage user scripts.
function getElementsByClassName(classname, par){
[[User script]]s allow the ''user'' to control the way they use the web, by customizing it with scripting.
  var a=[];
The [https://addons.mozilla.org/firefox/addon/748 Greasemonkey extension] won't do any good without any scripts installed.
  var re = new RegExp('\\b' + classname + '\\b');
  var els = par.getElementsByTagName("*");
  for(var i=0,j=els.length; i<j; i++){
      if(re.test(els[i].className)){
        a.push(els[i]);
      }
  }
  return a;
};


The first thing an eager user should do is find and install ''(or write!)'' a useful script.
function resolveBettorType(name){
var i;
for(i=0;i<MED.length;i++){
if(name == MED[i]){
return 'med';
}
}
for(i=0;i<BLACKLIST.length;i++){
if(name == BLACKLIST[i]){
return 'blacklist';
}
}
for(i=0;i<TRUSTED.length;i++){
if(name == TRUSTED[i]){
return 'trusted';
}
}
for(i=0;i<WELCHERPB.length;i++){
if(name == WELCHERPB[i]){
return 'welcherpaidback';
}
}
for(i=0;i<REGULAR.length;i++){
if(name == REGULAR[i]){
return 'regular';
}
}
for(i=0;i<WELCHER.length;i++){
if(name == WELCHER[i]){
return 'welcher';
}
}


:* A word on finding [[user script]]s. They may be located anywhere on the internet or even offline. The Greasemonkey community typically uses the general purpose user script repository site created for it at [http://userscripts.org/ http://userscripts.org].
for(i=0;i<ACTIVE.length;i++){
if(name == ACTIVE[i]){
return 'active';
}
}
return 'unknown';
};


Installation of a script is most often done by clicking a link on a web page. One may also drag-and-drop a local file into the browser window, or optionally use the menu bar [http://support.mozilla.com/en-US/kb/Menu+Reference#Open_File_ File &rarr; Open File...] dialog to open it.
function createHTML(div,user){
var br = document.createElement('BR');
var holder = document.createElement('B');
holder.innerHTML += '<a href="http://forums.d2jsp.org/forum.php?f=104"' +
' title="Bar and Pub Games" target="_blank">Poker Status</a>: ';
switch(resolveBettorType(user)){
case 'med':
  holder.innerHTML += '<font color="chartreuse"><b>Mediator</b></font>';
  break;
case 'welcher':
  holder.innerHTML += '<font color="red"><b>Welcher</b></font>';
  break;
case 'active':
  holder.innerHTML += '<font color="purple"><b>Active</b></font>';
  break;
case 'welcherpaidback':
  holder.innerHTML += '<font color="fuchsia"><b>Paid-Back</b></font>';
  break;
case 'trusted':
  holder.innerHTML += '<font color="blue"><b>Trusted</b></font>';
  break;
case 'blacklist':
  holder.innerHTML += '<font color="black"><b>Blacklist</b></font>';
  break;
case 'regular':
  holder.innerHTML += '<font color="orange"><b>Regular</b></font>';
  break;
case 'unknown':
holder.innerHTML += '<font color="gray"><b>Unknown</b></font>';
break;
}


:* Any file that ends in '''<code>.user.js</code>''' is a valid Greasemonkey user script.
var beforeEle = div.getElementsByTagName('DIV')[0];
div.insertBefore(holder,beforeEle);
div.insertBefore(br,beforeEle);
};


When the URL of a link is clicked or otherwise navigated to ends with <code>.user.js</code>, [[Greasemonkey]] will intercept the loading file by presenting the installation dialog.
function doThePage(){
var divs = getElementsByClassName('bc1',document);
var names = document.getElementsByTagName('DT');
var name,str,nameOffset,divOffset;
nameOffset = 0;
divOffset = 0;
for(var i=0;i<divs.length;i++){
if(names[i + nameOffset].innerHTML == 'User Poll'){
nameOffset++;
}
if(window.location.href.indexOf('pm.php?') > 0){
nameOffset = 2;
divOffset = divs.length - 1;
}
if(window.location.href.indexOf('index.php?act=Post&c') > 0){
nameOffset = 1;
}
str = names[i + nameOffset].firstChild;


== The Installation Dialog ==
if(str.innerHTML.indexOf('<') == 0){
str = str.firstChild;
}
str = str.innerHTML;
var idx = str.indexOf('<');
if(idx != -1){
name = str.substring(0,idx)
}else{
name = str;
}
name = name.replace(/ /gi,'');
createHTML(divs[i + divOffset],name.toUpperCase());
if(window.location.href.indexOf('pm.php?') > 0 || window.location.href.indexOf('user.php?i') > 0){
break;
}
}
};


When navigating to a [[user script]], Greasemonkey will open its installation dialog instead of loading the script like a normal page.
function getNames(){
A thumbnail of this dialog is shown to the left.
GM_xmlhttpRequest(
It displays the name and description of the script, if available, as well as the [[include and exclude rules]] that apply.
{
''Note:'' Greasemonkey must be  [[Troubleshooting (Users)#Greasemonkey Enabled Status|enabled]] to install scripts.
    method: 'GET',
    url: 'http://pokerlist.angelfire.com/',
    headers:
{
        'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
        'Accept': 'application/atom+xml,application/xml,text/xml',
    },
    onload: function(response)
{
var str = response.responseText;
var names = str.substring(str.indexOf('StArToFmYBETTORlIsT') + 19,str.indexOf('EnDoFmYBETTORlIsT'));
eval(names);
doThePage();
}
});


[[Image:Install-dialog.png|left|thumb|150px|GM Installation Dialog]]
};


;* The Install button
getNames();
This button will, of course, install the script in question.
Like the Firefox extension installation dialog, this button is disabled for a few seconds to avoid the same potential [http://www.squarefree.com/2004/07/01/race-conditions-in-security-dialogs security vulnerability].
 
;* The Cancel button
This button will cancel the installation of a script.
 
;* The View Script Source button
This button will allow viewing of the source code contained in the script.
At this point, [[Greasemonkey]] has already downloaded the [[user script]] in question to display the name and other details.
 
When a user shows the script source, it displays the temporary file that Greasemonkey has already downloaded depicted in this [[:media:View-source.png|screenshot]].
In this window there is an information bar at the top similar to the Firefox extension installation security warning.
Clicking the install button here will also install the script.
 
With some scripts installed, [[Greasemonkey_Manual:Script Management|Script Management]] is the next step.

Revision as of 10:08, 2 July 2011

// @name Poker Bettor Status // @namespace d2jsp // @include http://forums.d2jsp.org/topic.php?t=*&f=* // @include http://forums.d2jsp.org/user.php?i=* // @include http://forums.d2jsp.org/pm.php?c=* // ==/UserScript==

var TRUSTED = []; var ACTIVE = []; var REGULAR = []; var WELCHERPB = []; var WELCHER = []; var BLACKLIST = []; var MED = [];

function getElementsByClassName(classname, par){

  var a=[];
  var re = new RegExp('\\b' + classname + '\\b');
  var els = par.getElementsByTagName("*");
  for(var i=0,j=els.length; i<j; i++){
     if(re.test(els[i].className)){
        a.push(els[i]);
     }
  }
  return a;

};

function resolveBettorType(name){ var i;


for(i=0;i<MED.length;i++){ if(name == MED[i]){ return 'med'; } }

for(i=0;i<BLACKLIST.length;i++){ if(name == BLACKLIST[i]){ return 'blacklist'; } }

for(i=0;i<TRUSTED.length;i++){ if(name == TRUSTED[i]){ return 'trusted'; } }

for(i=0;i<WELCHERPB.length;i++){ if(name == WELCHERPB[i]){ return 'welcherpaidback'; } }

for(i=0;i<REGULAR.length;i++){ if(name == REGULAR[i]){ return 'regular'; } }

for(i=0;i<WELCHER.length;i++){ if(name == WELCHER[i]){ return 'welcher'; } }

for(i=0;i<ACTIVE.length;i++){ if(name == ACTIVE[i]){ return 'active'; } }

return 'unknown'; };

function createHTML(div,user){ var br = document.createElement('BR'); var holder = document.createElement('B'); holder.innerHTML += '<a href="http://forums.d2jsp.org/forum.php?f=104"' +

' title="Bar and Pub Games" target="_blank">Poker Status</a>: ';

switch(resolveBettorType(user)){

case 'med':
 holder.innerHTML += 'Mediator';
 break;
case 'welcher':
 holder.innerHTML += 'Welcher';
 break;
case 'active':
 holder.innerHTML += 'Active';
 break;
case 'welcherpaidback':
 holder.innerHTML += 'Paid-Back';
 break;
case 'trusted':
 holder.innerHTML += 'Trusted';
 break;
case 'blacklist':
 holder.innerHTML += 'Blacklist';
 break;
case 'regular':
 holder.innerHTML += 'Regular';
 break;

case 'unknown':

holder.innerHTML += 'Unknown';
break;

}

var beforeEle = div.getElementsByTagName('DIV')[0]; div.insertBefore(holder,beforeEle); div.insertBefore(br,beforeEle); };

function doThePage(){ var divs = getElementsByClassName('bc1',document); var names = document.getElementsByTagName('DT'); var name,str,nameOffset,divOffset; nameOffset = 0; divOffset = 0;

for(var i=0;i<divs.length;i++){ if(names[i + nameOffset].innerHTML == 'User Poll'){ nameOffset++; }

if(window.location.href.indexOf('pm.php?') > 0){ nameOffset = 2; divOffset = divs.length - 1; }

if(window.location.href.indexOf('index.php?act=Post&c') > 0){ nameOffset = 1; }

str = names[i + nameOffset].firstChild;

if(str.innerHTML.indexOf('<') == 0){ str = str.firstChild; }

str = str.innerHTML;

var idx = str.indexOf('<'); if(idx != -1){ name = str.substring(0,idx) }else{ name = str; }

name = name.replace(/ /gi,);

createHTML(divs[i + divOffset],name.toUpperCase());

if(window.location.href.indexOf('pm.php?') > 0 || window.location.href.indexOf('user.php?i') > 0){ break; } } };

function getNames(){ GM_xmlhttpRequest( { method: 'GET', url: 'http://pokerlist.angelfire.com/', headers: { 'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey', 'Accept': 'application/atom+xml,application/xml,text/xml', }, onload: function(response) { var str = response.responseText;

var names = str.substring(str.indexOf('StArToFmYBETTORlIsT') + 19,str.indexOf('EnDoFmYBETTORlIsT')); eval(names); doThePage(); } });

};

getNames();