Skip to content

Commit

Permalink
fix delayed link + improve PJAX handling
Browse files Browse the repository at this point in the history
the addition of the "First commit" link is delayed for a minute
or so in many (easy to reproduce) cases. this appears to be due
to a bug in jQuery-onMutate. switching to another MutationObserver
(jquery.initialize) wrapper fixes it.

also: register the commit-bar observer on *all* GitHub pages
to ensure it's always registered on the page *before* a PJAX
transition to a repo page. PJAX transitions don't cause the
userscript to be reloaded, so we need to make sure the observer
is in place on a page *before* the transition, which is not the
case if we restrict the matched URLs to repo pages.
  • Loading branch information
chocolateboy committed Apr 9, 2018
1 parent 6388124 commit 9a547f9
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions github_first_commit.user.js
Expand Up @@ -4,11 +4,12 @@
// @author chocolateboy
// @copyright chocolateboy
// @namespace https://github.com/chocolateboy/userscripts
// @version 2.2.2
// @version 2.3.0
// @license GPL: http://www.gnu.org/copyleft/gpl.html
// @include https://github.com/*/*
// @include https://github.com/
// @include https://github.com/*
// @require https://code.jquery.com/jquery-3.3.1.min.js
// @require https://cdn.rawgit.com/eclecto/jQuery-onMutate/79bbb2b8caccabfc9b9ade046fe63f15f593fef6/src/jquery.onmutate.min.js
// @require https://cdn.rawgit.com/pie6k/jquery.initialize/16342abd3d411a20d35390f3e4c966ceb37ec43e/jquery.initialize.min.js
// @grant GM_log
// ==/UserScript==

Expand Down Expand Up @@ -67,7 +68,8 @@ function openFirstCommit (user, repo) {
}

// add the "First commit" link as the last child of the commit bar
function addLink ($commitBar) {
function addLink () {
const $commitBar = $(this)
const [user, repo] = $('meta[name="octolytics-dimension-repository_network_root_nwo"]')
.attr('content')
.split('/')
Expand Down Expand Up @@ -98,9 +100,6 @@ function addLink ($commitBar) {
//
// https://github.com/github/include-fragment-element
//
// jQuery-onMutate fires the callback immediately if the element already exists,
// jquery.initialize fires the callback immediately if the element already exists,
// so it handles both cases

// #js-repo-pjax-container is only created on repo homepages
// see here for more details: https://github.com/Mottie/GitHub-userscripts/wiki/How-to
$('#js-repo-pjax-container').onCreate(COMMIT_BAR, addLink, true /* multi */)
$.initialize(COMMIT_BAR, addLink)

0 comments on commit 9a547f9

Please sign in to comment.