Skip to content

Commit e033c45

Browse files
stefanbuckjosephfrazier
authored andcommitted
Add Firefox support (#100)
* Do request in background * Repleace innerHTML with textContent * Tweak build step * Produce production build * Extend eslint ignore * Use background loading for all requests * Enable sourcemaps for dev build * Add `open-firefox` npm script This opens Firefox with the extension installed from ./firefox, using https://github.com/mozilla/web-ext Usage: npm run build-firefox npm run open-firefox * Use flag to determine if event handler is already attached Firefox doesn't support hasEventListeners therefore I implemented the flag.
1 parent 166abef commit e033c45

File tree

17 files changed

+142
-156
lines changed

17 files changed

+142
-156
lines changed

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
./dist
12
./chrome
3+
./firefox

.eslintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
},
1212
"globals": {
1313
"fixture": true,
14+
"chrome": true,
1415
}
1516
}

.gitignore

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
node_modules
22
npm-debug.log
3-
chrome/app.js
3+
4+
dist/
5+
6+
chrome/*
7+
!chrome/manifest.json
8+
9+
firefox/*
10+
!firefox/manifest.json
File renamed without changes.
File renamed without changes.

chrome/manifest.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
"icons": {
99
"128": "icon.png"
1010
},
11+
"background": {
12+
"scripts": ["background.js"]
13+
},
1114
"content_scripts": [
1215
{
1316
"matches": [

firefox/manifest.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "OctoLinker",
3+
"version": "4.1.1",
4+
"manifest_version": 2,
5+
"author": "Stefan Buck",
6+
"description": "",
7+
"homepage_url": "https://github.com/OctoLinker/browser-extension/",
8+
"icons": {
9+
"128": "icon.png"
10+
},
11+
"applications": {
12+
"gecko": {
13+
14+
}
15+
},
16+
"background": {
17+
"scripts": ["background.js"]
18+
},
19+
"content_scripts": [
20+
{
21+
"matches": [
22+
"https://github.com/*",
23+
"https://gist.github.com/*"
24+
],
25+
"js": ["app.js"],
26+
"css": ["style.css"],
27+
"run_at": "document_idle",
28+
"all_frames": false
29+
}
30+
],
31+
"permissions": [
32+
"https://github.com/",
33+
"https://githublinker.herokuapp.com/"
34+
]
35+
}

lib/background.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import $ from 'jquery';
2+
3+
function loader(urls, cb) {
4+
const req = urls.shift();
5+
const url = req.url || req;
6+
7+
$.ajax({
8+
method: req.method || 'HEAD',
9+
url,
10+
}).then(function (res) {
11+
cb(null, url, res);
12+
}).fail(function () {
13+
if (urls.length === 0) {
14+
return cb(new Error('Could not load any url'));
15+
}
16+
17+
loader(urls, cb);
18+
});
19+
}
20+
21+
chrome.runtime.onMessage.addListener(({ urls }, sender) => {
22+
loader(urls, (err, url, res) => {
23+
chrome.tabs.sendMessage(sender.tab.id, {
24+
err,
25+
url,
26+
res,
27+
});
28+
});
29+
});

lib/click-handler.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import $ from 'jquery';
2-
import tryLoad from '../packages/try-load';
32
import { showTooltip } from './gh-interface.js';
43
import * as resolvers from './resolver/index.js';
54

@@ -11,6 +10,8 @@ const LINK_SELECTOR = '.octo-linker-link';
1110
const $body = $('body');
1211
const resolverHandlers = new Map();
1312

13+
let hasEventListener = false;
14+
1415
function openUrl(url, newWindow = false) {
1516
if (!url) {
1617
return;
@@ -19,6 +20,24 @@ function openUrl(url, newWindow = false) {
1920
window.open(url, newWindow ? '_blank' : '_self');
2021
}
2122

23+
function tryLoad(urls, cb) {
24+
if (!hasEventListener) {
25+
chrome.runtime.onMessage.addListener((msg) => {
26+
if (msg.err) {
27+
return cb(new Error('Could not load any url'));
28+
}
29+
30+
cb(null, msg.url, msg.res);
31+
});
32+
33+
hasEventListener = true;
34+
}
35+
36+
chrome.runtime.sendMessage({
37+
urls,
38+
});
39+
}
40+
2241
function getResolverUrls(dataAttr) {
2342
const resolverStrings = dataAttr.resolver.replace(/\s/g, '').split('|');
2443

lib/gh-interface.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ export function showTooltip($target, msg) {
1010

1111
function getUserName() {
1212
const el = window.document.querySelector('.header .css-truncate-target');
13-
if (el && el.innerHTML.length) {
14-
return ' ' + el.innerHTML;
13+
if (el && el.textContent.length) {
14+
return ' ' + el.textContent;
1515
}
1616
return '';
1717
}

0 commit comments

Comments
 (0)