Greasemonkey Manual:Installing Scripts: Difference between revisions

From GreaseSpot Wiki
Jump to navigationJump to search
No edit summary
m Reverted edits by 69.157.137.115 (talk) to last revision by Arantius
Line 1: Line 1:
// @name          Poker Bettor Status
{{Greasemonkey Manual TOC}}
// @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 = [];
== About User Scripts ==
var ACTIVE = [];
var REGULAR = [];
var WELCHERPB = [];
var WELCHER = [];
var BLACKLIST = [];
var MED = [];


function getElementsByClassName(classname, par){
The purpose of [[Greasemonkey]] is to manage user scripts.
  var a=[];
[[User script]]s allow the ''user'' to control the way they use the web, by customizing it with scripting.
  var re = new RegExp('\\b' + classname + '\\b');
The [https://addons.mozilla.org/firefox/addon/748 Greasemonkey extension] won't do any good without any scripts installed.
  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){
The first thing an eager user should do is find and install ''(or write!)'' a useful script.
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++){
:* 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].
if(name == ACTIVE[i]){
return 'active';
}
}
return 'unknown';
};


function createHTML(div,user){
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.
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;
}


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


function doThePage(){
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.
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){
== The Installation Dialog ==
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(){
When navigating to a [[user script]], Greasemonkey will open its installation dialog instead of loading the script like a normal page.
GM_xmlhttpRequest(
A thumbnail of this dialog is shown to the left.
{
It displays the name and description of the script, if available, as well as the [[include and exclude rules]] that apply.
    method: 'GET',
''Note:'' Greasemonkey must be  [[Troubleshooting (Users)#Greasemonkey Enabled Status|enabled]] to install scripts.
    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]]


getNames();
;* The Install button
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 20:49, 2 July 2011


Greasemonkey Manual
Using Greasemonkey
Installing Scripts
Monkey Menu
Getting Help
User Script Authoring
Editing
Environment
API

About User Scripts

The purpose of Greasemonkey is to manage user scripts. User scripts allow the user to control the way they use the web, by customizing it with scripting. The Greasemonkey extension won't do any good without any scripts installed.

The first thing an eager user should do is find and install (or write!) a useful script.

  • A word on finding user scripts. 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.

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 File → Open File... dialog to open it.

  • Any file that ends in .user.js is a valid Greasemonkey user script.

When the URL of a link is clicked or otherwise navigated to ends with .user.js, Greasemonkey will intercept the loading file by presenting the installation dialog.

The Installation Dialog

When navigating to a user script, Greasemonkey will open its installation dialog instead of loading the script like a normal page. A thumbnail of this dialog is shown to the left. 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 enabled to install scripts.

GM Installation Dialog
  • The Install button

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 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 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, Script Management is the next step.