Archive Page 2

Javascript introspection

Very basic, but sometimes useful javascript object introspection

<img id="test">
<script type="text/javascript" language="JavaScript">
    el = document.getElementById('test');
    str = '';
    for (i in el) str += i+'; ';
    alert(str);
</script>

Safari popups in new tab

One of the browsers I’m using on Windows is Safari and I found an interesting tweak for it, that makes popup links open in a new tab instead of a new window. Now I can’t credit the person who discovered this, because I lost the link, but here’s what you do.
Go to

C:\Documents and Settings\User\Application Data\Apple Computer\Safari\Preferences\

where User is your Windows username. Open the file com.apple.Safari.plist for modification and insert the lines below somewhere within the <dict> tag.

<key>TargetedClicksCreateTabs</key>
<true />

That’s it. Start your safari browser and no more popup windows.

Strings in JAVA

A few days ago I needed to extract all strings from .java files and also thought that it would be a good idea to keep count how many times a string is used. So I came up with this simple python script. It’s kind of a quick and dirty solution, but it met my needs for the particular task.

import sys, os, re
from operator import itemgetter  

files = []
strings = {}
exp = re.compile("(\".+?\")")

def klist(bdir):
    dir = os.listdir(bdir)
    for fname in dir:
        if fname.endswith(".java"):
            files.append(bdir+"\\"+fname)
        if os.path.isdir(bdir+"\\"+fname):
            klist(bdir+"\\"+fname)

def get_strings(fname):
    fp = open(fname)
    data = fp.readlines()
    fp.close()
    print fname[fname.rfind("\\")+1:]+":"

for line in data:
        k = 1
        while(k
            m = exp.search(line, k)
            if m!=None:
                fstr = m.groups()[0]
                print "    "+fstr
                cnt = 1
                if strings.has_key(fstr):
                    cnt = strings[fstr] + 1
                strings.update({fstr : cnt})
                k = m.end()
            else:
                k = len(line)

if __name__ == "__main__":
    if len(sys.argv)<2:
        print "Usage: get_strings.py base_directory"
        exit(-1);

klist(sys.argv[1])
    for fname in files:
        get_strings(fname)

print "-"*70
    di = strings.items()
    di.sort(key=lambda x: x[1])
    for (k, v) in di:
        print v, ":", k

So what this basically does is gather the strings and prints out strings for each file and then after a separator line it prints some usage stats. This might contain bugs, because I was in a hurry to write it, so if you use do it at your own risk ;)

« Previous Page


 

December 2009
M T W T F S S
« Nov    
 123456
78910111213
14151617181920
21222324252627
28293031