SlideShare uma empresa Scribd logo
1 de 19
Introduction to Greasemonkey
Greasemonkey
Agenda
•

What is Greasemonkey?

•

What is User Script?

•

What you need, to use Greasemonkey?

•

Installing Greasemonkey

•

Understanding user script metadata

•

Creating your first user script

•

Installing User Scripts

•

Managing existing User Scripts

•

Deep dive into GreaseMonkey / User Scripts

•

Examples
Greasemonkey
What is Greasemonkey?
•

Greasemonkey is a user script manager.

•

It is an extension for the Mozilla Firefox web browser.

What is User Script?
•

User scripts, are scripts that make on-the-fly changes to specific web pages on the client side, typically to change their appearance or
to add or modify functionality.

•

User scripts are written in JavaScript and manipulate the contents of a web page using the Document Object Model interface.

•

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

•

The userscripts.org maintains a database of Greasemonkey scripts.

•

Greasemonkey scripts contain metadata, which specifies the name of the script, a description, a namespace URL used to differentiate
identically named scripts, and URL patterns for which the script is intended to be invoked or not.

•

When the user visits a matching website, Greasemonkey invokes the relevant scripts, which can modify a webpage in any way JavaScript
could.

•

Greasemonkey scripts can also pull external HTTP resources via a non-domain-restricted XMLHTTP request.

Note: A vanilla Firefox install doesn’t support user scripts, you need to use Greasemonkey extension to use user scripts.
Greasemonkey
What you need, to use Greasemonkey?
•

Firefox web browser

•

Greasemonkey extension for Firefox

•

Intermediate knowledge of JavaScript

Installing Greasemonkey
•

You can download Greasemonkey from https://addons.mozilla.org/en-US/firefox/addon/748/

•

You can enable/disable the Greasemonkey extension by clicking on monkey icon in Firefox status bar.

Understanding user script metadata
Every user script has a section of metadata that tells Greasemonkey about the script itself, where it came from, and when to run it.
// ==UserScript==
// @name
// @namespace
// @description
// @include
// @exclude
// ==/UserScript==

Hello World
http://hack.bangalore.corp.yahoo.com/
Example script to alert "Hello world!" on hack day website
http://hack.bangalore.corp.yahoo.com/*
http://hack.bangalore.corp.yahoo.com/archives/category/hackuzelaentry

There are six separate pieces of metadata here, wrapped in a set of Greasemonkey-specific comments.
Greasemonkey
Understanding user script metadata
// ==UserScript==
//
// ==/UserScript==

These comments are significant, and must match exactly.
Greasemonkey uses them to signal the start and end of your user script metadata.
// @name

Hello World

The name of your user script is displayed in the install dialog when you first install the script, and later in the “Manage User Scripts” dialog.
@name is optional. If not present, it defaults to the filename of the user script, minus the .user.js extension.
// @namespace

http://hack.bangalore.corp.yahoo.com/

This is a URL, and Greasemonkey uses it to distinguish user scripts that have the same name but are written by different authors.
// @description

Example script to alert "Hello world!" on hack day website

It is displayed in the install dialog when you first install the script, and later in the “Manage User Scripts” dialog.
@description is optional. If not present, it defaults to an empty string.
// @include
// @exclude

http://hack.bangalore.corp.yahoo.com/*
http://hack.bangalore.corp.yahoo.com/archives/category/hackuzelaentry

These lines tell Greasemonkey on which sites you want your user script to execute.
Excludes take precedence over includes.
@include and @exclude are optional.
If neither is specified, Greasemonkey will execute your user script on all sites.
You may specify as many included and excluded URLs as you need, but you must specify each on its own line.
Greasemonkey
Creating your first user script
// ==UserScript==
// @name
// @namespace
// @description
// @include
// @exclude
// ==/UserScript==

Hello World
http://hack.bangalore.corp.yahoo.com/
Example script to alert "Hello world!" on hack day website
http://hack.bangalore.corp.yahoo.com/*
http://hack.bangalore.corp.yahoo.com/archives/category/hackuzelaentry

alert("Hello World!");

Save this file as hello-world.user.js

