Greasy Fork

更好地一键开启github1s页面

更好地支持文件级用打开github1s

当前为 2022-11-12 提交的版本,查看 最新版本

// ==UserScript==
// @name        更好地一键开启github1s页面
// @namespace   github
// @author      cjm
// @description 更好地支持文件级用打开github1s
// @include     https://github.com/
// @match       https://github.com/*
// @version     1.0.0
// @license MIT
// ==/UserScript==

function func() {
    setTimeout(()=>{
        const href = `https://github1s.com${location.pathname}`
        const ele = document.getElementById('wocao');
        if(ele){
            ele.href = href;
        }else{
            const btn = `<a id="wocao" class="btn ml-2 d-none d-md-block" style="background: #8c7ae6; color: #fff;" target="_blank" href="${href}">` + '使用 github1s 打开' + '</a>'
            document.querySelector('#repository-container-header').insertAdjacentHTML('beforeBegin', btn)
        }
    }
    , 1000)
}
func();

//修改native以拦截popstate事件
var pushState = history.pushState;
history.pushState = function() {
    var ret = pushState.apply(history, arguments);
    window.dispatchEvent(new Event("pushstate"));
    window.dispatchEvent(new Event("locationchangefathom"));
    return ret;
}

window.addEventListener("popstate", function() {
    window.dispatchEvent(new Event("locationchangefathom"))
});
window.addEventListener("locationchangefathom", trackPageview)
function trackPageview() {
    func();
}