Skip to content
This repository has been archived by the owner on Jun 5, 2023. It is now read-only.

PageControlDelegator

jiajunGit edited this page Jul 24, 2017 · 7 revisions

Contents

  1. Description
  2. List of Analytics-locations that are currently being used
  3. What does PageControlDelegator do?
  4. Special notes

Description

The PageControlDelegator primarily decides which page controller should be called for a particular GitHub page by examining the value of the content attribute found on the meta tag element with name attribute of "analytics-location".

List of Analytics-locations that are currently being used

For new issues page:

/<user-name>/<repo-name>/issues/new

For existing issues page:

/<user-name>/<repo-name>/issues/show

For create new labels page:

/<user-name>/<repo-name>/labels/index

For new pull request page:

/<user-name>/<repo-name>/compare/show

For existing pull request page:

/<user-name>/<repo-name>/pull_requests/show

What does PageControlDelegator do?

1. It listens to the url change notification sent by the background page via chrome message passing API.

2. When a url change notification is received by PageControlDelegator, it checks the head of the DOM for the location of the page. The following is a sample code fragment that checks for the page location:

var location = document.head.querySelector("meta[name='analytics-location']");
if(!location) {
    return false;
}

var locationContent = location.getAttribute("content");
if(typeof(locationContent) !== "string"){
    return false;
}

switch(locationContent){
    case this.AnalyticsContentForNewIssue:
        return RunType.NEW_ISSUE;
    case this.AnalyticsContentForNewPullRequest:
        return RunType.NEW_PULL_REQUEST;
    case this.AnalyticsContentForNewLabels:
        return RunType.NEW_LABELS;
    case this.AnalyticsContentForExistingIssue:
        return RunType.EXISTING_ISSUE;
    case this.AnalyticsContentForExistingPullRequest:
        return RunType.EXISTING_PULL_REQUEST;
    default:
        break;
}

return RunType.NONE;

3. After determining which page the user has landed on, PageControlDelegator calls the respective page controller (For now, there are only two primary page controllers, namely IssuePageController which handles the issue page and LabelPageController which handles the create new labels page) to handle the page.

Special notes

  • PageControlDelegator resides on a content script so it does not have access to many chrome API(s). As such, if you require the use of those chrome API(s), you will have to notify the background page to run those API(s) and pass the results back to the content script using chrome messaging API.

  • Github uses HTML5 pushstate to update its pages so content scripts will not run on those pages and therefore you need to use chrome API(s) or listen to pushstate events to run your content scripts on those pages.