Using ‘New User Script’ wizard
You can also create a user script by using ‘New User Script ‘ wizard.
To open ‘New User Script’ wizard, right click the Greasemonkey icon in status
bar and select ‘New User Script’.
By default user scripts are saved in a folder called ‘gm_scripts’ under your firefox
Profile folder.
Greasemonkey
Installing User Scripts
To install a user script you can either drag the user script file on Firefox browser or open it from the net.
In both the cases it will shows ‘Install User Script’ dialog having option to install the script or view script source.

If Greasemonkey is disabled, it will open the file in browser instead of showing the install dialog box.
In this case you can install the script by selecting Tools -> Greasemonkey -> Install User Script…
Once the script is installed you can visit the site matching the pattern mentioned in @include metadata to see it in action.
Greasemonkey
Managing existing User Scripts
You can check and manage the already installed user scripts from ‘Manage User Script’ wizard.
To open ‘Manage User Script’ wizard right click the Greasemonkey icon.
Greasemonkey
Deep dive into GreaseMonkey / User Scripts
Example 1: Hiding all images from a webpage
// ==UserScript==
// @name
// @namespace
// @include
// @exclude
// ==/UserScript==

Hide Images
http://hack.bangalore.corp.yahoo.com/
http://hack.bangalore.corp.yahoo.com/*
http://hack.bangalore.corp.yahoo.com/archives/category/hackuzelaentry

var imgs = document.getElementsByTagName('img');
for (i=0; i<imgs.length; i++)
{
imgs[i].style.visibility = 'hidden';
}

Save the file as hide-image.user.js
Greasemonkey
Example 2: Hide a specific element
// ==UserScript==
// @name
// @namespace
// @include
// @exclude
// ==/UserScript==

XPath Example
http://hack.bangalore.corp.yahoo.com/
http://hack.bangalore.corp.yahoo.com/*
http://hack.bangalore.corp.yahoo.com/archives/category/hackuzelaentry

var allDivs, thisDiv;
allDivs = document.evaluate(
"//div[@id='hd']",
document,
null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
null);
for (var i = 0; i < allDivs.snapshotLength; i++) {
thisDiv = allDivs.snapshotItem(i);
thisDiv.style.display = 'none';
}

Save the file as xpath.user.js
document.evaluate (regular_expression, html_element, namespace, output_order, merge_xpath_results);
XPath Tutorial:
http://zvon.org/comp/r/tut-XPath_1.html#Pages~ListQ20XofQ20XXPaths
Greasemonkey
Example 4: Inserting an element before a specific element
// ==UserScript==
// @name
// @namespace
// @include
// @exclude
// ==/UserScript==

Insert Before
http://hack.bangalore.corp.yahoo.com/
http://hack.bangalore.corp.yahoo.com/*
http://hack.bangalore.corp.yahoo.com/archives/category/hackuzelaentry

Var body, newElement;
body = document.getElementById('bd');
if (body) {
newElement = document.createElement('hr');
body.parentNode.insertBefore(newElement, body);
}

Save the file as insert-before.user.js
Greasemonkey
Example 3: Inserting an element after a specific element
// ==UserScript==
// @name
// @namespace
// @include
// @exclude
// ==/UserScript==

Insert After
http://hack.bangalore.corp.yahoo.com/
http://hack.bangalore.corp.yahoo.com/*
http://hack.bangalore.corp.yahoo.com/archives/category/hackuzelaentry

Var body, newElement;
body = document.getElementById('bd');
if (body) {
newElement = document.createElement('hr');
body.parentNode.insertBefore(newElement, body.nextSibling);
}

Save the file as insert-after.user.js
Greasemonkey
Example 5: Replacing an element
// ==UserScript==
// @name
// @namespace
// @include
// @exclude
// ==/UserScript==

Replace Element
http://hack.bangalore.corp.yahoo.com/
http://hack.bangalore.corp.yahoo.com/*
http://hack.bangalore.corp.yahoo.com/archives/category/hackuzelaentry

var theDiv, altDiv, pInDiv, textNodeInP;
theDiv = document.getElementById('ft');
if (theDiv) {
altDiv = document.createElement('div');
pInDiv = document.createElement('p');
textNodeInP = document.createTextNode("Hack Bangalore is proudly powered by DNA-BLR-FE");
pInDiv.appendChild(textNodeInP);
altDiv.appendChild(pInDiv);
theDiv.parentNode.replaceChild(altDiv, theDiv);
}

