Skip to content

Commit

Permalink
Add Firefox support (OctoLinker#100)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
stefanbuck authored and josephfrazier committed Jun 20, 2016
1 parent 166abef commit e033c45
Show file tree
Hide file tree
Showing 17 changed files with 142 additions and 156 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
./dist
./chrome
./firefox
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
},
"globals": {
"fixture": true,
"chrome": true,
}
}
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
node_modules
npm-debug.log
chrome/app.js

dist/

chrome/*
!chrome/manifest.json

firefox/*
!firefox/manifest.json
File renamed without changes
File renamed without changes.
3 changes: 3 additions & 0 deletions chrome/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
"icons": {
"128": "icon.png"
},
"background": {
"scripts": ["background.js"]
},
"content_scripts": [
{
"matches": [
Expand Down
35 changes: 35 additions & 0 deletions firefox/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "OctoLinker",
"version": "4.1.1",
"manifest_version": 2,
"author": "Stefan Buck",
"description": "",
"homepage_url": "https://github.com/OctoLinker/browser-extension/",
"icons": {
"128": "icon.png"
},
"applications": {
"gecko": {
"id": "[email protected]"
}
},
"background": {
"scripts": ["background.js"]
},
"content_scripts": [
{
"matches": [
"https://github.com/*",
"https://gist.github.com/*"
],
"js": ["app.js"],
"css": ["style.css"],
"run_at": "document_idle",
"all_frames": false
}
],
"permissions": [
"https://github.com/",
"https://githublinker.herokuapp.com/"
]
}
29 changes: 29 additions & 0 deletions lib/background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import $ from 'jquery';

function loader(urls, cb) {
const req = urls.shift();
const url = req.url || req;

$.ajax({
method: req.method || 'HEAD',
url,
}).then(function (res) {
cb(null, url, res);
}).fail(function () {
if (urls.length === 0) {
return cb(new Error('Could not load any url'));
}

loader(urls, cb);
});
}

chrome.runtime.onMessage.addListener(({ urls }, sender) => {
loader(urls, (err, url, res) => {
chrome.tabs.sendMessage(sender.tab.id, {
err,
url,
res,
});
});
});
21 changes: 20 additions & 1 deletion lib/click-handler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import $ from 'jquery';
import tryLoad from '../packages/try-load';
import { showTooltip } from './gh-interface.js';
import * as resolvers from './resolver/index.js';

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

let hasEventListener = false;

function openUrl(url, newWindow = false) {
if (!url) {
return;
Expand All @@ -19,6 +20,24 @@ function openUrl(url, newWindow = false) {
window.open(url, newWindow ? '_blank' : '_self');
}

function tryLoad(urls, cb) {
if (!hasEventListener) {
chrome.runtime.onMessage.addListener((msg) => {
if (msg.err) {
return cb(new Error('Could not load any url'));
}

cb(null, msg.url, msg.res);
});

hasEventListener = true;
}

chrome.runtime.sendMessage({
urls,
});
}

function getResolverUrls(dataAttr) {
const resolverStrings = dataAttr.resolver.replace(/\s/g, '').split('|');

Expand Down
4 changes: 2 additions & 2 deletions lib/gh-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export function showTooltip($target, msg) {

function getUserName() {
const el = window.document.querySelector('.header .css-truncate-target');
if (el && el.innerHTML.length) {
return ' ' + el.innerHTML;
if (el && el.textContent.length) {
return ' ' + el.textContent;
}
return '';
}
Expand Down
2 changes: 1 addition & 1 deletion lib/insert-link.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function createLinkElement(text, dataAttr = {}) {
const linkEl = document.createElement('span');

// Set link text
linkEl.innerHTML = text;
linkEl.textContent = text;

// Add css classes
linkEl.classList.add(CLASS_NAME);
Expand Down
13 changes: 11 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@
"test": "karma start",
"test:watch": "karma start --no-single-run --auto-watch",
"build": "webpack",
"build:watch": "webpack --watch"
"build:watch": "webpack --watch",
"build-chrome": "webpack --output-path chrome",
"build-firefox": "webpack --output-path firefox",
"open-firefox": "web-ext run --source-dir firefox",
"dist-chrome": "webpack -p --config webpack.dist.config.js --dist=chrome",
"dist-firefox": "webpack -p --config webpack.dist.config.js --dist=firefox",
"dist-all": "npm run dist-chrome && npm run dist-firefox"
},
"dependencies": {
"JSONPath": "^0.11.2",
Expand All @@ -32,6 +38,7 @@
"babel-eslint": "^4.1.3",
"babel-loader": "^6.2.4",
"babel-preset-es2015": "^6.9.0",
"copy-webpack-plugin": "^3.0.1",
"eslint": "^1.10.3",
"eslint-config-airbnb": "2.1.1",
"json-loader": "^0.5.4",
Expand All @@ -46,7 +53,9 @@
"mocha": "^2.3.4",
"phantomjs": "^1.9.18",
"sinon": "^2.0.0-pre",
"webpack": "^1.13.1"
"web-ext": "^1.0.1",
"webpack": "^1.13.1",
"yargs": "^4.7.1"
},
"private": true
}
12 changes: 0 additions & 12 deletions packages/try-load/README.md

This file was deleted.

23 changes: 0 additions & 23 deletions packages/try-load/index.js

This file was deleted.

114 changes: 0 additions & 114 deletions packages/try-load/test.js

This file was deleted.

9 changes: 9 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
const CopyWebpackPlugin = require('copy-webpack-plugin');

module.exports = {
entry: {
app: './lib/app',
background: './lib/background',
},
devtool: 'source-map',
output: {
path: 'chrome',
filename: '[name].js',
},
plugins: [
new CopyWebpackPlugin([
{ from: 'assets' },
]),
],
module: {
loaders: [
{
Expand Down
21 changes: 21 additions & 0 deletions webpack.dist.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const argv = require('yargs').argv;
const webpackConfig = require('./webpack.config.js');
const CopyWebpackPlugin = require('copy-webpack-plugin');

const dist = argv.dist;

const config = Object.assign({}, webpackConfig, {
devtool: '',
output: {
path: `dist/${dist}`,
filename: '[name].js',
},
});

config.plugins.push(
new CopyWebpackPlugin([
{ from: `${dist}/manifest.json` },
])
);

module.exports = config;

0 comments on commit e033c45

Please sign in to comment.