Plato on Github
Report Home
node_modules\atropa-inject\src\atropa-inject.js
Maintainability
62.08
Lines of code
262
Difficulty
20.58
Estimated Errors
0.56
Function weight
By Complexity
By SLOC
/// <reference path="../docs/vsdoc/OpenLayersAll.js"/> var atropa = require('atropa-header'); atropa.setAsOptionalArg = require('atropa-setAsOptionalArg').setAsOptionalArg; /*jslint indent: 4, maxerr: 50, white: true, browser: true, devel: true, plusplus: true, regexp: true */ /*global atropa */ // end header atropa.requires( 'inject', function () { "use strict"; if(document.createElement === undefined) { return false; } return true; } ); /** * Contains tools for injecting elements and assemblies. * into the page. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20130308 * @namespace Contains tools for injecting elements and assemblies. * @requires atropa.data * @requires atropa.supportCheck * @requires atropa.setAsOptionalArg */ atropa.inject = {}; /** * Generic Element Injector. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20120909 * @param {String} elementType The type of element to be injected. * @param {HTML DOM Document} docref Optional. A reference to the document to * target, defaults to <code>document</code>. * @param {DOM Node} parentNod Optional. A reference to the parent node to * target, defaults to <code>docref.body</code>. * @param {Object} attributes Optional. An object whose properties are names of * HTML attributes, defaults to <code>{}</code>. The value of these properties * are to be strings representing the values of the HTML attributes as they are * to be applied to the injected element. * @example Example attributes object : * * attributesObj = { * "id" : "elementID", * "class" : "classy" * }; * @param {Function} onloadHandler Optional. If the element being injected will * fire a load event, this function will be called. Defaults to * <code>function () {}</code>. * @param {Function} callback Optional. This function will be called just before * the element is to be appended to the page. The callback will receive the * element in its current state for any additional processing to be done prior * to it's attachment on callback completion. Defaults to * <code>function () {}</code>. * @return {HTML Element} Returns a reference to the HTML Element created and * injected. * @see <a href="http://www.w3.org/Security/wiki/Same_Origin_Policy"> * http://www.w3.org/Security/wiki/Same_Origin_Policy</a> * @example * // this will inject a div element into the document body. * var el = atropa.inject.element ('div'); * * // This will inject a div with the id "myId" into the element referenced by * // "container" * var el = atropa.inject.element ( * 'div', document, container, { 'id': 'myId' }, null, null * ); * * // this will inject a div into the document of an iframe referenced with "fdoc" * // Just before the div is injected the callback will be called and the element * // may be augmented. When the callback returns the element will be injected. * var fdoc = document.getElementById('someFrame').contentWindow.document; * * var el = atropa.inject.element ( * 'div', fdoc, fdoc.body, { 'id': 'myId' }, * null, * function (myDiv) { * myDiv.textContent = 'I could have attached event handlers'; * } * ); * * // this will inject an iframe into the document * // once the iframe's document has finished loading the onload handler will be * // called. If the document and the iframe are on the same domain, scripts on * // the frame and the parent document will be able to commuincate with each * // other. * function iframeHasLoaded (message) { * console.log(message); * } * * var el = atropa.inject.element ( * 'iframe', document, document.body, * { 'id': 'myId', 'src' : 'http://localhost' }, * function () { * iframeHasLoaded('hey look at that, the frame is ready!'); * // what could I do with the frame? anything I want! * }, * null * ); */ atropa.inject.element = function ( elementType, docref, parentNod, attributes, onloadHandler, callback ) { "use strict"; atropa.supportCheck('inject'); var el, x; docref = atropa.setAsOptionalArg(document, docref); parentNod = atropa.setAsOptionalArg(docref.body, parentNod); attributes = atropa.setAsOptionalArg({}, attributes); onloadHandler = atropa.setAsOptionalArg(atropa.nop, onloadHandler); callback = atropa.setAsOptionalArg(atropa.nop, callback); el = docref.createElement(elementType); for (x in attributes) { if (attributes.hasOwnProperty(x)) { el.setAttribute(x, attributes[x]); } } el.addEventListener('load', onloadHandler, true); callback(el); parentNod.appendChild(el); return el; }; /** * Hidden Iframe Injector. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20130308 * @param {String} id The id of the element to be injected. * @param {String} srcUrl The URL to load in the iframe. * @param {HTML DOM Document} docref Optional. Reference to the document to * inject the iframe in. Defaults to document. * @param {Function} onloadHandler Optional. The onload handler for the iframe. * @param {DOM Node} parentNod Optional. Referenct to the parent node to * append the iframe to. Defaults to docref.body * @param {Function} callback Optional. Callback function for preprocessing * the iframe prior to injection. Called with a reference to the iframe. * @return {HTML Element} Returns a reference to the HTML Element created and * injected. * @see atropa.inject.element * @see <a href="http://www.w3.org/Security/wiki/Same_Origin_Policy"> * http://www.w3.org/Security/wiki/Same_Origin_Policy</a> * @example * el = atropa.inject.hiddenFrame( * 'injectHiddenFrame3', * 'http://localhost/', * null, * function () { * console.log('hey look at that, the frame is ready!'); * }, * null, * null * ); */ atropa.inject.hiddenFrame = function ( id, srcURL, docref, onloadHandler, parentNod, callback ) { "use strict"; atropa.supportCheck('inject'); return atropa.inject.element( 'iframe', docref, parentNod, { "id" : id, "src" : srcURL, "width" : "0px", "height" : "0px", "border" : "0px" }, onloadHandler, callback ); }; /** * Script Injector. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20120909 * @param {String} id The id of the element to be injected. * @param {String} srcUrl The URL where the script is located. * @param {HTML DOM Document} docref Optional. The document to inject the * script into. Defaults to document. * @param {Function} callback Optional. A function to execute once the script * has loaded. Defaults to function () {}; * @return {HTML Element} Returns a reference to the HTML Element created and * injected. * @see atropa.inject.element * @see <a href="http://www.w3.org/Security/wiki/Same_Origin_Policy"> * http://www.w3.org/Security/wiki/Same_Origin_Policy</a> * @example * // Given a script "dummy.js" located at "http://localhost/dummy.js" * // you can fetch the script and execute functions from within it * // as soon as it has loaded into the page. * * // contents of "dummy.js" * function dummy() { * return 'dummy'; * } * * // injecting "dummy.js" into any page. The script tag isn't restricted by * // the same origin policy. Host your script anywhere and inject it to any * // page on the net that you want to. * el = atropa.inject.script( * 'injectScript', * 'http://localhost/', * document, * function () { * console.log(dummy()); * } * ); * // you may also load scripts into iframes by replacing the third parameter * // with a reference to the iframe's document object. */ atropa.inject.script = function (id, srcURL, docref, callback) { "use strict"; atropa.supportCheck('inject'); var attributes, elementType, parentNod = null, onloadHandler, el; attributes = { "id" : id, "type" : "text/javascript", "src" : srcURL }; elementType = 'script'; onloadHandler = callback; el = atropa.inject.element( elementType, docref, parentNod, attributes, onloadHandler); return el; }; while(atropa.data.requirements.length > 0) { atropa.data.requirements.pop()(); } module.exports = atropa;