Save the file as replace-element.user.js
Greasemonkey
Example 6: Inserting Raw HTML
// ==UserScript==
// @name
// @namespace
// @include
// @exclude
// ==/UserScript==

Inserting Raw HTML
http://hack.bangalore.corp.yahoo.com/
http://hack.bangalore.corp.yahoo.com/*
http://hack.bangalore.corp.yahoo.com/archives/category/hackuzelaentry

var theDiv, altDiv;
theDiv = document.getElementById('ft');
if (theDiv) {
altDiv = document.createElement('div');
altDiv.innerHTML = "<p>Hack Bangalore is proudly powered by dna-blr-fe" +
"<br>Send your feedback to <a href='mailto:dna-blr-fe@yahoo-inc.com'>us</a></p>";
theDiv.parentNode.replaceChild(altDiv, theDiv);
}

Save the file as raw-html.user.js
Greasemonkey
Example 7: Open offsite links in new tab/window
// ==UserScript==
// @name
Open offsite links in new tab/window
// @namespace http://hack.bangalore.corp.yahoo.com/
// @include
*
// ==/UserScript==
var a, thisdomain, links;
thisdomain = window.location.host;
links = document.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
a = links[i];
if (a.host && a.host != thisdomain) {
a.target = "_blank";
}
}

Save the file as open-blank.user.js
Greasemonkey
Example 8: Adding CSS
// ==UserScript==
// @name
Adding CSS
// @namespace http://hack.bangalore.corp.yahoo.com/
// @include http://hack.bangalore.corp.yahoo.com/*
// @exclude http://hack.bangalore.corp.yahoo.com/archives/category/hackuzelaentry
// ==/UserScript==
function addGlobalStyle(css) {
var head, style;
head = document.getElementsByTagName('head')[0];
if (!head) { return; }
style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = css;
head.appendChild(style);
}
addGlobalStyle('p { font-size: large ! important; }');

Save the file as adding-css.user.js
Greasemonkey
Example 8: Adding CSS
// ==UserScript==
// @name
Adding CSS
// @namespace http://hack.bangalore.corp.yahoo.com/
// @include http://hack.bangalore.corp.yahoo.com/*
// @exclude http://hack.bangalore.corp.yahoo.com/archives/category/hackuzelaentry
// ==/UserScript==
function addGlobalStyle(css) {
var head, style;
head = document.getElementsByTagName('head')[0];
if (!head) { return; }
style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = css;
head.appendChild(style);
}
addGlobalStyle('p { font-size: large ! important; }');

Save the file as adding-css.user.js
Greasemonkey
Example 9: Hijacking Click Event
// ==UserScript==
// @name
Hijacking Click Event
// @namespace http://hack.bangalore.corp.yahoo.com/
// @include http://hack.bangalore.corp.yahoo.com/*
// @exclude http://hack.bangalore.corp.yahoo.com/archives/category/hackuzelaentry
// ==/UserScript==
document.addEventListener('click', function(event) {
event.stopPropagation();
event.preventDefault();
}, true);

Save the file as hijacking-click.user.js
Thank You!

Thank you!

Mais conteúdo relacionado

Mais procurados

Doctype html public
Doctype html publicDoctype html public
Doctype html public
Eddy_TKJ
 

Mais procurados (10)

JavaScript Dependencies, Modules & Browserify
JavaScript Dependencies, Modules & BrowserifyJavaScript Dependencies, Modules & Browserify
JavaScript Dependencies, Modules & Browserify
 
Doctype html public
Doctype html publicDoctype html public
Doctype html public
 
Browserify
BrowserifyBrowserify
Browserify
 
Requirejs
RequirejsRequirejs
Requirejs
 
Lightning Talk: Making JS better with Browserify
Lightning Talk: Making JS better with BrowserifyLightning Talk: Making JS better with Browserify
Lightning Talk: Making JS better with Browserify
 
On Demand Javascript - Scalecamp 2009
On Demand Javascript - Scalecamp 2009On Demand Javascript - Scalecamp 2009
On Demand Javascript - Scalecamp 2009
 
