Skip to content

Commit 07af7cd

Browse files
allow user to enable on a domain. enables github enterprise
1 parent 0eb6e13 commit 07af7cd

File tree

5 files changed

+180
-3
lines changed

5 files changed

+180
-3
lines changed

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
"version": "1.0.0",
55
"description": "Show github contributor's stats (# of Issues/PRs)",
66
"main": "index.js",
7-
"dependencies": {},
7+
"dependencies": {
8+
"webext-domain-permission-toggle": "0.0.2",
9+
"webext-dynamic-content-scripts": "^5.0.1"
10+
},
811
"devDependencies": {
912
"chrome-webstore-upload-cli": "^1.0.3"
1013
},

src/background.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
/* global chrome */
1+
/* global chrome DCE DCS */
2+
3+
// GitHub Enterprise support
4+
DCE.addContextMenu();
5+
DCS.addToFutureTabs();
26

37
// launch options page on first run
48
chrome.runtime.onInstalled.addListener((details) => {

src/manifest.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,19 @@
1111
"permissions": [
1212
"https://github.com/*/*",
1313
"storage",
14-
"identity"
14+
"identity",
15+
"contextMenus",
16+
"activeTab"
17+
],
18+
"optional_permissions": [
19+
"http://*/*",
20+
"https://*/*"
1521
],
1622
"background": {
1723
"persistent": false,
1824
"scripts": [
25+
"vendor/webext-domain-permission-toggle.js",
26+
"vendor/webext-dynamic-content-scripts.js",
1927
"background.js"
2028
]
2129
},
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// https://github.com/bfred-it/webext-domain-permission-toggle
2+
3+
const DCE = {
4+
addContextMenu(options) {
5+
const contextMenuId = 'webext-domain-permission-toggle:add-permission';
6+
7+
const {name: extensionName} = chrome.runtime.getManifest();
8+
9+
options = Object.assign({
10+
title: `Enable ${extensionName} on this domain`,
11+
reloadOnSuccess: `Do you want to reload this page to apply ${extensionName}?`
12+
}, options);
13+
14+
// This has to happen onInstalled in Event Pages
15+
chrome.runtime.onInstalled.addListener(() => {
16+
chrome.contextMenus.create({
17+
id: contextMenuId,
18+
title: options.title,
19+
contexts: ['page_action', 'browser_action'],
20+
documentUrlPatterns: [
21+
'http://*/*',
22+
'https://*/*'
23+
]
24+
});
25+
});
26+
27+
chrome.contextMenus.onClicked.addListener(async ({menuItemId}, {tabId, url}) => {
28+
if (menuItemId !== contextMenuId) {
29+
return;
30+
}
31+
chrome.permissions.request({
32+
origins: [
33+
`${new URL(url).origin}/*`
34+
]
35+
}, granted => {
36+
if (chrome.runtime.lastError) {
37+
console.error(`Error: ${chrome.runtime.lastError.message}`);
38+
alert(`Error: ${chrome.runtime.lastError.message}`);
39+
} else if (granted && options.reloadOnSuccess && confirm(options.reloadOnSuccess)) {
40+
chrome.tabs.reload(tabId);
41+
}
42+
});
43+
});
44+
}
45+
};
46+
47+
if (typeof module === 'object') {
48+
module.exports = DCE;
49+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// https://github.com/bfred-it/webext-dynamic-content-scripts
2+
(function (global, factory) {
3+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
4+
typeof define === 'function' && define.amd ? define(['exports'], factory) :
5+
(factory((global.DCS = global.DCS || {})));
6+
}(this, (function (exports) { 'use strict';
7+
8+
function interopDefault(ex) {
9+
return ex && typeof ex === 'object' && 'default' in ex ? ex['default'] : ex;
10+
}
11+
12+
function createCommonjsModule(fn, module) {
13+
return module = { exports: {} }, fn(module, module.exports), module.exports;
14+
}
15+
16+
var webextContentScriptPing = createCommonjsModule(function (module) {
17+
// https://github.com/bfred-it/webext-content-script-ping
18+
19+
/**
20+
* Ping responder
21+
*/
22+
document.__webextContentScriptLoaded = true;
23+
24+
/**
25+
* Pinger
26+
*/
27+
function pingContentScript(tab) {
28+
return new Promise((resolve, reject) => {
29+
chrome.tabs.executeScript(tab.id || tab, {
30+
code: 'document.__webextContentScriptLoaded',
31+
runAt: 'document_start'
32+
}, hasScriptAlready => {
33+
if (chrome.runtime.lastError) {
34+
reject(chrome.runtime.lastError);
35+
} else {
36+
resolve(Boolean(hasScriptAlready[0]));
37+
}
38+
});
39+
});
40+
}
41+
42+
if (typeof module === 'object') {
43+
module.exports = pingContentScript;
44+
}
45+
});
46+
47+
var pingContentScript = interopDefault(webextContentScriptPing);
48+
49+
async function p(fn, ...args) {
50+
return new Promise((resolve, reject) => fn(...args, r => {
51+
if (chrome.runtime.lastError) {
52+
reject(chrome.runtime.lastError);
53+
} else {
54+
resolve(r);
55+
}
56+
}));
57+
}
58+
59+
async function addToTab(tab, contentScripts) {
60+
if (typeof tab !== 'object' && typeof tab !== 'number') {
61+
throw new TypeError('Specify a Tab or tabId');
62+
}
63+
64+
if (!contentScripts) {
65+
// Get all scripts from manifest.json
66+
contentScripts = chrome.runtime.getManifest().content_scripts;
67+
} else if (!Array.isArray(contentScripts)) {
68+
// Single script object, make it an array
69+
contentScripts = [contentScripts];
70+
}
71+
72+
try {
73+
const tabId = tab.id || tab;
74+
if (!await pingContentScript(tabId)) {
75+
const injections = [];
76+
for (const group of contentScripts) {
77+
const allFrames = group.all_frames;
78+
const runAt = group.run_at;
79+
for (const file of group.css) {
80+
injections.push(p(chrome.tabs.insertCSS, tabId, {file, allFrames, runAt}));
81+
}
82+
for (const file of group.js) {
83+
injections.push(p(chrome.tabs.executeScript, tabId, {file, allFrames, runAt}));
84+
}
85+
}
86+
return Promise.all(injections);
87+
}
88+
} catch (err) {
89+
// Probably the domain isn't permitted.
90+
// It's easier to catch this than do 2 queries
91+
}
92+
}
93+
94+
function addToFutureTabs(scripts) {
95+
chrome.tabs.onUpdated.addListener((tabId, {status}) => {
96+
if (status === 'loading') {
97+
addToTab(tabId, scripts);
98+
}
99+
});
100+
}
101+
102+
var index = {
103+
addToTab,
104+
addToFutureTabs
105+
};
106+
107+
exports.addToTab = addToTab;
108+
exports.addToFutureTabs = addToFutureTabs;
109+
exports['default'] = index;
110+
111+
Object.defineProperty(exports, '__esModule', { value: true });
112+
113+
})));

0 commit comments

Comments
 (0)