Browse Source

Add support for GitHub Enterprise (#450)

master
Federico Brigante 7 years ago
committed by Sindre Sorhus
parent
commit
1bd2494431
  1. 3
      extension/background.js
  2. 10
      extension/content.js
  3. 20
      extension/manifest.json
  4. 6
      extension/options/index.css
  5. 12
      extension/options/index.html
  6. 25
      extension/options/index.js
  7. 105
      extension/vendor/webext-dynamic-content-scripts.js

3
extension/background.js

@ -0,0 +1,3 @@
/* globals injectContentScripts */
injectContentScripts();

10
extension/content.js

@ -467,7 +467,7 @@ $(document).on('pjax:end', () => {
} }
}); });
document.addEventListener('DOMContentLoaded', () => { function init() {
const username = getUsername(); const username = getUsername();
markUnread.unreadIndicatorIcon(); markUnread.unreadIndicatorIcon();
@ -601,8 +601,14 @@ document.addEventListener('DOMContentLoaded', () => {
} }
}); });
} }
}); }
if (!pageDetect.isGist()) { if (!pageDetect.isGist()) {
addTrendingMenuItem(); addTrendingMenuItem();
} }
if (document.readyState === 'complete') {
init();
} else {
document.addEventListener('DOMContentLoaded', init);
}

20
extension/manifest.json

@ -8,9 +8,24 @@
"permissions": [ "permissions": [
"storage" "storage"
], ],
"optional_permissions": [
"http://*/*",
"https://*/*"
],
"icons": { "icons": {
"128": "icon.png" "128": "icon.png"
}, },
"options_ui": {
"chrome_style": true,
"page": "options/index.html"
},
"background": {
"scripts": [
"vendor/webext-dynamic-content-scripts.js",
"background.js"
],
"persistent": false
},
"content_scripts": [ "content_scripts": [
{ {
"run_at": "document_start", "run_at": "document_start",
@ -26,6 +41,7 @@
"vendor/jquery.slim.min.js", "vendor/jquery.slim.min.js",
"vendor/element-ready.js", "vendor/element-ready.js",
"vendor/gh-injection.js", "vendor/gh-injection.js",
"vendor/webext-dynamic-content-scripts.js",
"util.js", "util.js",
"page-detect.js", "page-detect.js",
"icons.js", "icons.js",
@ -36,10 +52,10 @@
"copy-gist.js", "copy-gist.js",
"copy-on-y.js", "copy-on-y.js",
"show-names.js", "show-names.js",
"content.js",
"mark-unread.js", "mark-unread.js",
"linkify-urls-in-code.js", "linkify-urls-in-code.js",
"upload-button.js" "upload-button.js",
"content.js"
] ]
} }
] ]

6
extension/options/index.css

@ -0,0 +1,6 @@
html {
font-family: sans-serif;
}
input {
font: inherit;
}

12
extension/options/index.html

@ -0,0 +1,12 @@
<!doctype html>
<meta charset="utf-8">
<title>Refined GitHub options</title>
<link rel="stylesheet" href="index.css">
<form id="custom-domain">
<h2>GitHub Enterprise support</h2>
<p>
<input type="url" placeholder="https://github.yourdomain.com" size="30" required id="custom-domain-origin">
<button type="submit">Authorize</button>
</p>
</form>
<script src="index.js"></script>

25
extension/options/index.js

@ -0,0 +1,25 @@
const cdForm = document.querySelector('#custom-domain');
const cdInput = document.querySelector('#custom-domain-origin');
if (!chrome.permissions) {
cdForm.disabled = true;
cdForm.querySelector('p').textContent = 'Your browser doesn’t support the required Permission API.';
}
cdForm.addEventListener('submit', event => {
event.preventDefault();
const origin = new URL(cdInput.value).origin;
if (origin) {
chrome.permissions.request({
origins: [
`${origin}/*`
]
}, granted => {
if (granted) {
cdForm.reset();
}
});
}
});

105
extension/vendor/webext-dynamic-content-scripts.js

@ -0,0 +1,105 @@
// https://github.com/bfred-it/webext-dynamic-content-scripts 2.0.0
var injectContentScripts = (function () {
'use strict';
function createCommonjsModule(fn, module) {
return module = { exports: {} }, fn(module, module.exports), module.exports;
}
var webextContentScriptPing = createCommonjsModule(function (module, exports) {
// https://github.com/bfred-it/webext-content-script-ping
function pingContentScript(tab) {
return new Promise((resolve, reject) => {
setTimeout(reject, 300);
chrome.tabs.sendMessage(tab.id || tab, chrome.runtime.id, {
// Only the main frame is necessary;
// if that isn't loaded, no other iframe is
frameId: 0
}, response => {
if (response === chrome.runtime.id) {
resolve();
} else {
reject();
}
});
});
}
if (!chrome.runtime.getBackground) {
// Respond to pings
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request === chrome.runtime.id) {
sendResponse(chrome.runtime.id);
}
});
}
if (typeof exports === 'object') {
exports.pingContentScript = pingContentScript;
}
});
var pingContentScript = webextContentScriptPing.pingContentScript;
function logRuntimeErrors() {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
}
}
async function injectContentScript(script, tabId) {
const allFrames = script.all_frames;
const runAt = script.run_at;
script.css.forEach(file => chrome.tabs.insertCSS(tabId, {file, allFrames, runAt}, logRuntimeErrors));
script.js.forEach(file => chrome.tabs.executeScript(tabId, {file, allFrames, runAt}, logRuntimeErrors));
}
async function injectContentScripts(tab) {
// Get the tab object if we don't have it already
if (!tab.id) {
tab = await new Promise(resolve => chrome.tabs.get(tab, resolve));
logRuntimeErrors();
}
// If we don't have the URL, we definitely can't access it.
if (!tab.url) {
return;
}
// We might just get the url because of the `tabs` permission,
// not necessarily because we have access to the origin.
// This will explicitly verify this permission.
const isPermitted = await new Promise(resolve => chrome.permissions.contains({
origins: [new URL(tab.url).origin + '/']
}, resolve));
logRuntimeErrors();
if (!isPermitted) {
return;
}
// Exit if already injected
try {
return await pingContentScript(tab.id || tab);
} catch (err) {}
chrome.runtime.getManifest().content_scripts.forEach(s => injectContentScript(s, tab.id));
}
var index = function (tab = false) {
if (tab === false) {
chrome.tabs.onUpdated.addListener((tabId, {status}) => {
if (status === 'loading') {
injectContentScripts(tabId);
}
});
} else {
injectContentScripts(tab);
}
};
return index;
}());
Loading…
Cancel
Save