Chrome 動態書籤-執行Javascript

如何在 Chrome 上製作可以將javascript 寫進去的書籤

以下是個簡單的範例,會將目前頁面轉導到指定日期的

https://rl.fx678.com/date/日期.html

  • 在Chrome 開啟書籤管理頁面
  • 按右鍵>新增書籤>輸入名稱
  • 在下方網址列輸入以下字串
javascript: ! function () {function getDate(){     let objectDate = new Date();       let day = objectDate.getDate();     if(day < 10){         day = "0"+day;     }              let month = objectDate.getMonth();     month = month +1;           let year = objectDate.getFullYear();       return ""+year+month+day;      } window.location.href = "https://rl.fx678.com/date/"+getDate()+".html";}()
  • 儲存並關閉。

詳細內容為

javascript: ! 
function () {
    function getDate(){     
        let objectDate = new Date();       
        let day = objectDate.getDate();     
        if(day < 10){         
            day = "0"+day;     
        }              
        let month = objectDate.getMonth();     
        month = month +1;           
        let year = objectDate.getFullYear();       
        return ""+year+month+day;      
    } 
    window.location.href = "https://rl.fx678.com/date/"+getDate()+".html";
}()

開始先指定這是要使用javascript 執行的書籤,並指定一個會直接執行的隱函數。
裏面會將目前網址轉換成帶日期的版本,並打開

也可以利用此方法載入 jquery 來方便使用

javascript:
(
    function(e,s){
        e.src=s;
        e.onload=function(){
            jQuery.noConflict();
            $=jQuery;console.log('jQuery injected')
        };
        document.head.appendChild(e);
    }
)(
    document.createElement('script'),'http://code.jquery.com/jquery-latest.min.js'
)

寫法稍有不同,但都可以作為書籤使用,值得注意的是寫入書籤網址列的都必須變成一行,不能有換行符號

ex. 

javascript:(function(e,s){e.src=s;e.onload=function(){jQuery.noConflict();$=jQuery;console.log('jQuery injected')};document.head.appendChild(e);})(document.createElement('script'),'http://code.jquery.com/jquery-latest.min.js')

發佈留言