前言

繼上一篇 臺銀匯率每日通知,這次著手開發薪資結案狀態通知。

學校行政緩慢,也沒有公文追蹤系統,送出薪資清冊後,要時不時進入主計系統查看是否已審或被退件。

主計系統主要分為三個頁面,登入頁(Login)、內容頁(Menu)、檢查頁(Incheck)。

於登入頁按下登入按鈕後,將另開一個視窗顯示內容頁,原先的登入頁將重新導向至檢查頁。

我利用主計系統重新導向的特性,分別於三個頁面注入內容腳本(content_scripts)。


實作過程

Login.js

透過chrome.storage.onChanged.addListener()監控儲存內容是否被修改。

若被修改,表示薪資狀態有變更,進而發送通知,且每小時輸入帳密,按下登入按鈕。

window.addEventListener("load", function()
{
  let fb_url = "https://m.facebook.com/messages/read/?tid=cid.c.100000749************************"
  console.log("Pay Notify 已載入")
  chrome.storage.onChanged.addListener(function(changes, namespace)
  {
    for(key in changes)
    {
      var storageChange = changes[key]
      if(!storageChange.oldValue || !storageChange.newValue) { return }
      window.open(fb_url + "&msg=" + encodeURI(storageChange.newValue), "_blank")
    }
  })

  let min = 0
  setInterval(function()
  {
    if(min < 5)
    {
      min += 1
      console.log("已運行:" + min + "0 分鐘")
    }
    else
    {
      document.querySelector("#ID").value = "15**05"
      document.querySelector("#PASSWD").value = "12345678"
      document.querySelector("#Enter").click()
    }
  }, 600000)

})

此頁面分為很多frame,故需要用window.frames[""]來指定元素所在的frame

值得注意的是,若要觸發元素的onchange事件,需要new Event()dispatchEvent()

最後獲取想要的薪資資訊,以購案編號為keychrome.storage.local.set()儲存至本機。

window.addEventListener("load", function()
{
  window.frames["TITLE"].document.querySelector("#D14 > input[type=button]").click()

  setTimeout(function()
  {
    window.frames["TITLE"].document.querySelector("#BUTA1").click()
  }, 3000)

  setTimeout(function()
  {
    let change = new Event("change",{"bubbles": true,"cancelable": true})

    window.frames["MAIN"].document.querySelector("#APPYSET").value = "37"
    window.frames["MAIN"].document.querySelector("#APPYSET").dispatchEvent(change)

    window.frames["MAIN"].document.querySelector("#CHKVAL_S").value = "1"
    window.frames["MAIN"].document.querySelector("#CHKVAL_S").dispatchEvent(change)

    window.frames["MAIN"].document.querySelector("#LIST_ALL").value = "Y"
    window.frames["MAIN"].document.querySelector("#LIST_ALL").dispatchEvent(change)
  }, 6000)

  setTimeout(function()
  {
    let outlines = window.frames["MAIN"].document.querySelectorAll("center > div")
    let obj
    for (let outline = 0; outline < outlines.length; outline += 1)
    {
      fonts = outlines[outline].querySelectorAll("font")
      obj = {}
      obj[fonts[0].innerText] = fonts[3].innerText.replace("...", "")+","+fonts[4].innerText.replace(" ", "")+","+fonts[5].innerText+","+fonts[6].innerText
      chrome.storage.local.set(obj, function(){ console.log("已儲存") })
    }
    window.close()
  }, 9000)

})

Incheck.js

直接重新導向回登入頁。

window.addEventListener("load", function()
{
  document.location.href = "http://120.***.142.31/APSWIS_Q/Login_L_Q.asp"
})