Scope And Purpose Of This Post
Holds my notes as I learn Greasemonkey/Javascript/XPath/CSS/HTML/FirefoxWebDevTools.Terminology
Firefox = FFGreasemonkey = GM
JavaScript = JS
link prefixes...
- MozDoc = developer.mozilla.org/en-US/docs = MozDoc
- MozDoc/JSRef = developer.mozilla.org/en-US/docs/Web/JavaScript/Reference
- MozDoc/API = developer.mozilla.org/en-US/docs/Web/API
- GitHub = some github repo
Firefox
Webdev tools in firefox:- Right click => Inspect Element; that brings up webdev tool inspector showing you the source for that element.
- Ctrl+Shift+i or F12: toggles webdev tools in Firefox, so you can see the page inspector, console, and debugger; also the button just to the left of the inspector tab is like an enhanced “Inspect Element” feature;
- In inspector, you can delete nodes, attributes, and other live-editing of the page.
- In console, you can interactively run js and discover properties/methods of objects.
- MozDoc/Tools/Page_Inspector
- MozDoc/Tools/Page_Inspector/How_to/Examine_and_edit_HTML
- MozDoc/Tools/Page_Inspector/How_to/Examine_event_listeners
- MozDoc/Tools/Page_Inspector/How_to/Select_an_element
- MozDoc/Tools/Web_Console
- MozDoc/Tools/Debugger
Greasemonkey (GM)
Demo day links:- Suggested tool is Greasemonkey ( https://www.greasespot.net/ ).
- User script repo is https://greasyfork.org/ (but also https://openuserjs.org )
- Sample user script is Amazon Price Variation fork https://greasyfork.org/en/scripts/34849-amazon-price-variation-fork/code
- Sample amazon item that works with user script: https://www.amazon.com/Apple-Smartwatch-GPS-Certified-Refurbished/dp/B079G4KKY2/
- GM/JS tutorials: https://wiki.greasespot.net/Tutorials
- GM coding tips: https://wiki.greasespot.net/Category:Coding_Tips
- GM API: https://wiki.greasespot.net/Greasemonkey_Manual:API
- GM metadatablock directives: https://wiki.greasespot.net/Metadata_Block
- GM wiki: https://wiki.greasespot.net/GM.xmlHttpRequest
- You will need a "// @grant GM.xmlHttpRequest" in your metadata block.
- Pages on javascript's XMLHttpRequest (not sure how GM.xmlHttpRequest differs from JS's XMLHttpRequest stuff):
GM.xmlHttpRequest({
method: "GET",
url: "http://www.google.com/",
onload: function(response)
{
var responseDom = new DOMParser().parseFromString(
response.responseText,
"text/html");
console.debug("response DOM: ", responseDom);
}
});
Other
- GM stores user scripts inside a sqlite database (see stackoverflow discussion).
- Possibly get to use firefox JS debugger by using one GM user script to inject another GM user script into the page before other page-scripts start executing.
- Brock Adams
- Walk through on generating GM user script that waits for things to appear and manipulates them (Nike example).
- How to block certain page scripts or event handlers (checkForBadJavascript.js code).
JavaScript (JS)
JS tutorials- http://speakingjs.com/ (book, for programmers, introductory and in-depth material, just JS)
- http://eloquentjavascript.net/ (book, also gets into browser stuff and even Node.js)
- https://www.w3schools.com/js/
- TODO: more and rank
JS DOM API, for stuff like document.getElementsByClassName:
- MozDoc/API/Document
- MozDoc/API/Element
- MozDoc/API/Window
- https://www.w3schools.com/jsref/
- https://www.w3schools.com/jsref/obj_window.asp
- Event stuff at MozDoc/API
- stack overflow discussion: https://stackoverflow.com/questions/12897446/userscript-to-wait-for-page-to-load-before-executing-code-techniques
- Nike example again.
- MutationObserver
- MozDoc/JSRef/Functions/arguments
- MozDoc/JSRef/Functions/rest_parameters
- MozDoc/JSRef/Operators/Spread_syntax
- GitHub/OverArmour, a way to modify all functions in a class so they will fail but not crash.
- GitHub/Tracing, easy way to add tracing/pre-functions/post-functions.
JQuery
TODO: look at these and rank.Selector Stuff (HTML, CSS, XPATH)
https://www.w3schools.com/tags/CSS selector related...
- MozDoc/API/Element/querySelectorAll
- MozDoc/API/Document_object_model/Locating_DOM_elements_using_selectors
- MozDoc/CSS/CSS_Selectors
- https://www.w3schools.com/cssref/css_selectors.asp
- perhaps not useful: https://html.spec.whatwg.org/multipage/semantics-other.html#selectors
- perhaps not useful: https://drafts.csswg.org/selectors-4/
- document.evaluate: https://developer.mozilla.org/en-US/docs/Web/API/Document/evaluate
- https://www.w3schools.com/xml/xpath_syntax.asp
- MozDoc/Web/XPath
- MozDoc/API/XPathResult
- wish I knew how to use this one: https://www.w3.org/TR/xpath-31/
- https://www.tutorialspoint.com/xpath/index.htm
- https://doc.scrapy.org/en/xpath-tutorial/topics/xpath-tutorial.html
Other
some sort of web reference, not sure how helpful: http://help.dottoro.com/ljswgnnf.phpAvoid Common [Greasemonkey] Pitfalls
the article: https://www.oreilly.com/library/view/greasemonkey-hacks/0596101651/ch01s13.html- related: https://wiki.greasespot.net/Greasemonkey_Manual:Environment
- related: https://wiki.greasespot.net/Troubleshooting_(Script_Authors)
- beginning through “Going Deeper” talks about how Greasemonkey can’t just plunk down your userscript into the page to be executed; special sandboxing and wrapping needs to happen
- “Greasemonkey 0.5 uses a little-known Firefox feature called
XPCNativeWrappers
. Instead of simply referencing the window object or the document object, Greasemonkey redefines these to beXPCNativeWrappers
. AnXPCNativeWrapper
wraps a reference to the actual object, but doesn't allow the underlying object to redefine methods or intercept properties. This means that when a user script callsdocument.createElement
, it is guaranteed to be the realcreateElement
method, not some random method that was redefined by the remote page.” - “Greasemonkey 0.5 goes to great lengths to allow you to write what appears to be regular JavaScript code, and have it do what you would expect regular JavaScript code to do. But the illusion is not perfect.
XPCNativeWrappers
have some limitations that you need to be aware of. There are 10 common pitfalls to writing Greasemonkey scripts, and all of them revolve around limitations ofXPCNativeWrappers
.”
- “Greasemonkey 0.5 uses a little-known Firefox feature called
- Pitfalls #{1,2} show cases where you need to refer to functions and objects directly rather than by their text name
- Pitfalls #2 shows how you can’t do the usual javascript elmLink.onclick = my_func;, you’d have to do elmLink.addEventListener("click", my_func, true);
- Pitfalls #{3,4,10}, can’t do short-form accessing/modifying of HTML children and attributes (and associated styles from CSS), have to do use more functional interface.
- Pitfall #6: seems like some actions may require accessing the special UNSAFE wrappedJSObject property.
- Pitfall #7: Javascript has default properties (like accessing window.location is the same as accessing window.location.href), but this does not work in GM.
- Pitfall #{8,9}: sometimes you need to use the UNSAFE unsafeWindow, like when invoking a function defined by a script on the original page or doing a watch on window properties.
Written Scripts
Published Scripts
- greasyfork user page: https://greasyfork.org/en/users/218612-jacob-egner
- etf.com view full article
- https://github.com/jmegner/greasemonkey_etf_dot_com_view_full_article
- https://greasyfork.org/en/scripts/373119-etf-com-view-full-article
- google flights get unavailable prices: https://github.com/jmegner/greasemonkey_google_flights_get_unavailable_prices
- Some utility functions that I keep using in GreaseMonkey scripts: https://github.com/jmegner/javascript_utility
Unpublished Bloomberg Article Revealer (because it uses unsafeWindow)...
// ==UserScript==// @name bloomberg_article_revealer
// @namespace JacobEgner
// @version 0.1
// @description Reveals article when blocked by "To continue reading this article, you must..."
// @match https://www.bloomberg.com/view/articles/*
// @run-at document-end
// ==/UserScript==
unsafeWindow._fence = undefined;
No comments:
Post a Comment