2025年6月21日 星期六

如何替 google部落格 中的標準 code 區塊 統一新增複製按鈕

如何替 google部落格 中的標準 code 區塊 統一新增複製按鈕

本站可以算是我自己的筆記儲存庫吧自己也經常用,大概最困擾的就是不斷的滑鼠拖曳然後按複製紐,研究了一下成功造出複製按鈕了,現在網站所有文章都有複製按鈕可以用了!

由於是用 JavaScript 動態插入的,所以所有文章都會生效,編輯習慣也不用特別改就是原本的 code 區塊即可不用改變


代碼從這個地方可以新增小工具並插入



然後選擇 HTML/JavaScript



在內容處以下的代碼,直接無腦貼上就行了

<style>
.code-copy-box {
  position: relative;
  margin: 1em 0;
}
.code-copy-btn {
  position: absolute;
  top: 8px;       /* 向上貼近一點 */
  right: 8px;     /* 向右貼近一點 */
  z-index: 3;
  background: transparent;
  border: none;
  cursor: pointer;
  padding: 0;
  opacity: 0.74;
  transition: opacity 0.15s;
  line-height: 1;
}
.code-copy-btn:hover,
.code-copy-btn.copied {
  opacity: 1;
}
.code-copy-btn svg {
  display: block;
  width: 16px;    /* 比原本更小巧 */
  height: 16px;
  fill: #b7babd; /* 深色模式明亮灰 */
  transition: fill 0.18s;
}
.code-copy-btn.copied svg {
  fill: #79ff98; /* 成功後綠色 */
}
.code-copy-tooltip {
  display: none;
  position: absolute;
  top: 4px;
  right: 30px;   /* 跟右邊距離要比 svg 再多一點 */
  background: #22282f;
  color: #b7babd;
  border-radius: 8px;
  padding: 4px 8px;
  font-size: 12px;
  font-family: inherit;
  box-shadow: 0 1px 8px rgba(0,0,0,0.18);
  pointer-events: none;
  z-index: 10;
  opacity: 0.99;
  align-items: center;
  gap: 5px;
  user-select: none;
}
.code-copy-btn.copied + .code-copy-tooltip {
  display: flex;
  animation: fadein 0.15s;
}
@keyframes fadein {
  from { opacity: 0; transform: translateY(-7px);}
  to   { opacity: 0.99; transform: translateY(0);}
}
</style>
<script>
document.addEventListener("DOMContentLoaded", function() {
  const COPY_SVG = `<svg aria-hidden="true" height="16" viewBox="0 0 16 16" width="16" class="octicon octicon-copy"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg>`;
  const CHECK_SVG = `<svg aria-hidden="true" height="16" viewBox="0 0 16 16" width="16" class="octicon octicon-check"><path fill="#79ff98" d="M13.78 5.22a.75.75 0 0 1 0 1.06l-5.25 5.25a.75.75 0 0 1-1.06 0l-2.25-2.25a.75.75 0 1 1 1.06-1.06l1.72 1.72 4.72-4.72a.75.75 0 0 1 1.06 0Z"></path></svg>`;

  document.querySelectorAll('code.hljs').forEach(function(code) {
    if (code.parentElement.classList.contains('code-copy-box')) return;
    var wrapper = document.createElement('div');
    wrapper.className = 'code-copy-box';
    code.parentNode.insertBefore(wrapper, code);
    wrapper.appendChild(code);

    var btn = document.createElement('button');
    btn.className = 'code-copy-btn';
    btn.type = 'button';
    btn.innerHTML = COPY_SVG;
    btn.setAttribute('aria-label', '複製程式碼');
    btn.setAttribute('title', '複製程式碼');
    wrapper.appendChild(btn);

    var tooltip = document.createElement('div');
    tooltip.className = 'code-copy-tooltip';
    tooltip.innerHTML = `Copied!`;
    wrapper.appendChild(tooltip);

    btn.addEventListener('click', function() {
      // 直接處理:移除非斷行空格,然後移除最後一個換行符(通常是多餘的)
      var textToCopy = code.innerText.replace(/\u00A0/g, ' ').replace(/\n$/, '');

      navigator.clipboard.writeText(textToCopy).then(function() {
        btn.classList.add('copied');
        btn.innerHTML = CHECK_SVG;
        setTimeout(function() {
          btn.classList.remove('copied');
          btn.innerHTML = COPY_SVG;
        }, 1200);
      });
    });
  });
});
</script>




2025年5月14日 星期三

日本銀行開戶時應該如何填名字

日本銀行開戶時應該如何填名字

這個問題看起來有點無俚頭,實際來了踩過坑就知道痛了

先直接說普遍的認知
假設名字是 王小明 那應該填入的是

  • 名義: WANG XIAO MING
  • カナ: ワン シャオ ミン

這算是銀行間共同承認的填法,至於你的中文漢字只好放一邊了他們英文優先
(我現場申辦的三井三菱都是上面的模式,樂天線上申辦名字中間少空格被退件改正)



