คำสั่ง debugger; ใน javascript

เพื่อนๆ คงคุ้นเคยกับหน้าจอนี้ใน Developer Tools ของ Chrome/Firefox/Safari ที่เราสามารถกำหนด breakpoint เพื่อหยุดการทำงานโปรแกรม และไล่ดูสถานะของตัวแปรต่างๆ รวมทั้งรันโปรแกรมต่อไปจนถึง breakpoint ต่อไปได้

breakpoint.png
สมมติว่าฟังก์ชันนี้ซับซ้อนมาก… เราก็กำหนด breakpoint และรันโปรแกรม บราวเซอร์0tโชว์สถานะของตัวแปรต่างๆ ว่า ตอนนี้ a = 1, b =2 , …

เรารู้ว่าบราวเซอร์มี feature นี้ช่วยให้เราเขียนโปรแกรมง่ายขึ้น… แต่ไม่ค่อยได้ใช้เพราะการไล่เปิด tab source code และกำหนด breakpoint ตำแหน่งที่เราต้องการนั้นยากเหลือเกิน (บางกรณีทำได้ยากเพราะใช้ dependency injection/asynchronous module load) ทำให้ Chrome ไม่แสดง source code โปรแกรมเราให้เลือก

คำสั่ง debugger; ช่วยคุณได้!

เมื่อหาบรรทัดที่จะกำหนด breakpoint ไม่เจอ (หรือหาเจอแต่ขี้เกียจเพราะใช้เวลานาน) ลองทำตามนี้ดีกว่า

  1. ใส่คำสั่ง debugger; ไว้ใน source code ตำแหน่งที่ต้องการกำหนด breakpoint เช่น
    function addMe() {
        var a = 1;
        var b = 2;
        debugger;  // ใส่บรรทัดนี้
    
        var c = a + b;
        document.getElementById("result").innerHTML = "a + b = " + c;
    }
  2. Reload หน้าเว็บ และเปิด Developer Tool tab Sources (ใช้ได้ใน Chrome, Firefox, Safari)
  3. รันโค้ดให้ถึง breakpoint

breakpoint2.png

อ่านเพิ่มเติม:

