-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgoogle.chrome.user.js
89 lines (76 loc) · 3.07 KB
/
google.chrome.user.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
/// Dynamic script loader for lou-extensions in Google Chrome.
"use strict";
/**
* Dynamic injection of javascript and css files based on the list
* of 'web_accessible_resources' in the manifest.json file
* @namespace lou_extensions
* @version 0.3.0
*/
(function (window, undefined) {
var lou_extensions = window.lou_extensions = {};
/**
* Injects a link to a javascript file in the HEAD of a document.
* @param {string} url - location of the javascript file to inject.
* */
lou_extensions.injectScriptLink = function (url, mimetype) {
var scriptElement = document.createElement('script');
scriptElement.setAttribute("type", mimetype);
scriptElement.setAttribute("src", url);
if (typeof scriptElement != "undefined")
document.getElementsByTagName("head")[0].appendChild(scriptElement);
};
/**
* Injects a link to a stylesheet in the HEAD of a document.
* @param {string} url - location of the javascript file to inject.
* */
lou_extensions.injectLinkStylesheet = function (url, mimetype) {
var linkElement = document.createElement("link");
linkElement.setAttribute("rel", "stylesheet");
linkElement.setAttribute("type", mimetype);
linkElement.setAttribute("href", url);
if (typeof linkElement != "undefined")
document.getElementsByTagName("head")[0].appendChild(linkElement);
};
/**
* Analyse the manifest.json to find the accessible js and css and injects them.
* @param manifest
*/
lou_extensions.loadExtensions = function (manifest) {
if (manifest != null && manifest.web_accessible_resources != null) {
for (var i = 0; i < manifest.web_accessible_resources.length; i++) {
try {
var resource = manifest.web_accessible_resources[i];
if (resource.indexOf('.js', resource.length - 3) !== -1) {
lou_extensions.injectScriptLink(chrome.extension.getURL(resource), "text/javascript");
} else if (resource.indexOf('.css', resource.length - 4) !== -1) {
lou_extensions.injectLinkStylesheet(chrome.extension.getURL(resource),"text/css");
}
} catch (e) {
GM_log('[lou-extensions] - ' + e)
}
}
}
};
window["lou_extensions"] = lou_extensions;
})(window);
/**
* Main function
*/
try {
GM_log('[lou-extensions] Loading LOU extensions');
if (chrome != undefined && chrome.extension != undefined) {
// load extensions manifest.json file
var url = chrome.extension.getURL('manifest.json');
GM_xmlhttpRequest({
"url":url,
"onload":function (xhr) {
var manifest = JSON.parse(xhr.responseText);
lou_extensions.loadExtensions(manifest);
}
});
} else {
GM_log('[lou-extensions] Chrome extensions not available. No lou-extensions were injected');
}
} catch (e) {
GM_log('[lou-extensions]' + e);
}