Javascript1
Javascript1Javascript1
Javascript1
 
Building native Win8 apps with YUI
Building native Win8 apps with YUIBuilding native Win8 apps with YUI
Building native Win8 apps with YUI
 
Typical customization pitfalls in Magento 2
Typical customization pitfalls in Magento 2Typical customization pitfalls in Magento 2
Typical customization pitfalls in Magento 2
 
Requirejs
RequirejsRequirejs
Requirejs
 

Destaque

презентация
презентацияпрезентация
презентация
kapitoshka90
 
Retail carisma: il personal branding per il marketing dell'impresa commerciale
Retail carisma: il personal branding per il marketing dell'impresa commercialeRetail carisma: il personal branding per il marketing dell'impresa commerciale
Retail carisma: il personal branding per il marketing dell'impresa commerciale
Alessandra Salimbene
 
Guía práctica de ejercicios de word 2007
Guía práctica de ejercicios de word 2007Guía práctica de ejercicios de word 2007
Guía práctica de ejercicios de word 2007
umaumana
 

Destaque (20)

презентация
презентацияпрезентация
презентация
 
Нестероидные противовоспалительные препараты в терапевтической практике
Нестероидные противовоспалительные препараты в терапевтической практикеНестероидные противовоспалительные препараты в терапевтической практике
Нестероидные противовоспалительные препараты в терапевтической практике
 
Retail carisma: il personal branding per il marketing dell'impresa commerciale
Retail carisma: il personal branding per il marketing dell'impresa commercialeRetail carisma: il personal branding per il marketing dell'impresa commerciale
Retail carisma: il personal branding per il marketing dell'impresa commerciale
 
Programazione Neuro Linguistica: La PNL pratica
Programazione Neuro Linguistica: La PNL praticaProgramazione Neuro Linguistica: La PNL pratica
Programazione Neuro Linguistica: La PNL pratica
 
Laboratorio PLUS Consuasiva 11.05.2012
Laboratorio PLUS Consuasiva 11.05.2012Laboratorio PLUS Consuasiva 11.05.2012
Laboratorio PLUS Consuasiva 11.05.2012
 
Frasi & Riflessioni
Frasi & RiflessioniFrasi & Riflessioni
Frasi & Riflessioni
 
Ipnosi
IpnosiIpnosi
Ipnosi
 
Public speaking conquista il tuo pubblico in 7 semplici mosse
Public speaking conquista il tuo pubblico in 7 semplici mosse Public speaking conquista il tuo pubblico in 7 semplici mosse
Public speaking conquista il tuo pubblico in 7 semplici mosse
 
Comunicazione Efficace in Azienda
Comunicazione Efficace in AziendaComunicazione Efficace in Azienda
Comunicazione Efficace in Azienda
 
I Rischi Nei Luoghi Di Lavoro
I Rischi Nei Luoghi Di LavoroI Rischi Nei Luoghi Di Lavoro
I Rischi Nei Luoghi Di Lavoro
 
Frasi Motivazionali
Frasi Motivazionali Frasi Motivazionali
Frasi Motivazionali
 
Sicurezza antincendio nei luoghi di lavoro v2.3
Sicurezza antincendio nei luoghi di lavoro v2.3Sicurezza antincendio nei luoghi di lavoro v2.3
Sicurezza antincendio nei luoghi di lavoro v2.3
 
Strategie di persuasione di Nicoletta Salvatori
Strategie di persuasione di Nicoletta SalvatoriStrategie di persuasione di Nicoletta Salvatori
Strategie di persuasione di Nicoletta Salvatori
 
Guía práctica de ejercicios de word 2007
Guía práctica de ejercicios de word 2007Guía práctica de ejercicios de word 2007
Guía práctica de ejercicios de word 2007
 
PNL: Modellamento Avanzato
PNL: Modellamento Avanzato PNL: Modellamento Avanzato
PNL: Modellamento Avanzato
 
il preposto: compiti e responsabilità
il preposto: compiti e responsabilitàil preposto: compiti e responsabilità
il preposto: compiti e responsabilità
 
Le Armi della Persuasione
Le Armi della PersuasioneLe Armi della Persuasione
Le Armi della Persuasione
 