ใช้จอย DualShock 3 บน Windows 10

  1. ทำตามขั้นตอนใน http://pcgamingwiki.com/wiki/Controller:DualShock_3
    เพื่อติดตั้ง Driver
  2. Disable Driver signature enforcement ของ Windows 10 ตามขึ้นตอนนี้
    1. Go to Start Menu and open Settings
    2. Go to Update and Recovery
    3. Choose Recovery from the left pane
    4. Under Recovery section, choose Advanced Startup
    5. Your computer will restart and you’ll get advanced startup options on the next boot
    6. In these options, choose Troubleshoot
    7. Now head to Advanced options
    8. And then Startup settings
    9. The computer will restart again and it will give you the list of startup settings you can change
    10. You should look for Disable driver signature enforcement, to turn off this option, press F7
    11. Restart your computer and boot normally
      (ขั้นตอนจาก: http://windowsreport.com/ps3-controller-windows-10/)
  3. ใช้จอยได้ตามปกติ (จะมองเห็นเป็น Xbox 360 Controller)

git diff ด้วย meld

ปกติ git diff ใช้ดูการเปลี่ยนแปลงของไฟล์ได้แบบ text mode เช่น

gitdiff.png

ผมติดใจใช้ meld เป็น diff tool มาโดยตลอดเพราะทำให้เห็นการเปลี่ยนแปลงง่ายขึ้นเป็นแบบนี้

meld.png

ที่ผ่านมาผมก็ config git ให้ใช้ meld ด้วยท่าแปลกๆ แล้วก็ใช้มาเรื่อยๆ แต่จะมีอยู่จุดนึงที่รู้สึกว่าลำบากไปหน่อยคือเวลา git diff ระหว่างเวอร์ชั่นแล้วมีไฟล์ที่ต่างกันหลายๆ ไฟล์ เช่น

git diff 1.0..2.0    # สมมติมี 10 ไฟล์ที่ต่างกันจะมีหน้าต่าง meld เด้งขึ้นมา 10 ครั้ง

รู้จักกับ git difftool

ล่าสุดผมล้างเครื่องใหม่ เลยลองค้นดูว่าตอนนี้มีวิธีอะไรที่ดีกว่านี้มั้ยก็ไปเจอกับคำตอบนี้ ใน stackoverflow
ขอคัดลอกคำตอบมาแปะดังนี้

  1. ติดตั้ง meld และตั้งค่า git
    sudo apt-get install meld         # Ubuntu
    brew install homebrew/gui/meld    # OSX
    git config --global diff.tool meld
  2. เวลาอยากดูการเปลี่ยนแปลงก็เรียกคำสั่ง
    git difftool   # ใช้แทน git diff ได้เลย
  3. แต่ที่เจ๋งไปกว่านั้นคือ
    git difftool -d 1.0..2.0      # คำสั่งนี้จะเปิด meld ในโหมด dir diff

บอกลาหน้าต่าง meld เด้งขึ้นมา 10 ครั้งไปได้เลยเพราะคุณจะเจอกับหน้าจอนี้แทน

meld_dir2.png

หน้าจอ meld จะสรุปว่ามีไฟล์ไหนที่แตกต่างกันบ้าง ให้เราดับเบิ้ลคลิกเข้าไปดูไฟล์ที่สนใจได้ทันที

ปกติชอบใช้เครื่องมือ diff/merge ตัวไหนกันบ้างครับ?

GIF with H.264

เคยสงสัยไหมว่าทำไม GIF ใน facebook ถึงกดหยุดได้ ณ จังหวะที่เรากดทันที (ลองเล่นได้ที่นี่ หรือลอง inspect ดูได้ท้ายโพสต์)

facebook_git.png
ก็เพราะว่ามันไม่ใช่ GIF จริงๆ น่ะสิ

นี่มันวีดีโอชัดๆ! เป็น html5 video ที่เล่น loop และ play/pause ตามการกดของเรา

สงสัยไหมทำไม GIF ใน facebook ถึงโหลดเร็วกว่าเว็บอื่นๆ

GIF ต้นฉบับ 5MB:

gif.png

H264 Video 231KB:

video.png

ถ้าไม่ใช้ Video หละจะกด Play/Pause GIF ได้ไหม?
ตอบ ได้ ใช้ 2 รูปสิ GIF กับภาพนิ่ง 

SciTE กับภาษาไทย tis620

พอดีต้องกลับไปแก้โค้ด php ที่ใช้ encoding tis-620, windows-874

หาทางใช้ Sublime Text เปิดแล้วไม่สำเร็จเลยกลับไปขุด Text Editor ตัวโปรดตอนเรียนป.ตรีมาใช้ มันคือ SciTE

SciTE224.png

สามารถตั้งค่าให้เปิดไฟล์โค้ดภาษาไทยได้โดยกด Options -> Local settings

code.page=620
character.set=222

Use Volume button to wake up Nexus 5

nexus5Seem like power button on my Nexus 5 will be broken soon. When I pressed it, the screen turn off and on. I have to press it twice to make it work.

There are many custom ROMs/kernels out there that allow you to:

  • Double tap the screen to wake it up but it cost 2% of battery/hour
  • Use Volume Rocker to wake the screen

I don’t want to install custom ROMs because some of them are not open sources.

The cleanest way to remap button would be:

  1. clone AOSP
  2. modify /system/usr/keylayout/xxx.kl to remap the button
  3. build my own ROM, and flash it

Remap Volume button with temporary recovery ROM

I found a way to avoid permanently root my phone, or flash a custom ROM.  But you still need to unlock your bootloader which will WIPE ALL DATA ON YOUR PHONE!

I temporary boot into a custom recovery ROM via fastboot, modify the keymap file, and eureka!

  1. Get adb and fastboot from Android SDK
  2. Download openrecovery-twrp-2.8.5.2-hammerhead.img (a custom recovery for Nexus 5)
  3. Unlock the bootloader with following command:
    WARNING: THIS WILL WIPE ALL DATA ON YOUR PHONE!!!

    adb reboot bootloader         # reboot to bootloader
    fastboot oem unlock           # unlock the bootloader
  4. Temporary boot into a custom recovery ROM (without flashing it)
    adb reboot bootloader
    fastboot boot openrecovery-twrp-2.8.5.2-hammerhead.img
  5. Press Mount and select System to mount /systemrecovery
  6. On your PC, run:
    adb shell
    vi /system/usr/keylayout/gpio-keys.kl
  7. Modify the content of file from:
    key 115   VOLUME_UP
    key 114   VOLUME_DOWN

    to

    key 115   POWER
    key 114   VOLUME_DOWN
  8. Save [ESC] then :wq[ENTER], then unmount the /system by select System again on your phone
  9. Press back (bottom right of the screen) and Reboot -> System
  10. Choose NOT to install Super SU (DON’T swipe!)

When the phone reboots, you may use Volume Up button to wake the screen now. Unfortunately, the button will not turn up the volume anymore.

Note

I tried modifying the keymap to allow the volume button to acts as both VOLUME UP and button to wake the screen:

key 115   VOLUME_UP    WAKE      # THIS WONT WORK
key 114   VOLUME_DOWN

This does not work because the behaviour of Wake Key was controlled by  PhoneWindowManager.interceptKeyBeforeQueueing which you can read more from the doc and PhoneWindowManager source code. Seem like we can turn up the volume while listening to music without waking up the phone (That might be the feature that prevent this to work).

.ssh/config: สร้าง shortcut สำหรับ ssh ที่ใช้บ่อยๆ กันเถอะ

เวลาจะ SSH แล้วต้องมานั่งนึกมั้ยครับว่า server นี้ใช้ user/port อะไร, domain บางทีก็ยาวจนขี้เกียจพิมพ์ คำสั่ง ssh ใช้ option -p ระบุ port ส่วน scp ต้องใช้ -P (พีใหญ่) พิมพ์ผิดพิมพ์ถูกหลายครั้งกว่าจะส่งไฟล์ได้

ssh -p 2345 satoshi@gym1.pewter.com
scp -P 2345 satoshi@gym1.pewter.com:~/pikachu.txt .

ไฟล์ ~/.ssh/config

ไฟล์ ~/.ssh/config ช่วยให้เราตั้งค่า username, port, domain สำหรับ host ที่เราใช้บ่อยๆ เก็บไว้ได้

Host gym1                   # ตั้งชื่อเล่น
        HostName gym1.pewter.com    # หรือใช้ IP ก็ได้
        User satoshi
        Port 2345
Host red                    # มีได้หลายๆ host เลย
        HostName team-rocket.com
        User nyarth
        Port 4529

เวลาจะใช้ ssh ก็พิมพ์เพียงแค่ชื่อ Host สั้นๆ ก็พอ

ssh gym1
scp gym1:~/pikachu.txt .

ปล. ลอง man ssh_config ดูได้นะครับว่ามี options อะไรให้ใช้อีกบ้าง

Tetraodontidae – Pufferfish vs Porcupinefish

Divers Who Want To Learn More

Fact: Sometimes porcupinefish and pufferfish are collectively known as pufferfish (this is where confusion often sets in)

The Tetradontiformes are represented by 10 families with about 360 different species overall; most are marine and they are most diverse in the tropics with very few species in temperate waters, they are completely absent from cold waters. A few species are found in freshwater streams and estuaries.

This post is mostly focused on the difference between porcupinefish and pufferfish because during a conversation with a friend today we were unsure whether the fish we were discussing were called porcupinefish, balloonfish or pufferfish. So this post is for my dear friend and big brother Kana 😀

Common namePorcupinefish / Balloonfish Pufferfish
FamilyDiodontidaeTetraodontidae
Defence mechanismsPuff up body by drawing in water or air through the mouth into a small chamber near the stomach.

Their body is covered with sharp spines which…

View original post 445 more words