adamkdean

software engineering

Use jQuery to see if an element exists

By Adam K Dean on

You can use jQuery to see if an element exists by checking if the length of the jQuery object is > zero.

var exists = $('.your-selector-here').length > 0;

Probably the shortest snippet so far.

Install LESS highlighting in Sublime Text

By Adam K Dean on

Sublime Text doesn't come with LESS highlighting by default, but it's quite easy to install.

I've pulled the following from my previous blog post on how to install Zen Coding/Emmet in Sublime Text.

The first thing you want to do is open up Sublime Text. If you don't already have the package manager installed, then install it by bringing up the console Ctrl + ' and pasting in:

import urllib.request,os; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); urllib.request.install_opener( urllib.request.build_opener( urllib.request.ProxyHandler()) ); open(os.path.join(ipp, pf), 'wb').write(urllib.request.urlopen( 'http://sublime.wbond.net/' + pf.replace(' ','%20')).read())

Press enter and wait for that to install. Once complete, press Ctrl + Shift + P to bring up the command palette and type Install, press enter to select Package Control: Install package.

Once the menu appears, type LESS and select the first package.

Enjoy.

Bind to public IP from an AWS EC2 instance

By Adam K Dean on

One problem I have faced with Amazon Web Services EC2 instances is binding to a public IP or hostname. When you get an EC2 instance, you will have access to your local IP, and to your local hostname, but if you want to connect to the instance from an external source, you have to bind to a public interface.

There may be some fandangled way of using the AWS API to get your instance id and then from that, get your public hostname, but I have discovered a much easier way.

Bind to 0.0.0.0.

sudo nc -l 0.0.0.0 80

As you can see, I can now access it via my external browser.

Intercept AJAX requests

By Adam K Dean on

Just a snippet today, a reblog, of an article on how to Hijack AJAX Requests Like A Terrorist by Daniel Huckstep.

There are some who say not to modify prototypes because you never know how other libraries will use them, but then there are also some people who think that driving 30 MPH on a 60 MPH road is a good idea, so not everything everyone thinks is a good idea, is a good idea.

(function(open) {
  XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {
    // do some magic
    open.call(this, method, url, async, user, pass);
  };
})(XMLHttpRequest.prototype.open);

This works perfectly well for intercepting AJAX requests. In my case, it is to modify the URL that is requested by AngularJS.

Cross thread calls made easy

By Adam K Dean on

Update (27/06/2013): New blog post regarding parameterized calls - Cross thread calls made easier.

We've all had the problem, we're trying to do something and the form locks up, so we decide it's come to that time where we have to use another thread. Great, spaghetti code awaits. We get a coffee and we set to, eventually coming up with an awesome multi-threaded solution which keeps our form redrawing whilst we process what we're processing... except for when we try to update the form and get the dreaded Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on.

It may seem the quickest fix, but please don't under any circumstance use this recipe for disaster, ever.

Form1.CheckForIllegalCrossThreadCalls = false; // this is bad!

The situation isn't as dire as it first seems, read on and you'll see how to quickly fix this. In no time it'll become second nature. The first snippet applies if you have to pass parameters to a control, for example, you want to write text to a textbox:

using System;
using System.Windows.Forms;
using System.Threading;

namespace CrossThreadCalls
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private delegate void ObjectDelegate(object obj);

        private void button1_Click(object sender, EventArgs e)
        {
            // first thing we do is create a delegate pointing to update method
            ObjectDelegate del = new ObjectDelegate(UpdateTextBox);

            // then simply enough, we invoke it
            del.Invoke("Hello from button1_Click!");

            // if we wanted to create a thread to use it, we'd use a
            // params threadstart and pass the delegate as an object
            Thread th = new Thread(new ParameterizedThreadStart(WorkThread));
            th.Start(del);
        }

        private void WorkThread(object obj)
        {
            // we would then unbox the obj into the delegate
            ObjectDelegate del = (ObjectDelegate)obj;

            // and invoke it like before
            del.Invoke("Hello from WorkThread!");
        }

        private void UpdateTextBox(object obj)
        {
            // do we need to switch threads?
            if (InvokeRequired)
            {
                // we then create the delegate again
                // if you've made it global then you won't need to do this
                ObjectDelegate method = new ObjectDelegate(UpdateTextBox);

                // we then simply invoke it and return
                Invoke(method, obj);
                return;
            }

            // ok so now we're here, this means we're able to update the control
            // so we unbox the object into a string
            string text = (string)obj;

            // and update
            textBox1.Text += text + "\r\n";
        }
    }
}

Now lets say you simply want to run a method which doesn't take any arguments, things get much easier now. As with before we invoke the method, but there is no need for delegates:

using System;
using System.Windows.Forms;
using System.Threading;

namespace TestApp2
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }  

        private void button1_Click(object sender, EventArgs e)
        {
            // no delegates are needed here
            UpdateTextBox();

            // just to show what it'd be like from another thread
            // we create a standard thread without params
            Thread th = new Thread(new ThreadStart(WorkThread));
            th.Start();
        }

        private void WorkThread()
        {
            // we have no params, so just call the method
            UpdateTextBox();
        }

        private void UpdateTextBox()
        {
            // do we need to switch threads?
            if (InvokeRequired)
            {
                // slightly different now, as we dont need params
                // we can just use MethodInvoker
                MethodInvoker method = new MethodInvoker(UpdateTextBox);
                Invoke(method);
                return;
            }

            // as we didn't take a param, we just update
            textBox1.Text += "Hello world!\r\n";
        }
    }
}

So as you see, it's actually quite easy. If you're a bit confused by some of the things I've used here, post a comment, or read more on boxing and unboxing and threading.

If you're having difficulties, come and ask us at Dream.In.Code.