Skip to content

Commit 35ae6f1

Browse files
committed
feat(extension): support Firefox
1 parent 6ded1de commit 35ae6f1

5 files changed

Lines changed: 68 additions & 141 deletions

File tree

popup.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* global chrome */
22

33
document.getElementById('settings-btn').addEventListener('click', function() {
4-
chrome.tabs.create({ url: 'chrome://extensions/?options=' + chrome.runtime.id });
4+
chrome.runtime.openOptionsPage();
55
});

src/inject.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@
22
* enhanced-github
33
* https://github.com/softvar/enhanced-github
44
*
5-
* Licensed MIT (c) Varun Malhotrs
5+
* Licensed MIT (c) Varun Malhotra
66
*/
77
const messageListenerUtil = require('./utils/messageListenerUtil');
88
const domUtil = require('./utils/domUtil');
99
const storageUtil = require('./utils/storageUtil');
1010
const CommonEnum = require('./enums/CommonEnum');
1111

12-
window.chrome.extension.sendMessage({}, function(_response) {
12+
;(function() {
1313
window.enhancedGithub = {
14-
config: {
15-
isNewDesign: localStorage.getItem('enhanced-github-for-new-design') || 1
16-
}
14+
config: {}
1715
};
1816

1917
let readyStateCheckInterval = setInterval(function() {
@@ -32,15 +30,17 @@ window.chrome.extension.sendMessage({}, function(_response) {
3230

3331
messageListenerUtil.addListners();
3432

35-
window.chrome.storage.sync.get(
33+
chrome.storage.sync.get(
3634
{
3735
'x-github-token': ''
3836
},
3937
function(storedData) {
40-
storageUtil.set(CommonEnum.TOKEN, storedData['x-github-token']);
38+
if (storedData) {
39+
storageUtil.set(CommonEnum.TOKEN, storedData['x-github-token']);
40+
}
4141
domUtil.addRepoData();
4242
}
4343
);
4444
}
4545
}, 10);
46-
});
46+
})();

src/utils/commonUtil.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,6 @@ let commonUtil = {
9494
[].forEach.call(document.querySelectorAll(selector), function(el) {
9595
el.parentNode.removeChild(el);
9696
});
97-
},
98-
isForGithUbNewUi: function() {
99-
if (window.enhancedGithub && window.enhancedGithub.config) {
100-
return window.enhancedGithub.config.isNewDesign;
101-
}
102-
103-
return false;
10497
}
10598
};
10699

src/utils/domUtil.js

Lines changed: 17 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -32,47 +32,26 @@ let domUtil = {
3232
commonUtil.removePrevInstancesOf('.eg-repo-size');
3333

3434
let formattedFileSize = commonUtil.convertSizeToHumanReadableFormat(storageUtil.get('repoSize') * 1024); // GitHub API return size in KB for repo
35-
36-
if (commonUtil.isForGithUbNewUi()) {
37-
let formattedFileSize = commonUtil.convertSizeToHumanReadableFormat(storageUtil.get('repoSize') * 1024); // GitHub API return size in KB for repo
38-
let elem = document.querySelector('a[href="#readme"]');
39-
40-
if (elem) {
41-
let html = `
42-
<div class="mt-3 eg-repo-size">
43-
<a href="javascript:void(0)" class="muted-link">
44-
<svg class="octicon octicon-database mr-2" mr="2" aria-hidden="true" height="16" version="1.1" viewBox="0 0 12 16" width="16">
45-
<path d="M6 15c-3.31 0-6-.9-6-2v-2c0-.17.09-.34.21-.5.67.86 3 1.5 5.79 1.5s5.12-.64 5.79-1.5c.13.16.21.33.21.5v2c0 1.1-2.69 2-6 2zm0-4c-3.31 0-6-.9-6-2V7c0-.11.04-.21.09-.31.03-.06.07-.13.12-.19C.88 7.36 3.21 8 6 8s5.12-.64 5.79-1.5c.05.06.09.13.12.19.05.1.09.21.09.31v2c0 1.1-2.69 2-6 2zm0-4c-3.31 0-6-.9-6-2V3c0-1.1 2.69-2 6-2s6 .9 6 2v2c0 1.1-2.69 2-6 2zm0-5c-2.21 0-4 .45-4 1s1.79 1 4 1 4-.45 4-1-1.79-1-4-1z"></path>
46-
</svg>
47-
<span class="text-emphasized">${formattedFileSize.size}</span>
48-
<span>${formattedFileSize.measure}</span>
49-
</a>
50-
</div>
51-
`;
52-
53-
elem.parentElement.insertAdjacentHTML('beforeend', html);
54-
}
55-
56-
return;
57-
}
58-
commonUtil.removePrevInstancesOf('.repo-size');
59-
let elem = document.querySelector('ul.numbers-summary');
35+
let elem = document.querySelector('a[href="#readme"]');
6036

6137
if (elem) {
6238
let html = `
63-
<li class="repo-size">
64-
<a>
65-
<svg class="octicon octicon-database" aria-hidden="true" height="16" version="1.1" viewBox="0 0 12 16" width="12">
39+
<div class="mt-3 eg-repo-size">
40+
<a href="javascript:void(0)" class="muted-link">
41+
<svg class="octicon octicon-database mr-2" mr="2" aria-hidden="true" height="16" version="1.1" viewBox="0 0 12 16" width="16">
6642
<path d="M6 15c-3.31 0-6-.9-6-2v-2c0-.17.09-.34.21-.5.67.86 3 1.5 5.79 1.5s5.12-.64 5.79-1.5c.13.16.21.33.21.5v2c0 1.1-2.69 2-6 2zm0-4c-3.31 0-6-.9-6-2V7c0-.11.04-.21.09-.31.03-.06.07-.13.12-.19C.88 7.36 3.21 8 6 8s5.12-.64 5.79-1.5c.05.06.09.13.12.19.05.1.09.21.09.31v2c0 1.1-2.69 2-6 2zm0-4c-3.31 0-6-.9-6-2V3c0-1.1 2.69-2 6-2s6 .9 6 2v2c0 1.1-2.69 2-6 2zm0-5c-2.21 0-4 .45-4 1s1.79 1 4 1 4-.45 4-1-1.79-1-4-1z"></path>
6743
</svg>
68-
<span class="num text-emphasized">
69-
${formattedFileSize.size}
70-
</span>
71-
${formattedFileSize.measure}
44+
<span class="text-emphasized">${formattedFileSize.size}</span>
45+
<span>${formattedFileSize.measure}</span>
7246
</a>
73-
</li>`;
74-
elem.insertAdjacentHTML('beforeend', html);
47+
</div>
48+
`;
49+
50+
elem.parentElement.insertAdjacentHTML('beforeend', html);
7551
}
52+
53+
return;
54+
7655
},
7756
addRepoData: function() {
7857
setTimeout(() => {
@@ -124,22 +103,11 @@ let domUtil = {
124103
}
125104
},
126105
addFileSizeAndDownloadLink: function() {
127-
if (commonUtil.isForGithUbNewUi()) {
128-
apiUtil.getRepoContent(function(data) {
129-
handlersUtil.onPathContentFetched(data);
130-
});
131-
132-
return;
133-
}
134-
let links = document.querySelectorAll('tr.js-navigation-item > td.content a');
135-
let elems = document.querySelectorAll('tr.js-navigation-item > td.age');
106+
apiUtil.getRepoContent(function(data) {
107+
handlersUtil.onPathContentFetched(data);
108+
});
136109

137-
if (elems.length && elems.length === links.length) {
138-
// verify length for showing in-sync
139-
apiUtil.getRepoContent(function(data) {
140-
handlersUtil.onPathContentFetched(data);
141-
});
142-
}
110+
return;
143111
}
144112
};
145113

src/utils/handlersUtil.js

Lines changed: 42 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -43,90 +43,56 @@ let handlersUtil = {
4343
return;
4444
}
4545

46-
if (commonUtil.isForGithUbNewUi()) {
47-
setTimeout(function() {
48-
commonUtil.removePrevInstancesOf('.eg-download'); // remove before adding new ones
49-
50-
const containerItems = document.querySelectorAll(
51-
'.js-navigation-container.js-active-navigation-container .js-navigation-item'
52-
);
53-
54-
let actualDataIndex = 0;
55-
56-
for (var i = 0; i < containerItems.length; i++) {
57-
const commitElem = containerItems[i].querySelector('.commit-message');
58-
59-
if (commitElem) {
60-
containerItems[i].querySelector('div:nth-of-type(2)').classList.remove('col-md-2', 'mr-3');
61-
containerItems[i].querySelector('div:nth-of-type(2)').classList.add('col-md-1', 'mr-2');
62-
63-
if (data[actualDataIndex].type === 'file') {
64-
let formattedFileSize = commonUtil.getFileSizeAndUnit(data[actualDataIndex]);
65-
66-
let html = `
67-
<div role="gridcell" class="mr-1 text-gray-light eg-download" style="width: 95px;">
68-
<span class="css-truncate css-truncate-target d-block">
69-
<span style="margin-right: 5px;">
70-
${formattedFileSize}
71-
</span>
72-
<a style="float: right" href="${data[actualDataIndex].download_url}" title="(Alt/Option/Ctrl + Click) to download File" aria-label="(Alt/Option/Ctrl + Click) to download File" class="tooltipped tooltipped-s"
73-
download="${data[actualDataIndex].name}">
74-
<svg class="octicon octicon-cloud-download" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16">
75-
<path d="M9 12h2l-3 3-3-3h2V7h2v5zm3-8c0-.44-.91-3-4.5-3C5.08 1 3 2.92 3 5 1.02 5 0 6.52 0 8c0 1.53 1 3 3 3h3V9.7H3C1.38 9.7 1.3 8.28 1.3 8c0-.17.05-1.7 1.7-1.7h1.3V5c0-1.39 1.56-2.7 3.2-2.7 2.55 0 3.13 1.55 3.2 1.8v1.2H12c.81 0 2.7.22 2.7 2.2 0 2.09-2.25 2.2-2.7 2.2h-2V11h2c2.08 0 4-1.16 4-3.5C16 5.06 14.08 4 12 4z"></path>
76-
</svg>
77-
</a>
78-
</span>
79-
</div>
80-
`;
81-
82-
commitElem.insertAdjacentHTML('afterend', html);
83-
} else {
84-
const defaultHtml = `
85-
<div role="gridcell" class="mr-1 eg-download" style="width: 90px;"></div>
86-
`;
46+
setTimeout(function() {
47+
commonUtil.removePrevInstancesOf('.eg-download'); // remove before adding new ones
8748

88-
commitElem.insertAdjacentHTML('afterend', defaultHtml);
89-
}
49+
const containerItems = document.querySelectorAll(
50+
'.js-navigation-container.js-active-navigation-container .js-navigation-item'
51+
);
9052

91-
actualDataIndex++;
92-
}
93-
}
94-
}, 1000);
95-
96-
return;
97-
}
53+
let actualDataIndex = 0;
9854

99-
commonUtil.removePrevInstancesOf('.download'); // remove before adding new ones
55+
for (var i = 0; i < containerItems.length; i++) {
56+
const commitElem = containerItems[i].querySelector('.commit-message');
10057

101-
let uptree = document.querySelectorAll('tr.up-tree > td');
58+
if (commitElem) {
59+
containerItems[i].querySelector('div:nth-of-type(2)').classList.remove('col-md-2', 'mr-3');
60+
containerItems[i].querySelector('div:nth-of-type(2)').classList.add('col-md-1', 'mr-2');
10261

103-
if (uptree && uptree[3]) {
104-
uptree[3].insertAdjacentHTML('afterend', '<td class="download"></td>');
105-
}
62+
if (data[actualDataIndex].type === 'file') {
63+
let formattedFileSize = commonUtil.getFileSizeAndUnit(data[actualDataIndex]);
10664

107-
let elems = document.querySelectorAll('tr.js-navigation-item > td.age');
65+
let html = `
66+
<div role="gridcell" class="mr-1 text-gray-light eg-download" style="width: 95px;">
67+
<span class="css-truncate css-truncate-target d-block">
68+
<span style="margin-right: 5px;">
69+
${formattedFileSize}
70+
</span>
71+
<a style="float: right" href="${data[actualDataIndex].download_url}" title="(Alt/Option/Ctrl + Click) to download File" aria-label="(Alt/Option/Ctrl + Click) to download File" class="tooltipped tooltipped-s"
72+
download="${data[actualDataIndex].name}">
73+
<svg class="octicon octicon-cloud-download" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16">
74+
<path d="M9 12h2l-3 3-3-3h2V7h2v5zm3-8c0-.44-.91-3-4.5-3C5.08 1 3 2.92 3 5 1.02 5 0 6.52 0 8c0 1.53 1 3 3 3h3V9.7H3C1.38 9.7 1.3 8.28 1.3 8c0-.17.05-1.7 1.7-1.7h1.3V5c0-1.39 1.56-2.7 3.2-2.7 2.55 0 3.13 1.55 3.2 1.8v1.2H12c.81 0 2.7.22 2.7 2.2 0 2.09-2.25 2.2-2.7 2.2h-2V11h2c2.08 0 4-1.16 4-3.5C16 5.06 14.08 4 12 4z"></path>
75+
</svg>
76+
</a>
77+
</span>
78+
</div>
79+
`;
80+
81+
commitElem.insertAdjacentHTML('afterend', html);
82+
} else {
83+
const defaultHtml = `
84+
<div role="gridcell" class="mr-1 eg-download" style="width: 90px;"></div>
85+
`;
86+
87+
commitElem.insertAdjacentHTML('afterend', defaultHtml);
88+
}
10889

109-
for (let i = 0; i < elems.length; i++) {
110-
if (data[i].type === 'file') {
111-
let formattedFileSize = commonUtil.getFileSizeAndUnit(data[i]);
112-
113-
let html = `
114-
<td class="download js-enhanced-github-download-btn" style="width: 20px;padding-right: 10px;color: #6a737d;text-align: right;white-space: nowrap;">
115-
<span style="margin-right: 5px;">
116-
${formattedFileSize}
117-
</span>
118-
<a href="${data[i].download_url}" title="(Alt/Option/Ctrl + Click) to download File" aria-label="(Alt/Option/Ctrl + Click) to download File" class="tooltipped tooltipped-nw"
119-
download="${data[i].name}">
120-
<svg class="octicon octicon-cloud-download" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16">
121-
<path d="M9 12h2l-3 3-3-3h2V7h2v5zm3-8c0-.44-.91-3-4.5-3C5.08 1 3 2.92 3 5 1.02 5 0 6.52 0 8c0 1.53 1 3 3 3h3V9.7H3C1.38 9.7 1.3 8.28 1.3 8c0-.17.05-1.7 1.7-1.7h1.3V5c0-1.39 1.56-2.7 3.2-2.7 2.55 0 3.13 1.55 3.2 1.8v1.2H12c.81 0 2.7.22 2.7 2.2 0 2.09-2.25 2.2-2.7 2.2h-2V11h2c2.08 0 4-1.16 4-3.5C16 5.06 14.08 4 12 4z"></path>
122-
</svg>
123-
</a>
124-
</td>`;
125-
elems[i].insertAdjacentHTML('afterend', html);
126-
} else {
127-
elems[i].insertAdjacentHTML('afterend', '<td class="download"></td>');
90+
actualDataIndex++;
91+
}
12892
}
129-
}
93+
}, 1000);
94+
95+
return;
13096
}
13197
};
13298

0 commit comments

Comments
 (0)