Skip to content

Commit

Permalink
feat: support running code on gobyexample.com :bowtie:
Browse files Browse the repository at this point in the history
  • Loading branch information
shatgupt committed Jun 22, 2017
1 parent f3b9360 commit 8194b62
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
7 changes: 5 additions & 2 deletions build-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ const config = {
'description': 'Run code online from sites like Github, Gitlab and more',
'author': 'Shatrughn Gupta',
'homepage_url': 'https://runmycode.online',
'version': '1.1.1',
'version': '1.2.0',
'icons': { '128': 'icon128.png' },
'manifest_version': 2,
'content_scripts': [
{
'matches': [
'https://github.com/*',
'https://gitlab.com/*'
'https://gist.github.com/*',
'https://gitlab.com/*',
'https://bitbucket.org/*',
'https://gobyexample.com/*'
],
'js': ['browser-polyfill.min.js', 'content-script.js'],
'css': ['runmycode-panel.css'],
Expand Down
51 changes: 45 additions & 6 deletions content-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,31 @@ const extMap = {
go: 'go'
}

const locationMap = {
'github.com': 'github',
'gist.github.com': 'github_gist',
'gitlab.com': 'gitlab',
'gitlab.com/snippets': 'gitlab_snippets',
'bitbucket.org': 'bitbucket',
'bitbucket.org/snippets': 'bitbucket_snippets',
'gobyexample.com': 'gobyexample'
}

const getPlatform = () => {
const l = location.hostname + '/' + location.pathname.split('/')[1]
return locationMap[l] || locationMap[location.hostname]
}

const getLangFromPathExt = () => extMap[location.pathname.split('.').pop()]

const getCodeFromLines = (lines) => {
return [].map.call(lines, (line) => line.innerText === '\n' ? '' : line.innerText).join('\n')
}

const body = document.body
const platformMap = {
gitlab: {
getLang: () => getLangFromPathExt(),
getPage: () => {
if (body.dataset.page === 'projects:blob:show') {
return 'show'
Expand All @@ -42,6 +60,7 @@ const platformMap = {
}
},
github: {
getLang: () => getLangFromPathExt(),
getPage: () => {
if (body.classList.contains('page-edit-blob')) {
return 'edit'
Expand All @@ -63,12 +82,29 @@ const platformMap = {
getCode: () => $('.file-editor-textarea').value
}
}
},
gobyexample: {
getLang: () => 'go',
getPage: () => {
if ($('body>div.example')) return 'show'
},
injectRunButton: () => {
const runBtn = $('.run')
runBtn.parentNode.setAttribute('href', '#')
runBtn.setAttribute('id', 'runmycode-popup-runner')
},
pages: {
show: {
// there are 2 tables on the page, first one has the code
getCode: () => getCodeFromLines($('table').querySelectorAll('.code>.highlight>pre'))
}
}
}
}

const platform = window.location.hostname.split('.').slice(-2, -1)
const platform = getPlatform()
let runnerAdded = false
let ext, lang, page
let lang, page

const initRunner = () => {
if (runnerAdded) return
Expand Down Expand Up @@ -242,10 +278,13 @@ const initRunner = () => {
}

const handlePageUpdate = () => {
ext = location.pathname.split('.').pop()
lang = extMap[ext]
page = platformMap[platform] ? platformMap[platform].getPage() : null
if (lang && page) initRunner()
// console.log('platform:', platform)
if (platformMap[platform]) {
page = platformMap[platform].getPage()
lang = platformMap[platform].getLang()
// console.log('page:', page, ' lang:', lang)
if (lang && page) initRunner()
}
}

// this is required because of single page apps like Github,
Expand Down
1 change: 1 addition & 0 deletions runmycode-panel.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
/* set these so Chrome doesn't return 'auto' from getComputedStyle */
z-index: 999;
box-shadow: 5px 5px 10px #888;
font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}

#runmycode-runner .panel {
Expand Down

0 comments on commit 8194b62

Please sign in to comment.