Skip to content

Instantly share code, notes, and snippets.

@pmuellr
Created July 1, 2011 22:29
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 pmuellr/1059534 to your computer and use it in GitHub Desktop.
Save pmuellr/1059534 to your computer and use it in GitHub Desktop.
first pass, not working, of "local" weinre extensions via dotjs
//------------------------------------------------------------------------------
// dotjs extension to install extensions in weinre
//------------------------------------------------------------------------------
;(function() {
//------------------------------------------------------------------------------
// this function will string-ized and included in a script element that
// will be injected into the weinre client window
//------------------------------------------------------------------------------
function installExtensionAPI(weinreExtConfig) {
var wec = weinreExtConfig
//--------------------------------------------------------------------------
// window.weinre.ext is where our API is installed
//--------------------------------------------------------------------------
window.weinre = {}
weinre.ext = {}
weinre.ext.home = wec.home
//--------------------------------------------------------------------------
// install a CSS file from the extension directory
//--------------------------------------------------------------------------
weinre.ext.installCSS = function(extensionName, cssURL) {
var style = document.createElement("link")
style.setAttribute("rel", "stylesheet")
style.setAttribute("type", "text/css")
style.setAttribute("href", wec.home + "/" + extensionName + "/" + cssURL)
document.body.appendChild(style)
}
//--------------------------------------------------------------------------
// install a panel
//--------------------------------------------------------------------------
weinre.ext.installPanel = function(extensionName, panel, toolbarIconURL) {
// get the panel name (passed to Panel constructor)
var panelName = panel._panelName
// add panel to global list of panels
WebInspector.panels[panelName] = panel
// get the toolbar and the last toolbar item
var toolbar = document.getElementById("toolbar")
var lastToolbarItem = WebInspector.panelOrder.reverse()[0].toolbarItem
// add our toolbar item
var toolbarItem = WebInspector.addPanelToolbarIcon(toolbar, panel, lastToolbarItem)
// set the toolbar item image
var toolbarIconURL = weinre_ext_home + "/" + extensionName + "/" + toolbarIconURL
toolbarItem.firstChild.style.backgroundImage = "url('" + toolbarIconURL + "')"
}
//--------------------------------------------------------------------------
// load the extensions
//--------------------------------------------------------------------------
wec.extensions.forEach(function(extension) {
var script = document.createElement("script")
script.src = wec.home + "/" + extension + "/extension.js"
document.body.appendChild(script)
})
}
//------------------------------------------------------------------------------
function injectExtensionAPI(weinreExtConfig) {
var code = installExtensionAPI.toString()
var config = JSON.stringify(weinreExtConfig)
var script = document.createElement("script")
script.innerText = ";(" + code + ")(" + config + ");"
document.body.appendChild(script)
}
//------------------------------------------------------------------------------
// main logic here
//------------------------------------------------------------------------------
console.log("in dotjs weinre extension installer")
if (location.pathname != "/client/") return
var configURL = "http://localhost:3131/weinre-ext-config.json"
$(window).load(function() {
$.ajax(configURL, {
dataType: "json",
success: injectExtensionAPI,
error: function() { console.log("error loading " + configURL) }
})
})
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment