Last active
January 17, 2021 16:58
-
-
Save IvanaGyro/219b6ff724906cba193bbbf99daf525e to your computer and use it in GitHub Desktop.
Copy results of Taipei traffic violation reports from Gmail web page to the clipboard.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Usage: | |
| * 1. Minify this script. | |
| * 2. Rewrite minified script to `javascript:THE_MINIFIED_SCIPT`. | |
| * 3. Add modified script to your bookmark. | |
| * 4. Click it when you're at the Gmail page. | |
| */ | |
| (async function () { | |
| if (location.hostname != 'mail.google.com') return; | |
| async function sleep(ms) { | |
| await new Promise((resolve) => setTimeout(resolve, ms)); | |
| } | |
| function format (content) { | |
| let checkers = [ | |
| [/.+?檢舉\s+?([\w-]+)\s+?號車.+?案號[\s:]+?([\w-]+).*/, '案號:$2\n車號:$1'], | |
| [/.*?違規地點[\s:]+?(\S.*?)\s*$/, '違規地點:$1'], | |
| [/.*?違規事實.+?(支道未讓幹道|轉彎車未讓直行車|未讓行人|不讓行進中之車輛).*/, '違規事實:$1'], | |
| [/.*?處理結果[\s:]+?(\S.*?)\s*$/, '處理結果:$1'], | |
| [/.*?承辦員警[\s:]+?(\S.*?)\s*$/, '承辦員警:$1'], | |
| [/.*?連絡電話[\s:]+?(\S.*?)\s*$/, '連絡電話:$1'], | |
| ]; | |
| let matches = Array(checkers.length).fill(0).map(_ => []) | |
| for (let line of content.split('\n')) { | |
| let hasChecker = false; | |
| checkers.some((checker, i) => { | |
| if (!checker) return false; | |
| hasChecker = true; | |
| let match = line.replace(...checker); | |
| if (match.length === line.length) return false; | |
| matches[i] = match.split('\n'); | |
| checkers[i] = false; | |
| return true; | |
| }); | |
| if (!hasChecker) break; | |
| } | |
| if (matches.every(m => m.length)) return matches.map(m=>m.join('\n')).join('\n'); | |
| return ''; | |
| } | |
| let folded = 0; | |
| let elm = document.querySelector('span[role="button"][aria-label*="較舊的郵件"]:not([tabindex])'); | |
| if (elm) folded = parseInt(elm.getAttribute('aria-label'), 10); | |
| let ls = document.querySelectorAll('[role="list"] > [role="listitem"]'); | |
| let total = folded + ls.length; | |
| if (elm) elm.click(); | |
| while (ls.length != total) { | |
| await sleep(100); | |
| ls = document.querySelectorAll('[role="list"] > [role="listitem"]'); | |
| console.log('Cannot get all mails'); | |
| } | |
| let result = await Promise.all(Array.from(ls).map(async function (elm) { | |
| let content; | |
| if (!elm.querySelector('[data-message-id][style=""]')) { // not opened | |
| elm.querySelector('table').click(); | |
| content = elm.querySelector('[class*="msg"]'); | |
| while (!content) { | |
| await sleep(100); | |
| content = elm.querySelector('[class*="msg"]'); | |
| console.log('Cannot find content container.', elm); | |
| } | |
| } else { | |
| content = elm.querySelector('[class*="msg"]'); | |
| } | |
| return format(content.innerText); | |
| })); | |
| result = result.filter(s => s).join('\n\n'); | |
| console.log(result); | |
| if (result) { | |
| try { | |
| await navigator.clipboard.writeText(result); | |
| alert('結果已複製到剪貼簿'); | |
| } catch { | |
| // DOM has to be focused when using the clipboard API | |
| alert('複製失敗,請點擊網頁後再試一次'); | |
| } | |
| } | |
| })() && undefined; // prevent from navigation | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment