github-update-forked

update forked repository

اعتبارا من 02-12-2019. شاهد أحدث إصدار.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

ستحتاج إلى تثبيت إضافة مثل Stylus لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتتمكن من تثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

(لدي بالفعل مثبت أنماط للمستخدم، دعني أقم بتثبيته!)

// ==UserScript==
// @author gaojr
// @namespace https://github.com/gaojr/tampermonkey-scripts
// @name:CN-zh_cn GitHub更新fork仓库
// @name github-update-forked
// @version 0.1
// @description update forked repository
// @license MIT
// @match https://github.com/*
// @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 !!document.querySelector('.fork-flag');
};

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

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

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

};

/**
 * 更新
 * @param {*} 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 () {
  window.onload = function () {
    console.log('github-update-forked start!');
    const isRepository = /https:\/\/github\.com\/[^\/]*\/[^\/]*/;
    if (isRepository.test(wlh) && isForked() && isBehind()) {
      // 生成更新按钮
      let prBtn = document.querySelector('div.branch-infobar .muted-link');
      let updateBtn = prBtn.outerHTML.replace('Pull request', 'update');
      prBtn.outerHTML = updateBtn + prBtn.outerHTML;
      // 绑定点击事件
      dealUpdateBtnHref(document.querySelector('div.branch-infobar .muted-link'));
    }
    console.log('github-update-forked end!');
  }

})();