Skip to content

Instantly share code, notes, and snippets.

@jovaneyck
Last active December 14, 2015 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jovaneyck/5063629 to your computer and use it in GitHub Desktop.
Save jovaneyck/5063629 to your computer and use it in GitHub Desktop.
A basic stub for the Crossrider javascript API I use for local unit testing of browser extensions. It's by no means complete, so feel free to add/improve. Usage: *first reference this stub in your test html to make the stub available for your own code *next, reference the background.js in your test html (the order of script includes is important…
var appAPI = (function () {
var tabListener;
var backGroundListener;
var storedValue = null;
return {
ready: function (callback) { callback(); },
message: {
addListener: function (listener) {
//dirty hack: load the background.js first in the test html, this will bind its listener first
if (!backGroundListener)
backGroundListener = listener;
else {
tabListener = listener;
}
},
toBackground: function (message) {
if (backGroundListener)
backGroundListener(message);
else
console.log('could not find the background listener!');
},
toActiveTab: function (message) {
console.log('sending a message to the active tab:');
console.log(message);
tabListener(message);
},
toAllTabs: function (message) {
if (tabListener)
tabListener(message);
else
console.log('could not find the tab listener!');
},
getTabListener: function () {
return tabListener;
},
getBackgroundListener: function () {
return backGroundListener;
}
},
resources: {
includeJS: function (path) {console.log("loading JS: "+path); },
includeCSS: function (path) { console.log("loading CSS: " + path); },
getImage: function (path) { console.log("loading img: " + path); return 'http://localhost:8081/src/Resources/' + path; }
},
getTabId: function () { return 1; },
db: {
get: function (key) { return storedValue; },
set: function (key, value) {storedValue = value; },
removeAll: function () { storedValue = null; }
},
request: {
get: function (url, onsuccess, onfailure) {
console.log("GET request on: " + url);
var response = "undefined REST endpoint";
if (url.indexOf("API/Search/Get") != -1)
response = JSON.stringify({ "testData" : "testdata" });
onsuccess(response);
},
post: function (url, params, onsuccess, onfailure) {
console.log("POST request on: " + url + ", params: " + params);
onsuccess();
}
},
JSON: {
parse: function (data) {
return JSON.parse(data);
},
stringify: function (data) {
return JSON.stringify(data);
}
},
openURL: function(url, destination) {
console.log('appAPI.openURL(' + url + ') was called');
}
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment