Hexo high一下小功能

額外一點(diǎn)小功能可以為博客增光添彩。

一、引言

博客建立之初,有各種奇奇怪怪的想法,有網(wǎng)上找相關(guān)教程看到的,也有自己突發(fā)奇想的,這里介紹一下high一下功能。

二、high 一下源碼

<li> <a title="把這個(gè)鏈接拖到你的Chrome收藏夾工具欄中" href='javascript:(function() {
    function c() {
        var e = document.createElement("link");
        e.setAttribute("type", "text/css");
        e.setAttribute("rel", "stylesheet");
        e.setAttribute("href", f);
        e.setAttribute("class", l);
        document.body.appendChild(e)
    }
 
    function h() {
        var e = document.getElementsByClassName(l);
        for (var t = 0; t < e.length; t++) {
            document.body.removeChild(e[t])
        }
    }
 
    function p() {
        var e = document.createElement("div");
        e.setAttribute("class", a);
        document.body.appendChild(e);
        setTimeout(function() {
            document.body.removeChild(e)
        }, 100)
    }
 
    function d(e) {
        return {
            height : e.offsetHeight,
            width : e.offsetWidth
        }
    }
 
    function v(i) {
        var s = d(i);
        return s.height > e && s.height < n && s.width > t && s.width < r
    }
 
    function m(e) {
        var t = e;
        var n = 0;
        while (!!t) {
            n += t.offsetTop;
            t = t.offsetParent
        }
        return n
    }
 
    function g() {
        var e = document.documentElement;
        if (!!window.innerWidth) {
            return window.innerHeight
        } else if (e && !isNaN(e.clientHeight)) {
            return e.clientHeight
        }
        return 0
    }
 
    function y() {
        if (window.pageYOffset) {
            return window.pageYOffset
        }
        return Math.max(document.documentElement.scrollTop, document.body.scrollTop)
    }
 
    function E(e) {
        var t = m(e);
        return t >= w && t <= b + w
    }
 
    function S() {
        var e = document.createElement("audio");
        e.setAttribute("class", l);
        e.src = i;
        e.loop = false;
        e.addEventListener("canplay", function() {
            setTimeout(function() {
                x(k)
            }, 500);
            setTimeout(function() {
                N();
                p();
                for (var e = 0; e < O.length; e++) {
                    T(O[e])
                }
            }, 15500)
        }, true);
        e.addEventListener("ended", function() {
            N();
            h()
        }, true);
        e.innerHTML = " <p>If you are reading this, it is because your browser does not support the audio element. We recommend that you get a new browser.</p> <p>";
        document.body.appendChild(e);
        e.play()
    }
 
    function x(e) {
        e.className += " " + s + " " + o
    }
 
    function T(e) {
        e.className += " " + s + " " + u[Math.floor(Math.random() * u.length)]
    }
 
    function N() {
        var e = document.getElementsByClassName(s);
        var t = new RegExp("\\b" + s + "\\b");
        for (var n = 0; n < e.length; ) {
            e[n].className = e[n].className.replace(t, "")
        }
    }
 
    var e = 30;
    var t = 30;
    var n = 350;
    var r = 350;
    var i = "http://s3.amazonaws.com/moovweb-marketing/playground/harlem-shake.mp3";
    var s = "mw-harlem_shake_me";
    var o = "im_first";
    var u = ["im_drunk", "im_baked", "im_trippin", "im_blown"];
    var a = "mw-strobe_light";
    var f = "http://s3.amazonaws.com/moovweb-marketing/playground/harlem-shake-style.css";
    var l = "mw_added_css";
    var b = g();
    var w = y();
    var C = document.getElementsByTagName("*");
    var k = null;
    for (var L = 0; L < C.length; L++) {
        var A = C[L];
        if (v(A)) {
            if (E(A)) {
                k = A;
                break
            }
        }
    }
    if (A === null) {
        console.warn("Could not find a node of the right size. Please try a different page.");
        return
    }
    c();
    S();
    var O = [];
    for (var L = 0; L < C.length; L++) {
        var A = C[L];
        if (v(A)) {
            O.push(A)
        }
    }
})()    '>High一下</a> </li>

網(wǎng)上找的教程是直接把這段代碼當(dāng)成一個(gè)菜單項(xiàng)加在博客主題配置的_config.yml文件中

menu:
  high: javascript:void(0)