La gestione del tempo e degli impegni (metodo GTD) con gli strumenti sulla nu...
La gestione del tempo e degli impegni (metodo GTD) con gli strumenti sulla nu...La gestione del tempo e degli impegni (metodo GTD) con gli strumenti sulla nu...
La gestione del tempo e degli impegni (metodo GTD) con gli strumenti sulla nu...
 
I 6 principi della persuasione
I 6 principi della persuasioneI 6 principi della persuasione
I 6 principi della persuasione
 
Sicurezza sui luoghi di lavoro ; D.Lgs. n° 81/2008
Sicurezza sui luoghi di lavoro ; D.Lgs. n° 81/2008Sicurezza sui luoghi di lavoro ; D.Lgs. n° 81/2008
Sicurezza sui luoghi di lavoro ; D.Lgs. n° 81/2008
 

Semelhante a Introduction to Greasemonkey

Write your first WordPress plugin
Write your first WordPress pluginWrite your first WordPress plugin
Write your first WordPress plugin
Anthony Montalbano
 
Plugins on OnDemand with Remote Apps - Atlassian Summit 2012
Plugins on OnDemand with Remote Apps - Atlassian Summit 2012 Plugins on OnDemand with Remote Apps - Atlassian Summit 2012
Plugins on OnDemand with Remote Apps - Atlassian Summit 2012
Atlassian
 
Flash Security, OWASP Chennai
Flash Security, OWASP ChennaiFlash Security, OWASP Chennai
Flash Security, OWASP Chennai
lavakumark
 

Semelhante a Introduction to Greasemonkey (20)

Upstate CSCI 450 jQuery
Upstate CSCI 450 jQueryUpstate CSCI 450 jQuery
Upstate CSCI 450 jQuery
 
Build your own Chrome Extension with AngularJS
Build your own Chrome Extension with AngularJSBuild your own Chrome Extension with AngularJS
Build your own Chrome Extension with AngularJS
 
Develop High Performance Windows 8 Application with HTML5 and JavaScriptHigh ...
Develop High Performance Windows 8 Application with HTML5 and JavaScriptHigh ...Develop High Performance Windows 8 Application with HTML5 and JavaScriptHigh ...
Develop High Performance Windows 8 Application with HTML5 and JavaScriptHigh ...
 
Flash Widget Tutorial
Flash Widget TutorialFlash Widget Tutorial
Flash Widget Tutorial
 
Preparing a WordPress Plugin for Translation
Preparing a WordPress Plugin for TranslationPreparing a WordPress Plugin for Translation
Preparing a WordPress Plugin for Translation
 
Write your first WordPress plugin
Write your first WordPress pluginWrite your first WordPress plugin
Write your first WordPress plugin
 
Micro frontends
Micro frontendsMicro frontends
Micro frontends
 
Lecture6
Lecture6Lecture6
Lecture6
 
jQuery
jQueryjQuery
jQuery
 
JavaScript Modules in Practice
JavaScript Modules in PracticeJavaScript Modules in Practice
JavaScript Modules in Practice
 
Plugins on OnDemand with Remote Apps - Atlassian Summit 2012
Plugins on OnDemand with Remote Apps - Atlassian Summit 2012 Plugins on OnDemand with Remote Apps - Atlassian Summit 2012
Plugins on OnDemand with Remote Apps - Atlassian Summit 2012
 
Brian hogg word camp preparing a plugin for translation
Brian hogg   word camp preparing a plugin for translationBrian hogg   word camp preparing a plugin for translation
Brian hogg word camp preparing a plugin for translation
 
Java script
Java scriptJava script
Java script
 
JavaScript & Dom Manipulation
JavaScript & Dom ManipulationJavaScript & Dom Manipulation
JavaScript & Dom Manipulation
 
Grâce aux tags Varnish, j'ai switché ma prod sur Raspberry Pi
Grâce aux tags Varnish, j'ai switché ma prod sur Raspberry PiGrâce aux tags Varnish, j'ai switché ma prod sur Raspberry Pi
Grâce aux tags Varnish, j'ai switché ma prod sur Raspberry Pi
 
Django at the Disco
Django at the DiscoDjango at the Disco
Django at the Disco
 
