node_modules\atropa-inject\src\atropa-inject.js

Maintainability

62.08

Lines of code

262

Created with Raphaël 2.1.002550751002022-11-17

2022-11-17
Maintainability: 62.08

Created with Raphaël 2.1.00751502253002022-11-17

2022-11-17
Lines of Code: 262

Difficulty

20.58

Estimated Errors

0.56

Function weight

By Complexity

Created with Raphaël 2.1.0<anonymous>2

By SLOC

Created with Raphaël 2.1.0<anonymous>25
1
/// <reference path="../docs/vsdoc/OpenLayersAll.js"/>
2
var atropa = require('atropa-header');
3
atropa.setAsOptionalArg = require('atropa-setAsOptionalArg').setAsOptionalArg;
4
/*jslint
5
    indent: 4,
6
    maxerr: 50,
7
    white: true,
8
    browser: true,
9
    devel: true,
10
    plusplus: true,
11
    regexp: true
12
*/
13
/*global atropa */
14
// end header
15
 
16
 
17
atropa.requires(
18
    'inject',
19
    function () {
20
        "use strict";
21
        if(document.createElement === undefined) {
22
            return false;
23
        }
24
        return true;
25
    }
26
);
27
 
28
/**
29
 * Contains tools for injecting elements and assemblies.
30
 * into the page.
31
 * @author <a href="mailto:matthewkastor@gmail.com">
32
 *  Matthew Christopher Kastor-Inare III </a><br />
33
 *  ☭ Hial Atropa!! ☭
34
 * @version 20130308
35
 * @namespace Contains tools for injecting elements and assemblies.
36
 * @requires atropa.data
37
 * @requires atropa.supportCheck
38
 * @requires atropa.setAsOptionalArg
39
 */
40
atropa.inject = {};
41
/**
42
 * Generic Element Injector.
43
 * @author <a href="mailto:matthewkastor@gmail.com">
44
 *  Matthew Christopher Kastor-Inare III </a><br />
45
 *  ☭ Hial Atropa!! ☭
46
 * @version 20120909
47
 * @param {String} elementType The type of element to be injected.
48
 * @param {HTML DOM Document} docref Optional. A reference to the document to
49
 *  target, defaults to <code>document</code>.
50
 * @param {DOM Node} parentNod Optional. A reference to the parent node to
51
 *  target, defaults to <code>docref.body</code>.
52
 * @param {Object} attributes Optional. An object whose properties are names of
53
 *  HTML attributes, defaults to <code>{}</code>. The value of these properties
54
 *  are to be strings representing the values of the HTML attributes as they are
55
 *  to be applied to the injected element.
56
 * @example Example attributes object :
57
 *
58
 * attributesObj = {
59
 *     "id" : "elementID",
60
 *     "class" : "classy"
61
 * };
62
 * @param {Function} onloadHandler Optional. If the element being injected will
63
 *  fire a load event, this function will be called. Defaults to
64
 *  <code>function () {}</code>.
65
 * @param {Function} callback Optional. This function will be called just before
66
 *  the element is to be appended to the page. The callback will receive the
67
 *  element in its current state for any additional processing to be done prior
68
 *  to it's attachment on callback completion. Defaults to
69
 *  <code>function () {}</code>.
70
 * @return {HTML Element} Returns a reference to the HTML Element created and
71
 *  injected.
72
 * @see <a href="http://www.w3.org/Security/wiki/Same_Origin_Policy">
73
 * http://www.w3.org/Security/wiki/Same_Origin_Policy</a>
74
 * @example
75
 *  // this will inject a div element into the document body.
76
 *  var el = atropa.inject.element ('div');
77
 *  
78
 *  // This will inject a div with the id "myId" into the element referenced by
79
 *  // "container"
80
 *  var el = atropa.inject.element (
81
 *      'div', document, container, { 'id': 'myId' }, null, null
82
 *  );
83
 *  
84
 *  // this will inject a div into the document of an iframe referenced with "fdoc"
85
 *  // Just before the div is injected the callback will be called and the element
86
 *  // may be augmented. When the callback returns the element will be injected.
87
 *  var fdoc = document.getElementById('someFrame').contentWindow.document;
88
 *  
89
 *  var el = atropa.inject.element (
90
 *      'div', fdoc, fdoc.body, { 'id': 'myId' },
91
 *      null,
92
 *      function (myDiv) {
93
 *          myDiv.textContent = 'I could have attached event handlers';
94
 *      }
95
 *  );
96
 *  
97
 *  // this will inject an iframe into the document
98
 *  // once the iframe's document has finished loading the onload handler will be
99
 *  // called. If the document and the iframe are on the same domain, scripts on
100
 *  // the frame and the parent document will be able to commuincate with each
101
 *  // other.
102
 *  function iframeHasLoaded (message) {
103
 *      console.log(message);
104
 *  }
105
 *  
106
 *  var el = atropa.inject.element (
107
 *      'iframe', document, document.body,
108
 *      { 'id': 'myId', 'src' : 'http://localhost' },
109
 *      function () {
110
 *          iframeHasLoaded('hey look at that, the frame is ready!');
111
 *          // what could I do with the frame? anything I want!
112
 *      },
113
 *      null
114
 *  );
115
 */
