Skip to content

Instantly share code, notes, and snippets.

@jparishy
Created February 6, 2014 16:27
Show Gist options
  • Save jparishy/8847614 to your computer and use it in GitHub Desktop.
Save jparishy/8847614 to your computer and use it in GitHub Desktop.
Chrome Extension for switching between header (.h) and source files (.m,.mm,.c,.cpp) on GitHub with the Xcode Shortcut Ctrl+Command+Up
chrome.commands.onCommand.addListener(function(command) {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
var current = tabs[0];
var url = current.url;
var ext = url.substring(url.lastIndexOf("."));
if(ext === ".h")
{
existingSourceFileForHeaderURL(url, function (sourceURL) {
if(sourceURL)
{
chrome.tabs.update(current.id, { url: sourceURL });
}
});
}
else if(ext === ".m" || ext === ".c" || ext === ".cpp" || ext === ".mm")
{
var headerURL = replaceExtension(url, "h");
chrome.tabs.update(current.id, { url: headerURL });
}
});
});
function urlExists(url, callback) {
var http = new XMLHttpRequest();
http.open('HEAD', url);
http.onreadystatechange = function() {
if (this.readyState == this.DONE) {
callback(this.status != 404);
}
};
http.send();
}
function replaceExtension(url, newExt) {
var withoutExt = url.substring(0, url.lastIndexOf(".") + 1);
return withoutExt + newExt;
}
function existingSourceFileForHeaderURL(url, callback) {
var objcURL = replaceExtension(url, "m");
urlExists(objcURL, function (exists) {
if(exists)
{
callback(objcURL);
}
else
{
var mmURL = replaceExtension(url, "mm");
urlExists(mmURL, function (exists) {
if(exists)
{
callback(mmURL);
}
else
{
var cURL = replaceExtension(url, "c");
urlExists(cURL, function (exists) {
if(exists)
{
callback(cURL);
}
else
{
var cppURL = replaceExtension(url, "cpp");
urlExists(cURL, function (exists) {
if(exists)
{
callback(cppURL);
}
else
{
callback(null);
}
});
}
});
}
});
}
});
}
{
"name": "HeaderImpl",
"version": "1.0",
"description": "Switch between Header and Implementation Files using a Keyboard Shortcut",
"background": {
"persistent": false,
"scripts": [ "main.js" ]
},
"permissions": [ "tabs", "https://github.com/" ],
"commands": {
"switch-to-other-file": {
"suggested_key": {
"default": "Ctrl+Shift+Up",
"mac": "MacCtrl+Command+Up"
},
"description": "Switch to the other file"
}
},
"manifest_version": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment