瀏覽作者:

lex.xu

對資訊技術這方面非常有興趣,常常自學新技術來補齊自己的好奇心,解決問題的成就感是我繼續走向這條路的最大動力。

[JavaScript] $(window).on(‘resize’, Method) 在 Mobile 結果大不同

前幾天被Android與iOS的瀏覽器坑了,明明一個簡單的resize event卻有不同效果…

  • 程式碼
    <input type="text" />
    <script>
    	$(window).on('resize', function () {
    		alert('resize event');
    	});
    </script>
  • Android
    點擊input後會觸發虛擬鍵盤,當觸發虛擬鍵盤時瀏覽器會觸發Resize Event
  • iOS
    點擊input後會觸發虛擬鍵盤,當觸發虛擬鍵盤時瀏覽器不會觸發Resize Event

想不到吧 !! 我也想不到RRRRR !!!
這鳥問題害我卡了好久QQ

 

       

[C#] (免安裝)WMIScan v1.0 檢查是否有奇怪的腳本(hao123)

前提摘要:[Windows] 瀏覽器首頁被 hao123 綁架

我寫了一隻小程式可以檢查WMI裡面是否有奇怪的Script在排程,並顯示出怪異的Script讓使用者決定是否刪除

操作步驟:

  1. 下載 https://github.com/shuangrain/WMIScan/releases/download/v1.0/WMIScan.zip
  2. 解壓縮後開啟WMIScan.exe
  3. 點擊Scan後會在下方Show出可疑的Script
  4. 點選可疑的Script後點擊右邊的刪除
  5. 之後再清除有問題的lnk檔就沒問題囉 !!

 

GitHub:https://github.com/shuangrain/WMIScan

       

[Git] 常用指令總匯

分类 Git 命令 作用
配置 git config –global user.name “John Doe” 配置 Git 提交姓名
配置 git config –global user.email “[email protected] 配置 Git 提交邮箱
创建 git init 初始化当前目录为 Git 目录
创建 git add ./[file_name] 添加跟踪(所有)当前操作文件到 Git 管理暂存
创建 git clone [URL] 从 URL 仓库clone文件并建立 Git 管理
提交 git commit -m ‘提交记录’ 提交当前暂存区的文件并记录提交
提交 git status 查看文件 Git 状态
提交 git status -s/–short 查看文件 Git 状态(紧凑格式)
提交 .gitignore文件 文件中记录当前目录下不参与 git 跟踪的文件
比较 git diff 比较工作目录中当前文件和暂存区域快照之间的差异
比较 git diff –[cached/staged] 查看已暂存与当前已提交的文件差异
删除 git rm [file_name] 从当前 Git 仓库中删除某文件
删除 git rm -f [file_name] 从当前 Git 仓库中删除某文件(包括已修改但未暂存的文件
删除 git rm –cached [file_name] 从当前 Git 仓库中删除某文件不再跟踪(但保留在当前工作目录中)
移动 git mv [file_old_name] [file_new_name] 重命名/移动文件
日志 git log 查看提交日志
日志 git log -p -[N] 查看最近 [N] 次的提交日志和差异
日志 git log –stat 查看每次提交的简略的统计信息
日志 git log –graph 查看每次提交的图形化的统计信息
日志 git log –pretty=[oneline/short/full/fuller] 指定使用不同于默认格式的方式展示提交历史
日志 git log –pretty=format:”%h – %an, %ar : %s” 定制要显示的记录格式[^定制格式说明]
撤销 git commit –amend 撤销上次提交,并重新提交(用当前状态替换上次提交)
撤销 git reset HEAD [file] 取消某文件暂存(撤销某文件的 git add 操作)
撤销 git checkout – [file] 撤销对工作目录下未提交的文件的修改
远程 git remote [-v] 查看远端仓库名[及其 URL]
远程 git remote add [shortname] [url] 添加远端仓库并命名
远程 git fetch [remote-name] 拉取远端所有内容以及更新至本地引用
远程 git push [remote-name] [branch-name] 推送本地[branch-name]分支至远端[remote-name]
远程 git remote show [remote-name] 查看远端仓库详细状态
远程 git remote rename [remote-old-name] [remote-new-name] 重命名远端仓库
远程 git remote rm [remote-name] 移除远端仓库
标签 git tag 列出所有标签
标签 git tag -a [tag-name] -m “[annotation]” 创建附注标签
标签 git show [tag-name] 列出指定标签详细信息
标签 git tag [tag-name] 创建轻量标签
标签 git tag -a [tag-name] [commit-sha-1] 对某次提交(以 sha-1 码形式)补充标签
标签 git push [branch-name] [tag-name]/[–tags] 将本地的[某个/全部]标签全部推送至远端
标签 git checkout -b [branchname] [tagname] 在特定标签上创建一个新的分支
别名 git config –global alias.[alias] [command] 创建一个[command]的[alias]别名(git config --global alias.unstage 'reset HEAD --')
分支 git branch [branch-name] 创建分支(但是不会切换至此分支)
分支 git checkout [-b] [branch-name] [创建并]检出此分支为当前分支
分支 git merge [branch-name] 在当前分支处合并某分支
分支 git branch -[d/D] [branch-name] [删除/强制删除]某分支
分支 git branch [-v/-vv] 查看所有分支[以及它们最后一次提交/以及与本地缓存下的远端分支的联系]
分支 git branch –merged/–no-merged 查看[已经/尚未]合并到当前分支的分支
分支 git checkout -b [branch] [remotename]/[branch] 从远端检出并创建本地分支同时默认跟踪远端此分支
分支 git checkout –track [remotename]/[branch] 从远端检出并创建本地同名分支同时默认跟踪远端此分支
分支 git branch [-u/–set-upstream-to] [remotename]/[branch] 设置或更新当前分支跟踪远端分支
分支 git fetch –all 拉取远端分支所有更新至本地缓存
分支 git pull/[git fetch THEN git merge] 拉取远端分支并合并
分支 git push origin –delete [branch-name] 删除远端分支
变基 git rebase[目前使用不多,需要深入理解] 不要对在你的仓库外有副本的分支执行变基,详情参见此处
储藏 git stash/git stash save 储藏当前未提交的修改并回复工作目录至提交状态
储藏 git stash list 查看当前储藏栈
储藏 git stash apply [stash@{2}] 应用[最近的一次/指定的某次]储藏
储藏 git stash drop [stash@{0}] 在储藏栈中删除[指定的某次]储藏
储藏 git stash pop 在储藏栈中应用最近的一次储藏并删除
储藏 git stash –keep-index c储藏工作目录中未暂存的东西(已经通过 add 暂存的不储藏)
储藏 git stash -u 储藏当前未提交的修改(包括未加入 Git 跟踪的文件)
储藏 git stash branch [branch-name] 依据上次储藏的内容创建一个新的分支并应用上次的储藏(将上次储藏前的工作目录应用于一个新的分支上处理)
清理 git clean 清理当前目录中所有未被跟踪的文件且不能恢复(不包括 .gitignore 文件中的列表)
清理 git stash –all 清理当前目录中所有未被跟踪的文件并暂存起来
清理 git clean -f -d 移除工作目录中所有未追踪的文件以及空的子目录
清理 git clean -d -n 使用-n而不是-d表示做一次演习然后告诉你将要移除什么
清理 git clean -f -d -x 移除工作目录中所有未追踪的文件以及空的子目录(包括 .gitignore 文件中的列表)
清理 git clean -n -d -x 演习移除工作目录中所有未追踪的文件以及空的子目录(包括 .gitignore 文件中的列表)
重置 git reset[需要深入理解] 详情参见此处**
合并 git merge –abort 在 merge 操作后需要处理冲突时使用此命令撤销此合并
合并 git reset –hard HEAD 无论当前什么状态,直接恢复至本分支上次提交状态(这会将清除工作目录中的所有内容)
合并 git reset –hard HEAD~ 恢复至本分支上次提交的父状态(最近一次提交的父节点提交)
合并 git reset/revert 更多合并撤销详情见此处
  • 文件 .gitignore 的格式规范如下:
    • 所有空行或者以 # 开头的行都会被 Git 忽略。
    • 可以使用标准的 glob 模式匹配。
    • 匹配模式可以以(/)开头防止递归。
    • 匹配模式可以以(/)结尾指定目录。
    • 要忽略指定模式以外的文件或目录,可以在模式前加上惊叹号(!)取反。

    [^定制格式说明]: 格式详情见git log –pretty=format 常用的选项

來源:Git 常用命令总结

       

[Windows] 瀏覽器首頁被 hao123 綁架

前幾天再找百度乾淨雲的時候不小心中了hao123了
自從用了hao123 再也沒被奇怪的網站綁架了呢 !!! 才不是勒 !!!
稍微看了一下原來是瀏覽器的開啟路徑被動了手腳
本來以為很簡單…但最後費了一些力氣才終於解決…

 

病毒對於我們資訊人來說還不就是登入檔or排程or執行檔or註冊檔or服務之類的東西,沒想到這次我碰壁了,竟然在每個地方都找不到任何蹤跡 !!!
而我的瀏覽器連結就這樣定時的被修改路徑…

身為一個資訊人馬上就發揮了看家本領Google求大神了 !!
發現原來Win還有WMI Event這個鬼東西
也藏太深了吧 !!!  ( 受教了

解決方法如下:

  1. 下載 WMITools (官方網址掛了,這裡我有上傳備份,解壓縮密碼:https://exfast.me/)
  2. 安裝後右鍵使用管理員開啟
  3. 點選左上角
  4. 不用改直接按OK
  5. 直接按OK
  6. 兇手就是你啦 !!!!
    對著這個王八蛋點右鍵刪除吧 !!
  7. 最後清除所有lnk的hao網址即可 (圖片中為RogueKiller免費版)

 

參考:

  1. 中招了——WMI脚本劫持浏览器
  2. 瀏覽器首頁以「篡改瀏覽器捷徑方式」被綁架 – 復原雜記
       

[JavaScript] 在特定瀏覽器中使用 history.back() 或 history.go(-1) 皆無反應?

今天有個一個需求如下:

點擊連結後回到上一頁,這麼簡單的東西竟然有雷 !!!

首先我們來寫個簡單的範例:

<script>
    function clickEvent_back() {
        history.back();
    }
    function clickEvent_go() {
        history.go(-1);
    }

    $('a').on('click', function (event) { 
        event.preventdefault()
        return false;
    })
</script>

<h1>onclick</h1>
<a href="#" onclick="history.back()">history.back()</a>
<a href="#" onclick="history.go(-1)">history.go(-1)</a>
<h1>href</h1>
<a href="javascript: history.back()">history.back()</a>
<a href="javascript: history.go(-1)">history.go(-1)</a>
<h1>onclick call method</h1>
<a href="#" onclick="clickEvent_back()">history.back()</a>
<a href="#" onclick="clickEvent_go()">history.go(-1)</a>
<h1>href call method</h1>
<a href="javascript: clickEvent_back()">history.back()</a>
<a href="javascript: clickEvent_go()">history.go(-1)</a>

畫面如圖:

 

不管點擊哪種onclick觸發的返回上一頁皆無反應,但有趣的是寫在href裡面的卻都正常

有問題的瀏覽器:

  1. Safari
  2. Safari – WebView (in app)

正常運作的瀏覽器:

  1. Chrome
  2. Chrome (mobile app)
  3. Edge
  4. IE 11

看來Chrome比較聽話 …

       

[.NET Core] 將 NET Core 部署至 Ubuntu Server 16.04

最近裝了個Linux Server想來玩玩看NET Core並佈署到上面,結果發現好多眉眉角角,網路上的文件似乎有點舊讓我一直撞牆撞不停,經過了幾個小時的煎熬最後終於成功了,這裡筆記一下方便下次使用…

首先按照 官網 提供的步驟進行安裝如下:

  1. 加入apt-get來源
    sudo sh -c 'echo "deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/dotnet-release/ xenial main" > /etc/apt/sources.list.d/dotnetdev.list'
    sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 417A0893
    sudo apt-get update
  2. 開始安裝
    sudo apt-get install dotnet-dev-1.0.4
  3. 建立一個空資料夾
    mkdir HelloWebApp
    cd HelloWebApp
  4. 初始化MVC專案
    dotnet new mvc

    Templates清單:

    Templates                 Short Name      Language      Tags
    ----------------------------------------------------------------------
    Console Application       console         [C#], F#      Common/Console
    Class library             classlib        [C#], F#      Common/Library
    Unit Test Project         mstest          [C#], F#      Test/MSTest
    xUnit Test Project        xunit           [C#], F#      Test/xUnit
    ASP.NET Core Empty        web             [C#]          Web/Empty
    ASP.NET Core Web App      mvc             [C#], F#      Web/MVC
    ASP.NET Core Web API      webapi          [C#]          Web/WebAPI
    Solution File             sln                           Solution
  5. 檢查套件是否遺失並安裝
    dotnet restore
  6. 安裝Apache反向代理(Reverse-Proxy)
    sudo a2enmod proxy
    sudo a2enmod proxy_http
    sudo a2enmod proxy_ajp
    sudo a2enmod rewrite
    sudo a2enmod deflate
    sudo a2enmod headers
    sudo a2enmod proxy_balancer
    sudo a2enmod proxy_connect
    sudo a2enmod proxy_html
  7. 設定000-default.conf
    <VirtualHost *:80>
    	ProxyPreserveHost On
    	ProxyPass / http://127.0.0.1:5000/
    	ProxyPassReverse / http://127.0.0.1:5000/
    </VirtualHost>
  8. 重新啟動Apach並啟動.NET Core專案
    sudo service apache2 restart
    dotnet run
  9. 完成 !!
    2017-03-14-00_24_31-home-page-hellowebapp

 

 

  • 2017/05/24 開啟啟動&背景執行
    新增:/etc/systemd/system/kestrel-hellomvc.service

    [Unit]
    Description = https://blog.exfast.me/
    
    [Service]
    WorkingDirectory = /home/user/HelloWebApp/
    ExecStart = /usr/bin/dotnet run /home/user/HelloWebApp/HelloWebApp.dll
    Restart = always
    # Restart service after 10 seconds if dotnet service crashes
    RestartSec = 10
    SyslogIdentifier = dotnet-example
    User = user
    Environment = ASPNETCORE_ENVIRONMENT = Production 
    
    [Install]
    WantedBy = multi-user.target

    指令:

    sudo systemctl stop kestrel-hellomvc
    sudo systemctl disable kestrel-hellomvc
    sudo systemctl enable kestrel-hellomvc
    sudo systemctl start kestrel-hellomvc
    sudo systemctl status kestrel-hellomvc

     

來源:

  1. https://www.microsoft.com/net/core#linuxubuntu
  2. How To Use Apache HTTP Server As Reverse-Proxy Using mod_proxy Extension
  3. Asp.Net Core 发布和部署( MacOS + Linux + Nginx )
  4. Publish to a Linux Production Environment
  5. Using Apache web server as a reverse proxy
       

[C#][LeetCode][Easy] 167. Two Sum II – Input array is sorted

心得

這題我本來是想用兩個for迴圈解決的

public class Solution {
    public int[] TwoSum(int[] numbers, int target) {
        for (int i = 0; i < numbers.Length; i++)
        {
            for (int j = numbers.Length - 1; j > i; j--)
            {
                if (numbers[i] + numbers[j] == target)
                {
                    return new int[] { i + 1, j + 1 };
                }
            }
        }

        throw new Exception();
    }
}

但發現會Time Limit Exceeded於是乎看了一下Solutions他們是用一個while迴圈來解答,題目要求index1<index2且不能使用重複的元素,所以這題如果用while的話宣告兩個變數分別是left與right就可以只用一個迴圈就解決。

問題

Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution and you may not use the same element twice.

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2

答案

public class Solution {
    public int[] TwoSum(int[] numbers, int target) {
        int left = 0;
        int right = numbers.Length - 1;
        while (numbers[left] + numbers[right] != target)
        {
            if (numbers[left] + numbers[right] > target)
            {
                right--;
            }
            else
            {
                left++;
            }
        }

        return new int[] { left + 1, right + 1 };
    }
}

 

       

[C#][LeetCode][Easy] 409. Longest Palindrome

心得

獲得字串並判斷字串內的字母能組成最長的回文大小為多少,例如傳入asdasdAAAvzxh則答案為9,字串為asdAvAdsa,其中v可替換成任意一位使用字母。

問題

Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.

This is case sensitive, for example "Aa" is not considered a palindrome here.

Note:
Assume the length of given string will not exceed 1,010.

Example:

Input:
"abccccdd"

Output:
7

Explanation:
One longest palindrome that can be built is "dccaccd", whose length is 7.

答案

public class Solution {
    public int LongestPalindrome(string s) {
        int ans = 0;
        int[] lower = new int[26];
        int[] upper = new int[26];
        foreach (var item in s)
        {
            if (item > 'Z')
            {
                lower[item - 'a']++;
            }
            else
            {
                upper[item - 'A']++;
            }

        }
        for (int i = 0; i < lower.Length; i++)
        {
            ans += lower[i] / 2;
            ans += upper[i] / 2;
        }

        ans = ans * 2;
        return ans < s.Length ? ans + 1 : ans;
    }
}

 

       

[C#][LeetCode][Easy] 349. Intersection of Two Arrays

心得

題目要求找出兩個int[]交集的數字有哪些且不能重複並輸出。

問題

Given two arrays, write a function to compute their intersection.

Example:
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].

Note:

  • Each element in the result must be unique.
  • The result can be in any order.

答案

  1. .NET提供的方法
    public class Solution {
        public int[] Intersection(int[] nums1, int[] nums2) {
            return nums1.Intersect(nums2).ToArray();
        }
    }
  2. 上面那個方法太賤了,於是乎自己硬幹了一次
    public class Solution {
        public int[] Intersection(int[] nums1, int[] nums2) {
            List<int> list = new List<int>();
            Array.Sort(nums1);
            Array.Sort(nums2);
            for (int i = 0; i < nums1.Length; i++)
            {
                for (int j = 0; j < nums2.Length; j++)
                {
                    if (nums1[i] == nums2[j])
                    {
                        if (list.Count == 0 || list[list.Count - 1] < nums1[i])
                        {
                            list.Add(nums1[i]);
                        }
                        break;
                    }
                }
            }
    
            return list.ToArray();
        }
    }

     

       

[C#][LeetCode][Easy] 383. Ransom Note

心得

這題要比對字串變數ransomNote是否存在於字串變數magazine裡。

問題

Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom
note can be constructed from the magazines ; otherwise, it will return false.

Each letter in the magazine string can only be used once in your ransom note.

Note:
You may assume that both strings contain only lowercase letters.

canConstruct("a", "b") -> false
canConstruct("aa", "ab") -> false
canConstruct("aa", "aab") -> true

答案

  1. 先將所有字母的數量統計在比較
    public class Solution {
        public bool CanConstruct(string ransomNote, string magazine) {
            if (ransomNote.Length > magazine.Length)
            {
                return false;
            }
    
            Dictionary<char, int> dty_1 = getCharCount(ransomNote);
            Dictionary<char, int> dty_2 = getCharCount(magazine);
    
            foreach (var item in dty_1)
            {
                int tmp = 0;
                if (!dty_2.TryGetValue(item.Key, out tmp) || item.Value > tmp)
                {
                    return false;
                }
            }
    
    
            return true;
        }
        
        private Dictionary<char, int> getCharCount(string str)
        {
            Dictionary<char, int> dty = new Dictionary<char, int>();
    
            while (str.Length > 0)
            {
                dty.Add(str[0], str.Count(x => x == str[0]));
                str = str.Replace(str[0].ToString(), "");
            }
    
            return dty;
        }
    }
  2. 方法1的簡化版
    public class Solution {
        public bool CanConstruct(string ransomNote, string magazine) {
            if (ransomNote.Length > magazine.Length)
            {
                return false;
            }
    
            while (ransomNote.Length > 0)
            {
                if (ransomNote.Count(x => x == ransomNote[0]) > magazine.Count(x => x == ransomNote[0]))
                {
                    return false;
                }
    
                ransomNote = ransomNote.Replace(ransomNote[0].ToString(), "");
            }
    
            return true;
        }
    }
  3. 這方法是在解答內看到的,解題思路與方法1差不多但是速度快了很多。
    public class Solution {
        public bool CanConstruct(string ransomNote, string magazine) {
            if (ransomNote.Length > magazine.Length)
            {
                return false;
            }
        
            int[] table = new int[26];
            foreach (var item in magazine)
            {
                table[item - 'a']++;
            }
            foreach (var item in ransomNote)
            {
                table[item - 'a']--;
                if (table[item - 'a'] < 0)
                {
                    return false;
                }
            }
        
            return true;
        }
    }