前幾天就忽然看到之前網路申辦的 7-11銀行 用的名字是中文漢字
カナ自然也填成日文的讀音了,可以正常使用沒太大問題

只是如果信用卡要繳錢或幹嘛的,這兩個會被認為是不同人的帳戶
打電話去問了叫我解約重辦,理由是因為沒辦法確認是同一個人
(需要有政府文件背書更改前後的證明才能給你改)

我跟他聊了40分鐘,她其實有信我很認真在幫我找方法
最後願意通融讓我送件審看看,不過我覺得失敗率很高最後算了沒送

銀行填寫的時候是能自己決定漢字應該怎麼讀
如果把漢字寫成中文讀法,就很怪與其這樣不如直接用英文名



還有一個公家機關可以自己決定漢字讀音的地方是健康保險居民票
這兩個在第一次在区役所申請的時候是手寫文件的,可以自己決定怎麼讀

我目前公所是留漢字跟日文讀音,跟銀行留的不一樣是沒發生什麼衝突
公所的下一次更改大概就是歸化的時候才有機會改了吧,或是自己去公所橋橋看了


這是一個 "王" 跟 "ワン" 對不起來的悲劇(日文裡是オウ)

放棄漢字是最省力的毫無阻力到哪都認,要用漢字記得一開始要選對邊XD




2025年5月11日 星期日

Skype 轉到 Tesms 之後沒花完的數點怎麼辦 (Skype停止後打電話方法)

Skype 轉到 Tesms 之後沒花完的數點怎麼辦 (其實還能用)

兩個方法一個是從 teams 裡面進去,有個撥號鍵盤可以打
另一個是直接存取網址 (teams也是從網址打的)


https://calling.web.skype.com/

這裡進去有個簡易版本 skype 可以用


從這裡就可以打了,不過無法儲值用完為止,服務到什麼時候未知,趕緊打完吧


另外有發現如果從網頁登入 teams 或是 安卓的 app 登入都會被要求綁定新信箱,這個信箱還不能是已經存在的微軟帳號,如果信箱已經用掉了可以考慮申請一個 outlook.com 的信箱,直接就可以用了。




2025-05-30
今天發現登入之後雖然會要求綁定信箱,按取消再重登一次就進去了。

然後如果不小心登入其他帳號導致一進去就說你的帳號沒辦法用要切到 Teams 這個試了一下好像只能拚手速了。



登出的方法如圖。重新整理之後大概有 2 秒左右的時間可以操作,趕快感登出把沒點數的帳號登出,然後就可以切到有點數的帳號了。



2025年5月3日 星期六

PowerShell 重試特定區塊腳本的函式

PowerShell 重試特定區塊腳本的函式


引用的函式

irm 'raw.githubusercontent.com/hunandy14/Invoke-Retry/main/Invoke-Retry.ps1'|iex


使用方法1

Invoke-Retry {
    Get-Content "non-existing-file.txt"
} -FinallyScriptBlock {
    Write-Host "  🔄 Running Cleanup"
} -MaxRetries 3 -DelaySeconds 1 -ErrorAction Stop

這是一個簡單的試錯,自動重試3次,每次1秒,然後最後面的 ErrorAction Stop 建議是設定一下避免出現預期之外的事情,內建很多函式只報錯並不會被 try 擷取到。


使用方法2

Invoke-Retry {
    Get-Content "non-existing-file.txt"
} -FinallyScriptBlock {
    Write-Host "  🔄 Running Cleanup"
} -RetryableErrors @(
    [System.Management.Automation.ItemNotFoundException],
    [System.IO.FileNotFoundException],
    [System.IO.DirectoryNotFoundException]
) -MaxRetries 3 -DelaySeconds 1 -ErrorAction Stop

可以通過 RetryableErrors 指定特定錯誤才重試


2025年2月4日 星期二

PowerShell 使用 ssh 驗證本地與遠端檔案的 SHA256 是否一致

PowerShell 使用 ssh 驗證本地與遠端檔案的 SHA256 是否一致

要驗證傳輸到底有沒有傳對用的函式

