diff --git a/src/manifest.json b/src/manifest.json index b4b4401..a8aefd8 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -1,10 +1,10 @@ { "name": "Edit in Prose", "description": "Provides an \"Edit in Prose\" link from a hosted file on GitHub.", - "version": "1.3.0", + "version": "1.4.0", "manifest_version": 2, "author": "Tristen Brown", - "homepage_url": "http://prose.io", + "homepage_url": "https://prose.io", "icons": { "16": "icon16.png", "48": "icon48.png", diff --git a/src/prose.js b/src/prose.js index b6a5c54..1ca61a3 100644 --- a/src/prose.js +++ b/src/prose.js @@ -9,32 +9,44 @@ const repo = parts[1]; const branch = parts[3]; const path = parts.slice(4).join('/'); + const configFilePath = 'https://github.com/' + user + '/' + repo + '/blob/master/_prose.yml'; + // User test if (!user || !/^\w[\w-]+$/.test(user)) { return; } - // Nav button - if (repo) { - const nav = document.querySelector('.hx_reponav'); - const button = document.querySelector('#prose'); - if (!button) { - const link = document.createElement('a'); - link.id = 'prose'; - link.innerHTML = '<span class="prose-icon"></span>Prose'; - link.title = 'Open in Prose'; - link.className = 'js-selected-navigation-item reponav-item'; - link.rel = 'nofollow'; - nav.appendChild(link); - link.href = 'http://prose.io/#' + user + '/' + repo + '/'; + // Prose config file test + fetch(configFilePath).then(response => { + if (response.ok) { + // Display nav button + const nav = document.querySelector('.hx_reponav'); + const button = document.querySelector('#prose'); + if (!button) { + const link = document.createElement('a'); + link.id = 'prose'; + link.innerHTML = '<span class="prose-icon"></span>Prose'; + link.title = 'Open in Prose'; + link.className = 'js-selected-navigation-item reponav-item'; + link.rel = 'nofollow'; + nav.appendChild(link); + link.href = 'https://prose.io/#' + user + '/' + repo + '/'; + } + } else { + throw new Error('Can\'t find "_prose.yml" file'); } - } + }).catch(err => console.log(err.message)); - // Sha Test + // Sha test if (/^[0-9a-f]{40}$/.test(branch)) { return; } + // Markdown file type test + if (!/\.markdown|mdown|mkdn|mkd|md$/i.test(path)) { + return; + } + // Edit button const root = document.querySelector(rootClass); if (!root) { @@ -57,6 +69,6 @@ edit = link.firstChild; group.insertBefore(link, group.firstChild); } - edit.href = 'http://prose.io/#' + user + '/' + repo + '/edit/' + branch + '/' + path; + edit.href = 'https://prose.io/#' + user + '/' + repo + '/edit/' + branch + '/' + path; } })();