116
atropa.inject.element = function (
117
    elementType, docref, parentNod, attributes, onloadHandler, callback
118
) {
119
    "use strict";
120
    atropa.supportCheck('inject');
121
    
122
    var el,
123
    x;
124
    docref = atropa.setAsOptionalArg(document, docref);
125
    parentNod = atropa.setAsOptionalArg(docref.body, parentNod);
126
    attributes = atropa.setAsOptionalArg({}, attributes);
127
    onloadHandler = atropa.setAsOptionalArg(atropa.nop, onloadHandler);
128
    callback = atropa.setAsOptionalArg(atropa.nop, callback);
129
    
130
    el = docref.createElement(elementType);
131
    for (x in attributes) {
132
        if (attributes.hasOwnProperty(x)) {
133
            el.setAttribute(x, attributes[x]);
134
        }
135
    }
136
    el.addEventListener('load', onloadHandler, true);
137
    callback(el);
138
    parentNod.appendChild(el);
139
    return el;
140
};
141
/**
142
 * Hidden Iframe Injector.
143
 * @author <a href="mailto:matthewkastor@gmail.com">
144
 *  Matthew Christopher Kastor-Inare III </a><br />
145
 *  ☭ Hial Atropa!! ☭
146
 * @version 20130308
147
 * @param {String} id The id of the element to be injected.
148
 * @param {String} srcUrl The URL to load in the iframe.
149
 * @param {HTML DOM Document} docref Optional. Reference to the document to
150
 *  inject the iframe in. Defaults to document.
151
 * @param {Function} onloadHandler Optional. The onload handler for the iframe.
152
 * @param {DOM Node} parentNod Optional. Referenct to the parent node to
153
 *  append the iframe to. Defaults to docref.body
154
 * @param {Function} callback Optional. Callback function for preprocessing
155
 *  the iframe prior to injection. Called with a reference to the iframe.
156
 * @return {HTML Element} Returns a reference to the HTML Element created and
157
 *  injected.
158
 * @see atropa.inject.element
159
 * @see <a href="http://www.w3.org/Security/wiki/Same_Origin_Policy">
160
 * http://www.w3.org/Security/wiki/Same_Origin_Policy</a>
161
 * @example
162
 *  el = atropa.inject.hiddenFrame(
163
 *      'injectHiddenFrame3',
164
 *      'http://localhost/',
165
 *      null,
166
 *      function () {
167
 *          console.log('hey look at that, the frame is ready!');
168
 *      },
169
 *      null,
170
 *      null
171
 *  );
172
 */
173
atropa.inject.hiddenFrame = function (
174
    id, srcURL, docref, onloadHandler, parentNod, callback
175
) {
176
    "use strict";
177
    atropa.supportCheck('inject');
178
    
179
    return atropa.inject.element(
180
        'iframe',
181
        docref,
182
        parentNod,
183
        {
184
            "id" : id,
185
            "src" : srcURL,
186
            "width" : "0px",
187
            "height" : "0px",
188
            "border" : "0px"
189
        },
190
        onloadHandler,
191
        callback
192
    );
193
};
194
/**
195
 * Script Injector.
196
 * @author <a href="mailto:matthewkastor@gmail.com">
197
 *  Matthew Christopher Kastor-Inare III </a><br />
198
 *  ☭ Hial Atropa!! ☭
199
 * @version 20120909
200
 * @param {String} id The id of the element to be injected.
201
 * @param {String} srcUrl The URL where the script is located.
202
 * @param {HTML DOM Document} docref Optional. The document to inject the
203
 *  script into. Defaults to document.
204
 * @param {Function} callback Optional. A function to execute once the script
205
 *  has loaded. Defaults to function () {};
206
 * @return {HTML Element} Returns a reference to the HTML Element created and
207
 *  injected.
208
 * @see atropa.inject.element
209
 * @see <a href="http://www.w3.org/Security/wiki/Same_Origin_Policy">
210
 * http://www.w3.org/Security/wiki/Same_Origin_Policy</a>
211
 * @example
212
 *  // Given a script "dummy.js" located at "http://localhost/dummy.js"
213
 *  // you can fetch the script and execute functions from within it
214
 *  // as soon as it has loaded into the page.
215
 *  
216
 *  // contents of "dummy.js"
217
 *  function dummy() {
218
 *      return 'dummy';
219
 *  }
220
 *  
221
 *  // injecting "dummy.js" into any page. The script tag isn't restricted by
222
 *  // the same origin policy. Host your script anywhere and inject it to any
223
 *  // page on the net that you want to.
224
 *  el = atropa.inject.script(
225
 *      'injectScript',
226
 *      'http://localhost/',
227
 *      document,
228
 *      function () {
229
 *          console.log(dummy());
230
 *      }
231
 *  );
232
 *  // you may also load scripts into iframes by replacing the third parameter
233
 *  // with a reference to the iframe's document object.
234
 */
235
atropa.inject.script = function (id, srcURL, docref, callback) {
236
    "use strict";
237
    atropa.supportCheck('inject');
238
    
239
    var attributes,
240
    elementType,
241
    parentNod = null,
242
    onloadHandler,
243
    el;
244
    attributes = {
245
        "id" : id,
246
        "type" : "text/javascript",
247
        "src" : srcURL
248
    };
249
    elementType = 'script';
250
    onloadHandler = callback;
251
    el = atropa.inject.element(
252
        elementType, docref, parentNod, attributes, onloadHandler);
253
    return el;
254
};
255
 
256
 
257
 
258
 
259
while(atropa.data.requirements.length > 0) {
260
    atropa.data.requirements.pop()();
261
}
262
module.exports = atropa;