javascript loop array

var arr = [1, 2, 3, 4, 5],
len = arr.length;
// Method1 (逆)
while (len--) {
console.log(arr[len]);
}
// Method2 (逆)
for (var i = arr.length; i--;) {
console.log(arr[i]);
}
// Method3 (順)
for (var i = 0, a; a = arr[i++];) {
console.log(a);
}
// Method4 (順)
var el;
while (el = arr.shift()) {
console.log(el);
}
view raw loop.js hosted with ❤ by GitHub

Raspbian Jessie 初始設定

export NODE_PATH=/usr/local/lib/node_modules
function vv() {
for id in core sdram_c sdram_i sdram_p ; do \
v=$(vcgencmd measure_volts $id)
v=${v:5}
echo -e "$id:\t$v" ; \
done
}
alias ll="ls -al"
alias free="free -h"
alias cls="clear"
alias cd..="cd .."
alias vt="vcgencmd measure_temp"
alias psm="ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head"
alias psc="ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%cpu | head"
alias t="tmux attach"
alias re="bash ~/MyNodeJS/Ptt/rescan.sh"
alias ptt="bash ~/MyShellScript/loop_Ptt.sh"
alias dcard="bash ~/MyShellScript/loop_DCard.sh"
view raw .bashrc hosted with ❤ by GitHub

基本設定

執行設定程式
sudo raspi-config

  • Expand Filesystem (擴展第二個分割區,讓記憶卡的全部可用空間都可以使用。)
  • Internationalisation Options
    • Change Locale
      • 取消 en_GB.UTF-8 UTF8
      • 選取 zh_TW.UTF8 UTF8
      • 選取 en_US.UTF8 UTF8
    • Change Timezone
      • Asia/Taipei
    • Change Wi-fi Country
      • TW Taiwan
  • Advanced Options
    • A2 Hostname (變更Hostname)

設定系統自動校時
sudo timedatectl set-ntp yes
檢查自動校時
timedatectl

view raw rpi3-init.md hosted with ❤ by GitHub

module.exports init

module.exports = function(config) {
this.config = config;
this.myMethod = function() {
console.log('Has access to this');
};
return this;
};
view raw a.js hosted with ❤ by GitHub
var myModule = require('./a.js')(config);
myModule.myMethod(); //Prints 'Has access to this'
view raw b.js hosted with ❤ by GitHub

indexOf to array object

pos = myArray.map(function(e) { return e.hello; }).indexOf('stevie');
view raw indexOf.js hosted with ❤ by GitHub

find files by extension

var path = require('path'),
fs = require('fs');
function fromDir(startPath, filter, callback) {
//console.log('Starting from dir '+startPath+'/');
if (!fs.existsSync(startPath)) {
console.log("no dir ", startPath);
return;
}
var files = fs.readdirSync(startPath);
for (var i = 0; i < files.length; i++) {
var filename = path.join(startPath, files[i]);
var stat = fs.lstatSync(filename);
if (stat.isDirectory()) {
fromDir(filename, filter, callback); //recurse
} else if (filter.test(filename)) callback(filename);
};
};
fromDir('../LiteScript', /\.html$/, function(filename) {
console.log('-- found: ', filename);
});
view raw findFile.js hosted with ❤ by GitHub

Removing Duplicate Objects From An Array

var arrObj = [{
id: 1,
title: 'a'
}, {
id: 1,
title: 'a'
}, {
id: 2,
title: 'b'
}];
//過濾id重複
var arrUnique = arrObj.filter((obj, pos, arr) => {
return arr.map(x => x['id']).indexOf(obj['id']) === pos
});
//找出重複id
var arrDuplicate = arrObj.filter((obj, pos, arr) => {
return arr.map(x => x['id']).indexOf(obj['id']) !== pos
});
view raw unique.js hosted with ❤ by GitHub

ADB command

取得su權限
adb shell

su

列出全部app
adb shell pm list packages

列出全部系統app
adb shell pm list packages -s

安裝apk
adb install test.apk

移除apk
adb uninstall <com.package.name>

顯示apk實體路徑
adb shell pm path com.android.phone

喚醒手機
adb shell input keyevent 26

adb shell input keyevent KEYCODE_POWER

停用app
adb shell pm disable <com.package.name>

啟用app
adb shell pm enable <com.package.name>

下載資料夾
adb pull /storage/0123-4567/beauty beauty/

上傳資料夾
adb push beauty storage/0123-4567/beauty/

view raw adb.md hosted with ❤ by GitHub

Disable Close Button 停用視窗關閉鈕

SetTitleMatchMode, 2
DisableCloseButton(WinExist("Notepad"))
DisableCloseButton(hWnd="") {
hSysMenu:=DllCall("GetSystemMenu","Int",hWnd,"Int",FALSE)
nCnt:=DllCall("GetMenuItemCount","Int",hSysMenu)
DllCall("RemoveMenu","Int",hSysMenu,"UInt",nCnt-1,"Uint","0x400")
DllCall("RemoveMenu","Int",hSysMenu,"UInt",nCnt-2,"Uint","0x400")
DllCall("DrawMenuBar","Int",hWnd)
}

