-
Notifications
You must be signed in to change notification settings - Fork 2
/
inject-some.js
96 lines (80 loc) · 3.08 KB
/
inject-some.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
var injectsome = (function (injectsomeInstance, undefined) {
/**
* Contains functions to inject blocks of content into the HTML page
*/
injectsomeInstance.content = (function(contentInstance) {
/**
* Injects a block of javascript into a new script tag in the HEAD of a document.
* @param {string} jsContent - javascript code to inject.
* @param {string} scriptId - Id of the script tag that will be injected.
*/
contentInstance.script = function(jsContent, scriptId) {
var existingElement = document.getElementById(scriptId);
if(existingElement !== null && existingElement !== undefined) {
console.log("[injectsome][content] Element with id " + scriptId + " already exists. Skipping.");
return;
}
var scriptElement = document.createElement('script');
scriptElement.setAttribute("type", "text/javascript");
scriptElement.setAttribute("id", scriptId);
scriptElement.innerHTML = jsContent;
document.head.appendChild(scriptElement);
}
/**
* Appends a block of HTML to the body tag
* @param {string} htmlContent - HTML to inject.
*/
contentInstance.html = function(htmlContent) {
var div = document.createElement('div');
div.innerHTML = htmlContent;
while (div.children.length > 0) {
document.body.appendChild(div.children[0]);
}
}
/**
* Appends a block of css to the head tag
* @param {string} cssContent - CSS to inject.
* @param {string} cssId - Id of the style tag that will be injected.
*/
contentInstance.css = function(cssContent, cssId) {
var existingElement = document.getElementById(cssId);
if(existingElement !== null && existingElement !== undefined) {
console.log("[injectsome][content] Element with id " + cssId + " already exists. Skipping.");
return;
}
var styleElement = document.createElement('style');
styleElement.setAttribute("id", cssId);
styleElement.innerHTML = cssContent;
document.head.appendChild(styleElement);
}
return contentInstance;
}(injectsomeInstance.content || {}));
/**
* Contains functions to inject links to external content into the HTML page
*/
injectsomeInstance.links = (function(linksInstance) {
/**
* Injects a link to a javascript file in the HEAD of a document.
* @param {string} url - location of the javascript file to inject.
*/
linksInstance.script = function(url, mimetype) {
var scriptElement = document.createElement('script');
scriptElement.setAttribute("type", mimetype);
scriptElement.setAttribute("src", url);
document.head.appendChild(scriptElement);
};
/**
* Injects a link to a stylesheet in the HEAD of a document.
* @param {string} url - location of the javascript file to inject.
*/
linksInstance.stylesheet = function(url, mimetype) {
var linkElement = document.createElement("link");
linkElement.setAttribute("rel", "stylesheet");
linkElement.setAttribute("type", mimetype);
linkElement.setAttribute("href", url);
document.head.appendChild(linkElement);
};
return linksInstance;
}(injectsomeInstance.links || {}));
return injectsomeInstance;
}(injectsome || {}));