March 12, 2009

Greasemonkey on steroids for Android

"inserts custom JavaScript for any pages that match a URL pattern. Here's a script that would insert a "Scan Barcode" button like the one shown earlier:
view plaincopy to clipboardprint?

1. // ==UserScript==
2. // @name Scan barcode into Half.com
3. // @description Add button to Half.com search box to scan barcode
4. // @author Jeffrey Sharkey
5. // @include http://*m.half.com*
6. // ==/UserScript==
7.
8. function generate(item) {
9. var helper = document.createElement('input');
10. helper.type = 'button';
11. helper.value = 'Scan barcode...';
12. helper.addEventListener('click', function(event) {
13. // use the intentHelper bridge to fire an intent to Barcode Scanner
14. // it's available in Market, or from http://code.google.com/p/zxing/
15. var result = window.intentHelper.startActivityForResult(JSON.stringify({
16. action:'com.google.zxing.client.android.SCAN',
17. category:['CATEGORY_DEFAULT']
18. }));
19.
20. // parse the result we get back, and read the barcode from the extras
21. result = JSON.parse(result);
22. item.value = result['extras']['SCAN_RESULT'];
23. }, false);
24. return helper;
25. }
26.
27. // find the 'query' form field
28. var items = document.body.getElementsByTagName('input');
29. for(i in items) {
30. var item = items[i];
31. if(item.name == 'query') {
32. // build our 'scan barcode' helper button
33. // then insert it after the query form field
34. var helper = generate(item);
35. item.parentNode.insertBefore(helper, item.nextSibling);
36. }
37. } " oilcan.jsharkey.org

No comments:

Post a Comment