判斷JS日期物件

// Method 1
typeof date.getMonth === 'function'
// Method 2
date instanceof Date
// Method 3
Object.prototype.toString.call(date) === '[object Date]'
view raw typeof.js hosted with ❤ by GitHub

Javascript Unicode

console.log('好'.charCodeAt()); //22909
console.log('好'.charCodeAt().toString(16)); //597d
console.log(String.fromCharCode(22909)); //好
console.log(String.fromCharCode(0x597d)); //好
console.log('\u597d'); //好
console.log('\u{597d}'); //好
//prototype 擴充類別定義
//方法1 (Regex)
String.prototype.toUnicode = function() {
return this.replace(/./g, function(char) {
var v = char.charCodeAt(0).toString(16);
var pad = "0000";
return (pad.substring(0, pad.length - v.length) + v).toUpperCase();
});
};
console.log('狂'.toUnicode()); //72C2
//方法2 (while)
String.prototype.toUnicode = function () {
return this.replace(/./g, function (char) {
var v = String.charCodeAt(char).toString(16);
if (v.length != 4) {
do {
v = '0' + v;
} while (v.length < 4)
}
return v;
});
};
console.log('狂'.toUnicode()); //72C2
view raw gistfile1.js hosted with ❤ by GitHub

尋找陣列中缺少的數字

function findLost(aryNum) {
aryNum.sort(function(a, b) {
return a > b;
});
if (aryNum.length > 1) {
var intAryIndex = 0,
intNumIndex = 0,
aryAns = [];
while (intNumIndex < aryNum[aryNum.length - 1]) {
if (aryNum[intAryIndex] !== intNumIndex) {
aryAns.push(intNumIndex);
} else {
intAryIndex++;
}
intNumIndex++;
}
return aryAns;
}
}
function findLostV2(aryNum) {
var aryAns = [];
for (var i = 0; i < aryNum[aryNum.length - 1]; i++) {
if (aryNum.indexOf(i) === -1) {
aryAns.push(i);
}
}
return aryAns;
}
console.log(findLost([2, 4, 6, 8, 10]));
console.log(findLostV2([1, 3, 5, 7, 9]));
view raw ans.js hosted with ❤ by GitHub
[問題] 尋找陣列中缺少的數字 給P幣5000 - 看板 Ajax - 批踢踢實業坊

document.evaluate

var links, thisLink;
links = document.evaluate("//a[@href]",
document,
null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
null);
for (var i = 0; i < links.snapshotLength; i++) {
var thisLink = links.snapshotItem(i);
console.log(thisLink.href);
}

error: device not found

請退出adb shell,直接在cmd執行
# Wrong
root@g3:/storage/sdcard0/Download $ adb pull "/LMT icon"
error: device not found
#Right
C:\adb\adb pull "/storage/sdcard0/Download/LMT icon" "LMT icon"
pull: building file list...
pull: /storage/sdcard0/Download/LMT icon/pie10.png -> LMT icon/pie10.png
1 file pulled. 0 files skipped.
550 KB/s (19169 bytes in 0.034s)
view raw demo.sh hosted with ❤ by GitHub

android - adb pull -> device not found - Stack Overflow android - How do i adb pull ALL files of a folder present in SD Card - Stack Overflow

Overwrite Ajax Request

// Method 1
(function(open) {
XMLHttpRequest.prototype.open = function() {
this.addEventListener('load', function() {
console.log('ajax done');
}, false);
open.apply(this, arguments);
};
})(XMLHttpRequest.prototype.open);
// Method 2
(function(open) {
XMLHttpRequest.prototype.open = function() {
this.addEventListener("readystatechange", function() {
if (this.readyState === 4 && this.status === 200) {
console.log('ajax done');
}
}, false);
open.apply(this, arguments);
};
})(XMLHttpRequest.prototype.open);
view raw req.js hosted with ❤ by GitHub

javascript - How can I intercept XMLHttpRequests from a Greasemonkey script? - Stack Overflow XMLHttpRequest.readyState - Web APIs | MDN

Factory Images for Nexus Devices 安裝步驟

Factory Images for Nexus Devices 安裝步驟

範例機種:Nexus 5X (bullhead)

範例版本:6.0.1 (MMB29Q)


事前準備

  • fastboot oem unlock
  • adb/bootloader/recovery驅動安裝
    • MTP → Android Composite ADB Interface
    • fastboot (bootloader) → Android Bootloader Interface
    • Recovery Mode → Android Composite ADB Interface
    • adb sideload → Android Platform Sooner Single ADB Interface
  • 驅動測試
    • 開始狀態,輸入adb devices
    • Bootloader mode,輸入fastboot devices
    • TWRP Sideload,輸入adb devices
  • Factory Images 檔案
  • SuperSU (需要root才要)
  • twrp-x.x.x.x-bullhead.img (需要Custom Recovery才要)

