Categories
discuss

How to make a title blink until it becomes active with jQuery?

I have a javascript chat. When a user receives a message, I want the title to blink until it becomes active. (like Gmail Talk)

For example:

  • You are in an other tab. Title is My website
  • Someone talks to you. Title blinks betwen My website and User says: bla bla
  • So you click the tab, and now the title is My website

How can I achieve that using jQuery ?


What i tried so far: (blinking never stop playing)

var isOldTitle = true;
var oldTitle = "oldTitle";
var newTitle = "newTitle";
function changeTitle() {
     document.title = isOldTitle ? oldTitle : newTitle;
     isOldTitle = !isOldTitle;
     setTimeout(changeTitle, 700);
}
changeTitle();

Answer

Full solution:

var isOldTitle = true;
var oldTitle = "oldTitle";
var newTitle = "newTitle";
var interval = null;
function changeTitle() {
    document.title = isOldTitle ? oldTitle : newTitle;
    isOldTitle = !isOldTitle;
}
interval = setInterval(changeTitle, 700);

$(window).focus(function () {
    clearInterval(interval);
    $("title").text(oldTitle);
});
Categories
discuss

Java AES without padding

What are some of the simplest ways to AES encrypt and decrypt a 16 byte array without the automatic padding? I have found solutions that use external libraries, but I want to avoid that if possible.

My current code is

SecretKeySpec skeySpec = new SecretKeySpec(getCryptoKeyByteArray(length=16)); // 128 bits
Cipher encryptor = Cipher.getInstance("AES");
encryptor.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = encryptor.doFinal(plain);

How can I prevent the padding? The plain data is always fixed length and includes its own padding. How can I allow plain to be 16 bytes without causing encrypted to become 32 bytes?

Answer

See my comment. Sorry I probably should have taken a closer look the first time.

  1. Change "AES" to "AES/CBC/NoPadding"
  2. Change decryptor.init(Cipher.DECRYPT_MODE, skeySpec); to decryptor.init(Cipher.DECRYPT_MODE, skeySpec, encryptor.gerParameters());

To encrypt only 16 bytes of data, fixed length, using a method that requires no initialization vector to be saved, Change "AES" to "AES/ECB/NoPadding"

I pick ECB because that is the default.

If you need to encrypt more than 16 bytes, consider using something other than ECB, which suffers a certain repetition detection flaw

In this bitmap example, this image has repeated white blocks, so you can deduce the outline of the image simply by looking for where the blocks become different.

before encryption encrypted

If you are only encrypting one block, it doesn’t really matter though, only if you are encrypting multiple blocks that are combined does ECB become revealing.

Related: https://security.stackexchange.com/questions/15740/what-are-the-variables-of-aes

Categories
discuss

Load class without knowing package?

Is it possible to load a class by name if you don’t know the whole package path? Something like:

getClassLoader().loadClass("Foo");

The class named “Foo” might be around, might not be – I don’t know the package. I’d like to get a listing of matching classes and their packages (but not sure that’s possible!),

Thanks

Answer

Nope. The Java ClassLoader.loadClass(String) method requires that class names must be fully qualified by their package and class name (aka “Binary name” in the Java Language Specification).

Categories
discuss

Android LinearLayout within LinearLayout?

I tried to make a simple layout like so

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">


    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="hello" />

    </LinearLayout>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="world" />

</LinearLayout>

Only the first TextView (“hello”) is displayed. What am I doing wrong?

Answer

Make layout_height of inner layout wrap_content.

Categories
discuss

Programmatically implementing callbacks with JS/jQuery

So, I’m writing a web app. Pretty much everything is done client-side, the server is but a RESTful interface. I’m using jQuery as my framework of choice and implementing my code in a Revealing Module Pattern.

The wireframe of my code basically looks like this:

(function($){
    $.fn.myplugin = function(method)
    {
        if (mp[method])
        {
            return mp[method].apply(this, Array.prototype.slice.call(arguments, 1));
        }
        else if (typeof method === 'object' || ! method)
        {
            return mp.init.apply(this, arguments);
        }
        else
        {
            $.error('Method ' +  method + ' does not exist on $.myplugin');
        }
    };

    var mp =
    {
        init : function( options )
        {
            return this.each(function()
            {
                // stuff
            }
        },
        callbacks : {},
        addCallback : function(hook_name, cb_func, priority)
        {
            // some sanity checking, then push cb_func onto a stack in mp.callbacks[hook_name]
        },
        doCallbacks : function(hook_name)
        {
            if (!hook_name) { hook_name = arguments.callee.caller.name; }
            // check if any callbacks have been registered for hook_name, if so, execute one after the other
        }
    };
})(jQuery);

Pretty straightforward, right?

Now, we’re able to register (multiple, hierarchical) callbacks from inside as well as from outside the application scope.

What is bugging me: To make the whole thing as extensible as possible, I’d have to resort to something along these lines:

foo : function() {
    mp.doCallbacks('foo_before');
    // do actual stuff, maybe some hookpoints in between
    mp.doCallbacks('foo_after');        
}

Every single function inside my app would have to start and end like that. This just doesn’t seem right.

So, JS wizards of SO – what do?

Answer

You can write a function that takes another function as an argument, and returns a new function that calls your hooks around that argument. For instance:

function withCallbacks(name, func)
{
    return function() {
        mp.doCallbacks(name + "_before");
        func();
        mp.doCallbacks(name + "_after"); 
    };
}

Then you can write something like:

foo: withCallbacks("foo", function() {
    // Do actual stuff, maybe some hookpoints in between.
})
Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..