How to use jQuery in Firefox Extension

I use the following example.xul: <?xml version=”1.0″?> <overlay id=”example” xmlns=”http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul”> <head></head> <script type=”application/x-javascript” src=”https://stackoverflow.com/questions/491490/jquery.js”></script> <script type=”application/x-javascript” src=”example.js”></script> </overlay> And here is an example.js (function() { jQuery.noConflict(); $ = function(selector,context) { return new jQuery.fn.init(selector,context||example.doc); }; $.fn = $.prototype = jQuery.fn; example = new function(){}; example.log = function() { Firebug.Console.logFormatted(arguments,null,”log”); }; example.run = function(doc,aEvent) { // Check … Read more

How to pass parameter to function using in addEventListener?

No need to pass anything in. The function used for addEventListener will automatically have this bound to the current element. Simply use this in your function: productLineSelect.addEventListener(‘change’, getSelection, false); function getSelection() { var value = this.options[this.selectedIndex].value; alert(value); } Here’s the fiddle: http://jsfiddle.net/dJ4Wm/ If you want to pass arbitrary data to the function, wrap it in … Read more

Clear Text Selection with JavaScript

if (window.getSelection) { if (window.getSelection().empty) { // Chrome window.getSelection().empty(); } else if (window.getSelection().removeAllRanges) { // Firefox window.getSelection().removeAllRanges(); } } else if (document.selection) { // IE? document.selection.empty(); } Credit to Mr. Y.

How do I convert special UTF-8 chars to their iso-8859-1 equivalent using javascript?

Actually, everything is typically stored as Unicode of some kind internally, but lets not go into that. I’m assuming you’re getting the iconic “åäö” type strings because you’re using an ISO-8859 as your character encoding. There’s a trick you can do to convert those characters. The escape and unescape functions used for encoding and decoding … Read more

REACT – toggle class onclick

Use state. Reacts docs are here. class MyComponent extends Component { constructor(props) { super(props); this.addActiveClass= this.addActiveClass.bind(this); this.state = { active: false, }; } toggleClass() { const currentState = this.state.active; this.setState({ active: !currentState }); }; render() { return ( <div className={this.state.active ? ‘your_className’: null} onClick={this.toggleClass} > <p>{this.props.text}</p> </div> ) } } class Test extends Component { … Read more

Replace words in the body text

To replace a string in your HTML with another use the replace method on innerHTML: document.body.innerHTML = document.body.innerHTML.replace(‘hello’, ‘hi’); Note that this will replace the first instance of hello throughout the body, including any instances in your HTML code (e.g. class names etc..), so use with caution – for better results, try restricting the scope … Read more