開始

  1. 找到 6.0.1 (MMB29Q)

  2. 點擊 Link,下載檔案 bullhead-mmb29q-factory-197bd207.tgz

  3. 解壓縮至此,資料夾結構如下

     bullhead-mmb29q
         ├─bootloader-bullhead-bhz10k.img
         ├─flash-all.bat
         ├─flash-all.sh
         ├─flash-base.sh
         ├─image-bullhead-mmb29q.zip
         └─radio-bullhead-m8994f-2.6.30.0.68.img
    
  4. 進入Bootloader模式

    開機狀態,輸入 adb reboot bootloader
    關機狀態,使用 Vol Down + Power

以下分成自動、手動刷入


自動刷入

執行 flash-all.bat 即可
註:如需保留個人資料請修改flash-all.bat部份內容,如下

-w 刪除
fastboot -w update image-bullhead-mmb29q.zip


手動刷入

  1. image-bullhead-mmb29q.zip 解壓縮至此,資料夾結構如下

     bullhead-mmb29q    
         ├─android-info.txt
         ├─boot.img
         ├─bootloader-bullhead-bhz10k.img
         ├─cache.img
         ├─flash-all.bat
         ├─flash-all.sh
         ├─flash-base.sh
         ├─image-bullhead-mmb29q.zip
         ├─radio-bullhead-m8994f-2.6.30.0.68.img
         ├─recovery.img
         ├─system.img
         ├─userdata.img
         └─vendor.img
    
  2. 依序鍵入以下指令

    1. fastboot flash bootloader bootloader-bullhead-bhz10k.img
    2. fastboot reboot-bootloader
    3. fastboot flash radio radio-bullhead-m8994f-2.6.31.1.09.img
    4. fastboot reboot-bootloader
    5. fastboot flash boot boot.img
    6. fastboot erase cache
    7. fastboot flash cache cache.img
    8. fastboot flash recovery recovery.img
    9. fastboot flash system system.img
    10. fastboot flash vendor vendor.img

    Note: 如需保留原有TWRP Recovery,請跳過步驟8


刷入TWRP Recovery

  1. 進入Bootloader模式

    開機狀態,輸入 adb reboot bootloader
    關機狀態,使用 Vol Down + Power

  2. fastboot flash recovery twrp-3.0.2-2-bullhead.img


刷入SuperSU

  1. 進入TWRP Recovery

    • 開機狀態,輸入 adb reboot recovery
    • 關機狀態 1. 使用 Vol Down + Power 2. 使用 音量鍵 選擇 Recovery mode,並按下 電源鈕 確認

以下分為兩種方法

方法1

  1. BETA-SuperSU-v2.67-20160121175247.zip,放置手機內部記憶卡
  2. 清除 Dalvik-CacheCache
  3. 選擇 Install
  4. 選擇 BETA-SuperSU-v2.67-20160121175247.zip

方法2

  1. 與電腦連接
  2. 選擇 AdvancedADB Sideload
  3. 勾選 Wipe DalvikCacheWipe Cache
  4. Swipe to start Sideload
  5. 電腦cmd,輸入 adb sideload BETA-SuperSU-v2.67-20160121175247.zip

Nexus 5X 線材檢測

  1. 將Nexus 5X連接至電腦
  2. adb shell "cat /sys/bus/i2c/drivers/fusb301/*/fclientcur"
  3. 回傳0,代表該線材符合標準

USB C to A cable testing refined - Nexus 5X only Edit: Now 6P as well Edit:… 檢測你的5X type-C 數據線是否合格 - LG Nexus 5X 安卓論壇 機鋒論壇

Nexus 5X (bullhead) 初見面

Nexus 5X 初見面


1. 安裝驅動程式

  • adb devices
  • fastboot devices

2. Unlock Bootloader

  1. 開啟開發者人員選項
  2. 設定 → 系統 → 開發者人員選項
  3. OEM 解鎖 → 開啟
  4. USB Debugging → 開啟
  5. adb shell
  6. 允許USB偵錯 → 確定
  7. adb reboot bootloader
  8. fastboot oem unlock
  9. Yes Unlock bootloader (may void warranty)
  10. 在Bootloader畫面中,會顯示DEVICE STATE - unlocked為紅字

3. 刷入TWRP

  1. adb reboot bootloader
  2. fastboot flash recovery twrp-3.0.2-2-bullhead.img

4. 解除資料加密

※ 使用者資料將全部重置 ※

  1. 刷入[FIX] FED-Patcher v8 (ForceEncrypt Disable Patcher)
  2. adb reboot bootloader
  3. fastboot format userdata
  4. fastboot reboot

5. 備份EFS

  1. adb reboot recovery
  2. 選擇 Backup
  3. 只勾選 EFS
  4. Swipe to Backup
  5. 在TWRP裡,將備份好的EFS,匯入電腦端,adb pull /sdcard/TWRP TWRP
  6. 將TWRP資料夾備份至雲端空間

6. Root

  1. BETA-SuperSU-v2.67-20160121175247.zip,放置於手機內部記憶卡
  2. adb reboot recovery
  3. 選擇 Install
  4. 選取 BETA-SuperSU-v2.67-20160121175247.zip
  5. Wipe Cache/Dalvik
  6. 重新開機

查詢PowerShell版本

#査Powershell版本
$PSVersionTable
Get-Host