menu_icons:
  high: play

像這種格式添加,這里只是舉個(gè)例子,實(shí)際操作一定要找到對應(yīng)的模塊添加。(先說明一下個(gè)人使用的是NexT主題)第一個(gè) high 的javascript:void(0)是指a標(biāo)簽的跳轉(zhuǎn)鏈接,其實(shí)這里的功能就是為了讓它不跳轉(zhuǎn)的,另外還有一個(gè)功能,可以把空鏈接的a標(biāo)簽懸浮變成手型的鼠標(biāo)。第二個(gè) high對應(yīng)的play是展示的圖標(biāo),NexT主題使用的是fontawesome圖標(biāo),這里的play就是其對應(yīng)的圖標(biāo),想自己 diy 一下就去fontawesome上找個(gè)自己喜歡的。

這樣的方法簡單粗暴有效,并且還有一個(gè)好處,可以直接把這個(gè)鏈接保存成瀏覽器的書簽,這樣就能在任何網(wǎng)頁都能夠調(diào)用這個(gè) High一下 的方法。且不評價(jià)這個(gè)方式好不好,但是如果這個(gè)功能就這樣結(jié)束,那就太沒意思了。所以,得改。

三、high 一下獨(dú)立 cdn

最初的想法是把high一下獨(dú)立成一個(gè)js文件,方便使用 cdn 來調(diào)用。這個(gè)挺簡單,js 提取出來成為一個(gè)函數(shù),然后給特定的元素綁定點(diǎn)擊觸發(fā)的事件即可。NexT主題加載外部 js 文件在 layout->_scripts->vendors.swg中,我模仿了加載fancybox的方式,添加了這段代碼:

{% if theme.high %}
  {% set js_vendors.high  = 'high/high.js?v=1.0.0' %}
{% endif %}

這樣的處理就可以在主題的配置文件中使用配置選擇是否打開high一下功能,現(xiàn)在就還需要把high.js放到對應(yīng)的文件夾中,我這邊的路徑是next->source->vendors->high->high.js。后來又發(fā)現(xiàn)high.js的源碼中還有著這么一段:

var f = "http://s3.amazonaws.com/moovweb-marketing/playground/harlem-shake-style.css";

外部還引用了一個(gè)亞馬遜云的 css 文件,兵來將擋水來土掩,css文件也要轉(zhuǎn)移:

var f = "/vendors/high/high.css";

css 放到和 js 同一個(gè)目錄下。
源碼里還有這么一段:

var i = musicUrl || "http://s3.amazonaws.com/moovweb-marketing/playground/harlem-shake.mp3";

這個(gè)就是播放的歌曲地址里,這個(gè)也可以隨意改。
但是!但是!但是!重要的事情提醒三次,這個(gè) js 是專門根據(jù)這首曲子寫的,我嘗試換了其他的曲子,效果都不理想,所以就只能將就用這首曲子了。

四、high 一下自定義頁面歌曲

就這樣就結(jié)束還是有點(diǎn)不甘心,我是希望能做到每個(gè)頁面都能夠自定義播放的曲子。雖然其他曲子都對不上這個(gè) js的節(jié)奏,但是這個(gè)想法的創(chuàng)意很棒,必須得實(shí)現(xiàn)!
首先是在需要自定義曲子的文章中加上:

<div id="musicUrl" url="***"><div>

然后最終版high.js代碼是這樣:

