Skip to content

Commit

Permalink
Fetch links in background script (OctoLinker#546)
Browse files Browse the repository at this point in the history
Chrome 73 changed to cross-origin requests in chrome extension content scripts see
https://www.chromestatus.com/feature/5629709824032768
  • Loading branch information
stefanbuck authored Mar 14, 2019
1 parent 00a1d3a commit a179ffe
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
17 changes: 17 additions & 0 deletions packages/core/background/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
import * as storage from '@octolinker/helper-settings';
import newTab from './newTab';
import fetchUrls from '../utils/fetch';

storage.load().then(() => {
newTab();
});

chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.type !== 'fetch') {
return;
}

fetchUrls(request.urls)
.then(res => {
sendResponse(res);
})
.catch(() => {
sendResponse();
});

return true;
});
18 changes: 8 additions & 10 deletions packages/core/click-handler.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import $ from 'jquery';
import * as storage from '@octolinker/helper-settings';
import { showTooltip, removeTooltip } from '@octolinker/user-interface';
import fetch from './utils/fetch.js';

const SORRY = "I'm sorry, unable to resolve this link 😱";
const PROCESS = 'Processing ⏳';
Expand Down Expand Up @@ -109,17 +108,16 @@ async function onClick(event) {
return;
}

try {
const { url, res } = await fetch(urls);

// const { url, res } = await fetch(urls);
chrome.runtime.sendMessage({ type: 'fetch', urls }, res => {
if (res === 'error') {
showTooltip($tooltipTarget, SORRY);
return;
}
showTooltip($tooltipTarget, RESOLVED);
openUrl(event, (res || {}).url || url);
openUrl(event, (res || {}).url || res);
removeTooltip($tooltipTarget);
} catch (err) {
showTooltip($tooltipTarget, SORRY);

return console.error(err); // eslint-disable-line no-console
}
});
}

export default function(_matches) {
Expand Down
18 changes: 10 additions & 8 deletions packages/core/utils/fetch.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import $ from 'jquery';

export default async urls => {
for (const { url, func, method = 'HEAD' } of urls) {
try {
Expand All @@ -14,18 +12,22 @@ export default async urls => {
// However, we explicity want to do this sequentially.
// See http://eslint.org/docs/rules/no-await-in-loop
// eslint-disable-next-line
const res = await $.ajax({
const res = await fetch(url, {
method,
url,
});

return { url, res };
if (res.status < 300) {
if (method === 'GET') {
// eslint-disable-next-line
const json = await res.json();
return json;
}

return { url };
}
} catch (err) {
// There's nothing to do here, so just keep going.
// eslint-disable-line no-empty
}
}

// If we get here, no urls could be loaded.
throw new Error('Could not load any url');
};

0 comments on commit a179ffe

Please sign in to comment.