Django at the Disco
Django at the DiscoDjango at the Disco
Django at the Disco
 
Django at the Disco
Django at the DiscoDjango at the Disco
Django at the Disco
 
Django at the Disco
Django at the DiscoDjango at the Disco
Django at the Disco
 
Flash Security, OWASP Chennai
Flash Security, OWASP ChennaiFlash Security, OWASP Chennai
Flash Security, OWASP Chennai
 

Mais de Gurpreet Singh Sachdeva (6)

iOS App performance - Things to take care
iOS App performance - Things to take careiOS App performance - Things to take care
iOS App performance - Things to take care
 
Firefox addons
Firefox addonsFirefox addons
Firefox addons
 
iOS training (advanced)
iOS training (advanced)iOS training (advanced)
iOS training (advanced)
 
iOS training (intermediate)
iOS training (intermediate)iOS training (intermediate)
iOS training (intermediate)
 
iOS training (basic)
iOS training (basic)iOS training (basic)
iOS training (basic)
 
Introduction to Data Warehousing
Introduction to Data WarehousingIntroduction to Data Warehousing
Introduction to Data Warehousing
 

Último

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
MateoGardella
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
SanaAli374401
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 

Último (20)

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 

Introduction to Greasemonkey

  • 2. Greasemonkey Agenda • What is Greasemonkey? • What is User Script? • What you need, to use Greasemonkey? • Installing Greasemonkey • Understanding user script metadata • Creating your first user script • Installing User Scripts • Managing existing User Scripts • Deep dive into GreaseMonkey / User Scripts • Examples
  • 3. Greasemonkey What is Greasemonkey? • Greasemonkey is a user script manager. • It is an extension for the Mozilla Firefox web browser. What is User Script? • User scripts, are scripts that make on-the-fly changes to specific web pages on the client side, typically to change their appearance or to add or modify functionality. • User scripts are written in JavaScript and manipulate the contents of a web page using the Document Object Model interface. • Any file that ends in .user.js is a valid Greasemonkey user script. • The userscripts.org maintains a database of Greasemonkey scripts. • Greasemonkey scripts contain metadata, which specifies the name of the script, a description, a namespace URL used to differentiate identically named scripts, and URL patterns for which the script is intended to be invoked or not. • When the user visits a matching website, Greasemonkey invokes the relevant scripts, which can modify a webpage in any way JavaScript could. • Greasemonkey scripts can also pull external HTTP resources via a non-domain-restricted XMLHTTP request. Note: A vanilla Firefox install doesn’t support user scripts, you need to use Greasemonkey extension to use user scripts.
  • 4. Greasemonkey What you need, to use Greasemonkey? • Firefox web browser • Greasemonkey extension for Firefox • Intermediate knowledge of JavaScript Installing Greasemonkey • You can download Greasemonkey from https://addons.mozilla.org/en-US/firefox/addon/748/ • You can enable/disable the Greasemonkey extension by clicking on monkey icon in Firefox status bar. Understanding user script metadata Every user script has a section of metadata that tells Greasemonkey about the script itself, where it came from, and when to run it. // ==UserScript== // @name // @namespace // @description // @include // @exclude // ==/UserScript== Hello World http://hack.bangalore.corp.yahoo.com/ Example script to alert "Hello world!" on hack day website http://hack.bangalore.corp.yahoo.com/* http://hack.bangalore.corp.yahoo.com/archives/category/hackuzelaentry There are six separate pieces of metadata here, wrapped in a set of Greasemonkey-specific comments.
  • 5. Greasemonkey Understanding user script metadata // ==UserScript== // // ==/UserScript== These comments are significant, and must match exactly. Greasemonkey uses them to signal the start and end of your user script metadata. // @name Hello World The name of your user script is displayed in the install dialog when you first install the script, and later in the “Manage User Scripts” dialog. @name is optional. If not present, it defaults to the filename of the user script, minus the .user.js extension. // @namespace http://hack.bangalore.corp.yahoo.com/ This is a URL, and Greasemonkey uses it to distinguish user scripts that have the same name but are written by different authors. // @description Example script to alert "Hello world!" on hack day website It is displayed in the install dialog when you first install the script, and later in the “Manage User Scripts” dialog. @description is optional. If not present, it defaults to an empty string. // @include // @exclude http://hack.bangalore.corp.yahoo.com/* http://hack.bangalore.corp.yahoo.com/archives/category/hackuzelaentry These lines tell Greasemonkey on which sites you want your user script to execute. Excludes take precedence over includes. @include and @exclude are optional. If neither is specified, Greasemonkey will execute your user script on all sites. You may specify as many included and excluded URLs as you need, but you must specify each on its own line.
  • 6. Greasemonkey Creating your first user script // ==UserScript== // @name // @namespace // @description // @include // @exclude // ==/UserScript== Hello World http://hack.bangalore.corp.yahoo.com/ Example script to alert "Hello world!" on hack day website http://hack.bangalore.corp.yahoo.com/* http://hack.bangalore.corp.yahoo.com/archives/category/hackuzelaentry alert("Hello World!"); Save this file as hello-world.user.js Using ‘New User Script’ wizard You can also create a user script by using ‘New User Script ‘ wizard. To open ‘New User Script’ wizard, right click the Greasemonkey icon in status bar and select ‘New User Script’. By default user scripts are saved in a folder called ‘gm_scripts’ under your firefox Profile folder.
  • 7. Greasemonkey Installing User Scripts To install a user script you can either drag the user script file on Firefox browser or open it from the net. In both the cases it will shows ‘Install User Script’ dialog having option to install the script or view script source. If Greasemonkey is disabled, it will open the file in browser instead of showing the install dialog box. In this case you can install the script by selecting Tools -> Greasemonkey -> Install User Script… Once the script is installed you can visit the site matching the pattern mentioned in @include metadata to see it in action.
  • 8. Greasemonkey Managing existing User Scripts You can check and manage the already installed user scripts from ‘Manage User Script’ wizard. To open ‘Manage User Script’ wizard right click the Greasemonkey icon.
  • 9. Greasemonkey Deep dive into GreaseMonkey / User Scripts Example 1: Hiding all images from a webpage // ==UserScript== // @name // @namespace // @include // @exclude // ==/UserScript== Hide Images http://hack.bangalore.corp.yahoo.com/ http://hack.bangalore.corp.yahoo.com/* http://hack.bangalore.corp.yahoo.com/archives/category/hackuzelaentry var imgs = document.getElementsByTagName('img'); for (i=0; i<imgs.length; i++) { imgs[i].style.visibility = 'hidden'; } Save the file as hide-image.user.js
  • 10. Greasemonkey Example 2: Hide a specific element // ==UserScript== // @name // @namespace // @include // @exclude // ==/UserScript== XPath Example http://hack.bangalore.corp.yahoo.com/ http://hack.bangalore.corp.yahoo.com/* http://hack.bangalore.corp.yahoo.com/archives/category/hackuzelaentry var allDivs, thisDiv; allDivs = document.evaluate( "//div[@id='hd']", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); for (var i = 0; i < allDivs.snapshotLength; i++) { thisDiv = allDivs.snapshotItem(i); thisDiv.style.display = 'none'; } Save the file as xpath.user.js document.evaluate (regular_expression, html_element, namespace, output_order, merge_xpath_results); XPath Tutorial: http://zvon.org/comp/r/tut-XPath_1.html#Pages~ListQ20XofQ20XXPaths
  • 11. Greasemonkey Example 4: Inserting an element before a specific element // ==UserScript== // @name // @namespace // @include // @exclude // ==/UserScript== Insert Before http://hack.bangalore.corp.yahoo.com/ http://hack.bangalore.corp.yahoo.com/* http://hack.bangalore.corp.yahoo.com/archives/category/hackuzelaentry Var body, newElement; body = document.getElementById('bd'); if (body) { newElement = document.createElement('hr'); body.parentNode.insertBefore(newElement, body); } Save the file as insert-before.user.js
  • 12. Greasemonkey Example 3: Inserting an element after a specific element // ==UserScript== // @name // @namespace // @include // @exclude // ==/UserScript== Insert After http://hack.bangalore.corp.yahoo.com/ http://hack.bangalore.corp.yahoo.com/* http://hack.bangalore.corp.yahoo.com/archives/category/hackuzelaentry Var body, newElement; body = document.getElementById('bd'); if (body) { newElement = document.createElement('hr'); body.parentNode.insertBefore(newElement, body.nextSibling); } Save the file as insert-after.user.js
  • 13. Greasemonkey Example 5: Replacing an element // ==UserScript== // @name // @namespace // @include // @exclude // ==/UserScript== Replace Element http://hack.bangalore.corp.yahoo.com/ http://hack.bangalore.corp.yahoo.com/* http://hack.bangalore.corp.yahoo.com/archives/category/hackuzelaentry var theDiv, altDiv, pInDiv, textNodeInP; theDiv = document.getElementById('ft'); if (theDiv) { altDiv = document.createElement('div'); pInDiv = document.createElement('p'); textNodeInP = document.createTextNode("Hack Bangalore is proudly powered by DNA-BLR-FE"); pInDiv.appendChild(textNodeInP); altDiv.appendChild(pInDiv); theDiv.parentNode.replaceChild(altDiv, theDiv); } Save the file as replace-element.user.js
  • 14. Greasemonkey Example 6: Inserting Raw HTML // ==UserScript== // @name // @namespace // @include // @exclude // ==/UserScript== Inserting Raw HTML http://hack.bangalore.corp.yahoo.com/ http://hack.bangalore.corp.yahoo.com/* http://hack.bangalore.corp.yahoo.com/archives/category/hackuzelaentry var theDiv, altDiv; theDiv = document.getElementById('ft'); if (theDiv) { altDiv = document.createElement('div'); altDiv.innerHTML = "<p>Hack Bangalore is proudly powered by dna-blr-fe" + "<br>Send your feedback to <a href='mailto:dna-blr-fe@yahoo-inc.com'>us</a></p>"; theDiv.parentNode.replaceChild(altDiv, theDiv); } Save the file as raw-html.user.js
  • 15. Greasemonkey Example 7: Open offsite links in new tab/window // ==UserScript== // @name Open offsite links in new tab/window // @namespace http://hack.bangalore.corp.yahoo.com/ // @include * // ==/UserScript== var a, thisdomain, links; thisdomain = window.location.host; links = document.getElementsByTagName('a'); for (var i = 0; i < links.length; i++) { a = links[i]; if (a.host && a.host != thisdomain) { a.target = "_blank"; } } Save the file as open-blank.user.js
  • 16. Greasemonkey Example 8: Adding CSS // ==UserScript== // @name Adding CSS // @namespace http://hack.bangalore.corp.yahoo.com/ // @include http://hack.bangalore.corp.yahoo.com/* // @exclude http://hack.bangalore.corp.yahoo.com/archives/category/hackuzelaentry // ==/UserScript== function addGlobalStyle(css) { var head, style; head = document.getElementsByTagName('head')[0]; if (!head) { return; } style = document.createElement('style'); style.type = 'text/css'; style.innerHTML = css; head.appendChild(style); } addGlobalStyle('p { font-size: large ! important; }'); Save the file as adding-css.user.js
  • 17. Greasemonkey Example 8: Adding CSS // ==UserScript== // @name Adding CSS // @namespace http://hack.bangalore.corp.yahoo.com/ // @include http://hack.bangalore.corp.yahoo.com/* // @exclude http://hack.bangalore.corp.yahoo.com/archives/category/hackuzelaentry // ==/UserScript== function addGlobalStyle(css) { var head, style; head = document.getElementsByTagName('head')[0]; if (!head) { return; } style = document.createElement('style'); style.type = 'text/css'; style.innerHTML = css; head.appendChild(style); } addGlobalStyle('p { font-size: large ! important; }'); Save the file as adding-css.user.js
  • 18. Greasemonkey Example 9: Hijacking Click Event // ==UserScript== // @name Hijacking Click Event // @namespace http://hack.bangalore.corp.yahoo.com/ // @include http://hack.bangalore.corp.yahoo.com/* // @exclude http://hack.bangalore.corp.yahoo.com/archives/category/hackuzelaentry // ==/UserScript== document.addEventListener('click', function(event) { event.stopPropagation(); event.preventDefault(); }, true); Save the file as hijacking-click.user.js