# 獲取遠端檔案的雜湊值
function Get-RemoteFileHash {
    [CmdletBinding()]
    param ( 
        # 遠端檔案路徑
        [Parameter(Mandatory, ValueFromPipeline)]
        [string]$Path,
        # 遠端登入資訊
        [Parameter(Mandatory)]
        [string]$RemoteLoginInfo,
        # 遠端私鑰
        [Parameter(Mandatory)]
        [string]$IdentityFile,
        # 遠端作業系統
        [Parameter(Mandatory)]
        [ValidateSet('Windows', 'Linux')]
        [string]$RemoteOS
    ) begin {
        # 檢查私鑰是否存在
        if (-not (Test-Path $IdentityFile)) {
            Write-Error "Cannot find path '$IdentityFile' because it does not exist."
        }
        # 定義不同作業系統的雜湊命令
        $hashCommands = @{
            'Windows' = "powershell `"(Get-FileHash -Path ([WildcardPattern]::Escape('$Path')) -Algorithm SHA256 -EA 1).Hash`""
            'Linux'   = "sha256sum '$Path' 2>&1 >/dev/null && sha256sum '$Path' | cut -d' ' -f1 | tr '[:lower:]' '[:upper:]'"
        }
        # SSH 連線參數設定
        $sshParams = @(
            '-oBatchMode=yes'
            "-oIdentityFile=$IdentityFile"
            $RemoteLoginInfo
        )
    } process {
        # 執行遠端雜湊命令
        $hash = & ssh @sshParams $hashCommands[$RemoteOS] 2>&1
        # 檢查 ssh 命令是否成功
        if ($LASTEXITCODE -ne 0) {
            Write-Error "ssh command failed: $hash" 
            return $null
        }
        # 檢查雜湊值格式是否正確 (SHA-256 應為 64 個十六進制字符)
        if (-not ($hash -match '^[A-F0-9]{64}$')) {
            Write-Error "The hash value '$hash' returned from command '$hashCommand' is not a valid SHA-256 format"
            return $null
        }
        return $hash
    }
}


使用範例

Get-RemoteFileHash "/home/chg/work/Tester.bats" `
    -RemoteLoginInfo "chg@192.168.3.53" `
    -IdentityFile "${env:USERPROFILE}\.ssh\id_ed25519" `
    -RemoteOS "Linux"

執行完會自動返回遠端檔案的哈希值,再來比較本地端即可知道是否一致了。




如果是要從 sftp 指令獲取位址可以參考這個

# 解析 SFTP 命令
function ParseSftpCommandPath {
    [CmdletBinding()]
    param (
        [Parameter(ValueFromPipeline)]
        [string]$CommandText
    )
    process {
        # 跳過空行和註解
        if (-not ($CommandText -match '\S') -or $CommandText -match '^\s*#') { return }
        # 解析命令行
        $tokens = [regex]::Matches(
            $CommandText.Trim(), '"[^"]+"|[^\s"]+'
        ).Value.Trim('"')
        # 獲取遠端檔案路徑
        if ($tokens[0] -in 'get','put') {
            $paths = $tokens[1..($tokens.Count-1)].Where({ $_ -notmatch '^-' })
            $idx = @{
                'get' = @{local=-1; remote=0}
                'put' = @{local=0; remote=-1}
            }[$tokens[0]]
            [PSCustomObject]@{
                Command = $tokens[0]
                LocalPath = $paths[$idx.local]
                RemotePath = $paths[$idx.remote]
            }
        }
    }
} # "get /home/chg/work/Tester.bats Tester.bats" | ParseSftpCommandPath


2025-02-04 PowerShell 修改檔案日期與時間 (3)

PowerShell 修改檔案日期與時間 (3)

tags: 部落格文章


關於檔案的 修改時間 和 存取時間有什麼差別可以看這篇站內文


字串格式和日期格式怎麼互轉可以參考這一邊



修改檔案日期

代碼開源在這裡: hunandy14/FileDateEditor


快速使用

irm bit.ly/4gkzM5D|iex; Set-FileDate -File "test\file.txt" "2025-2-4"


詳細功能

# 載入函式
irm bit.ly/4gkzM5D|iex;

# 建立日期
Get-Item "test\file.txt" | Set-FileDate "2025-2-4" -Creation

# 修改日期
Get-Item "test\file.txt" | Set-FileDate "2025-2-4" -Write

# 存取日期
Get-Item "test\file.txt" | Set-FileDate "2025-2-4" -Access

# 日期字串
Get-Item "test\file.txt" | Set-FileDate "2025-02-04" -Format "yyyy-MM-dd"


生成測試檔案

irm bit.ly/4gkzM5D|iex; 1..10 | ForEach-Object{
  $idx=$_.ToString('00')
  New-Item -ItemType File -Path "file_2025-02-$idx.txt" |
  Set-FileDate "2025-02-$idx"
}




2025年1月10日 星期五

git 無更動檔案 合併當前到主分支

git 無更動檔案 合併當前到主分支

最大的分支雖然都是由PR合併的,但是開發的次要主線規模不大太大還是有可能會讓開發者自己管理的

在這種情況下每當做好功能之後會合併回去還得退回,檔案會經過一次大洗牌,這邊提供一個可以不更動檔案的方式合併到分支

為什麼會洗牌是因為git並不提交 merage to 他只有 merage from,就注定你只能切過去不然不要合

解法是還是得造一個工作區,但是不是當前的目錄,而次第二個工作樹,在次要的工作樹合併這件事情就解決了

大概需要底下幾個流程

# 進入主工作樹目錄
cd "Z:/my-project"

# 新增臨時工作樹,檢查出目標分支
git worktree add "Z:/my-project-dev" dev

# 在臨時工作樹中進行合併
cd "Z:/my-project-dev"
git merge fix

# 合併完成後,回到主工作樹刪除臨時工作樹
cd "Z:/my-project"
git worktree remove "Z:/my-project-dev"

對就是單純的在檢出一個工作樹就行了,不過做完記得要刪除因為那個工作樹是會佔用分支的,會導致你的主目錄切不過去。