document.getElementsByClassName('menu-item-high')[0].addEventListener('click', (function(){
    var play = false;
    var musicUrl = document.getElementById('musicUrl');
    if(musicUrl){
        musicUrl = musicUrl.getAttribute('url');
    }else{
        musicUrl = null;
    }
    return function(){
        if(play){
            console.log('Enjoying');
            return;
        }
        play = true;
        function c() {
            var e = document.createElement("link");
            e.setAttribute("type", "text/css");
            e.setAttribute("rel", "stylesheet");
            e.setAttribute("href", f);
            e.setAttribute("class", l);
            document.body.appendChild(e)
        }
         
        function h() {
            var e = document.getElementsByClassName(l);
            for (var t = 0; t < e.length; t++) {
                document.body.removeChild(e[t])
            }
        }
        
        function p() {
            var e = document.createElement("div");
            e.setAttribute("class", a);
            document.body.appendChild(e);
            setTimeout(function() {
                document.body.removeChild(e)
            }, 100)
        }
         
        function d(e) {
            return {
                height: e.offsetHeight,
                width: e.offsetWidth
            }
        }
         
        function v(i) {
            var s = d(i);
            return s.height > e && s.height < n && s.width > t && s.width < r
        }
         
        function m(e) {
            var t = e;
            var n = 0;
            while (!!t) {
                n += t.offsetTop;
                t = t.offsetParent
            }
            return n
        }
         
        function g() {
            var e = document.documentElement;
            if (!!window.innerWidth) {
                return window.innerHeight
            } else if (e && !isNaN(e.clientHeight)) {
                return e.clientHeight
            }
            return 0
        }
        
        function y() {
            if (window.pageYOffset) {
                return window.pageYOffset
            }
            return Math.max(document.documentElement.scrollTop, document.body.scrollTop)
        }
         
        function E(e) {
            var t = m(e);
            return t >= w && t <= b + w
        }
         
        function S() {
            var e = document.createElement("audio");
            e.setAttribute("class", l);
            e.src = i;
            e.loop = false;
            e.addEventListener("canplay", function() {
                setTimeout(function() {
                    x(k)
                }, 500);
                setTimeout(function() {
                    N();
                    p();
                    for (var e = 0; e < O.length; e++) {
                        T(O[e])
                    }
                }, 15500)
            }, true);
            e.addEventListener("ended", function() {
                N();
                h();
                paly = false;
            }, true);
            e.innerHTML = " <p>If you are reading this, it is because your browser does not support the audio element. We recommend that you get a new browser.</p> <p>";
            document.body.appendChild(e);
            e.play()
        }
         
        function x(e) {
            e.className += " " + s + " " + o
        }
         
        function T(e) {
            e.className += " " + s + " " + u[Math.floor(Math.random() * u.length)]
        }
        
        function N() {
            var e = document.getElementsByClassName(s);
            var t = new RegExp("\\b" + s + "\\b");
            for (var n = 0; n < e.length; ) {
                e[n].className = e[n].className.replace(t, "")
            }
        }
         
        var e = 30;
        var t = 30;
        var n = 350;
        var r = 350;
        var i = musicUrl || "http://s3.amazonaws.com/moovweb-marketing/playground/harlem-shake.mp3";
        var s = "mw-harlem_shake_me";
        var o = "im_first";
        var u = ["im_drunk", "im_baked", "im_trippin", "im_blown"];
        var a = "mw-strobe_light";
        var f = "/vendors/high/high.css";
        var l = "mw_added_css";
        var b = g();
        var w = y();
        var C = document.getElementsByTagName("*");
        var k = null;
        for (var L = 0; L < C.length; L++) {
            var A = C[L];
            if (v(A)) {
                if (E(A)) {
                    k = A;
                    break
                }
            }
        }
        if (A === null) {
            console.warn("Could not find a node of the right size. Please try a different page.");
            return
        }
        c();
        S();
        var O = [];
        for (var L = 0; L < C.length; L++) {
            var A = C[L];
            if (v(A)) {
                O.push(A)
            }
        }
    };
})(), false);

這樣就能自定義每篇文章的 high 的曲子都不一樣了,當(dāng)然也都純屬娛樂,不要過于較真。(因?yàn)橛型Χ嗯笥颜f我神經(jīng),所以就暫時(shí)先把這個(gè)功能關(guān)閉了)。

附上個(gè)人博客對應(yīng)博文地址:
http://lancelot_lewis.coding.me/2016/05/14/blog/hexo-high/

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,219評論 25 708
  • 這篇文章發(fā)表在我的個(gè)人博客上,為了更好的閱讀體驗(yàn),請戳這里:jmyblog > 首先聲明:這并不是一篇教程。 這不...
    誰把錢丟了閱讀 2,630評論 1 10
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,432評論 4 61
  • 這個(gè)世界上有一種情懷叫感恩有你。每每聽到時(shí)間管理課程里葉老師的感恩有你這句話時(shí),都會(huì)不由自主的產(chǎn)生敬畏。特別是最近...
    2組13號(hào)石志霞閱讀 390評論 0 0
  • 2017-10-12 姓名:徐祖德 公司:廣東思沃精密機(jī)械有限公司 230期_利他1組 272期_樂觀2組志工 【...
    徐祖德閱讀 106評論 0 0

友情鏈接更多精彩內(nèi)容