github-update-forked

update forked repository

当前为 2019-12-03 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @author gaojr
// @namespace https://github.com/gaojr/tampermonkey-scripts
// @name:CN-zh_cn GitHub更新fork仓库
// @name github-update-forked
// @version 0.5
// @description update forked repository
// @license MIT
// @match https://github.com/*
// @require https://greasyfork.org/scripts/393085-commonsutil/code/CommonsUtil.js
// @grant none
// @connect github.com
// @icon https://github.githubassets.com/pinned-octocat.svg
// ==/UserScript==

const wlh = window.location.href;

/**
 * 是否为 forked 仓库
 * @return {boolean} 是否为 forked 仓库
 */
const isForked = function () {
  return !!_$('.fork-flag');
};

/**
 * 是否落后
 * @return {boolean} 是否落后
 */
const isBehind = function () {
  let content = _$('.branch-infobar').textContent;
  return content.indexOf('behind') !== -1
};

/**
 * 获取父项目信息
 * @return {*} 父项目信息
 */
const getParentInfo = function () {
  if (isForked) {
    let content = _$('.fork-flag a').textContent;
    return {
      user: content.split('/')[0],
      repository: content.split('/')[1]
    };
  }
  return {};
};

/**
 * 根据url获取分支
 * @param {string} url 地址
 * @return {string} 分支名
 */
const getBranch = function (url) {
  let index = url.lastIndexOf('/') + 1;
  return url.substr(index);
};

/**
 * 处理update按钮的链接
 * @param {Element} ele 元素
 */
const dealUpdateBtnHref = function (ele) {
  let parentInfo = getParentInfo();
  let href = ele.href.replace('pull/new', 'compare');
  let branch = getBranch(href);
  href += '...' + parentInfo.user + '/' + branch;
  ele.href = href;
};

(function () {
  let func = () => {
    const isRepository = /https:\/\/github\.com\/[^\/]*\/[^\/]*/;
    if (isRepository.test(wlh) && isForked() && isBehind()) {
      // 生成更新按钮
      let prBtn = _$('div.branch-infobar .muted-link');
      if (prBtn.textContent.indexOf('Pull request') === -1) {
        // 有合并请求时不生成按钮
        return;
      }
      let updateBtn = prBtn.outerHTML.replace('Pull request', 'update');
      prBtn.outerHTML = updateBtn + prBtn.outerHTML;
      // 绑定点击事件
      dealUpdateBtnHref(_$('div.branch-infobar .muted-link'));
    }
  };
  addToFuncMap('github-update-forked', func);
})();