Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

working test module

  • Loading branch information...
commit cc5602855dcd4cb3ca3fb42d37c975ab8952bf7f 1 parent bf1c1a8
@anstosa authored
View
15 js/init.js
@@ -19,12 +19,12 @@ function init() {
return;
}
if (lastCheck != null) {
- lastCheck.removeEventListener("DOMSubtreeModified", runModules, false);
+ lastCheck.removeEventListener("DOMSubtreeModified", injectBody, false);
}
lastCheck = getTarget()
if (lastCheck != null) {
- lastCheck.addEventListener("DOMSubtreeModified", runModules, false);
- runModules(); // fencepost
+ lastCheck.addEventListener("DOMSubtreeModified", injectBody, false);
+ //runModules(); // fencepost
}
}
@@ -35,10 +35,11 @@ chrome.extension.sendRequest({name: "getModules", target: window.location.toStri
if (bootstrapTarget != null) {
window.addEventListener("DOMSubtreeModified", init, false);
} else {
- debug("no bootstrap target. Skipping load...")
- runModules();
+ debug("no bootstrap target. Skipping load...");
+ injectBody();
}
- injectCSS(staticStyles); // static styles don't need page load
- chrome.extension.sendRequest({name: "activateBrowserAction"}, false);
+ buildModules();
+ injectHead();
+ chrome.extension.sendRequest({name: "activateBrowserAction"}, function(response){});
}
});
View
54 js/inject.js
@@ -5,34 +5,36 @@
* Licensed under GNU GPL v3
**/
- function injectJS(scripts) {
- debug("injecting JavaScript...");
- var m_bodies = document.getElementsByTagName("body");
- if (m_bodies.length > 0) {
- var m_scriptBlock = document.createElement("script");
- m_scriptBlock.type = "text/javascript";
- m_scriptBlock.appendChild(document.createTextNode(scripts));
- m_bodies[0].appendChild(m_scriptBlock);
+ function injectBody() {
+ if (bodyScripts.length > 0) {
+ debug("injecting body JavaScript...");
+ var bodies = document.getElementsByTagName("body");
+ if (bodies.length > 0) {
+ var scriptBlock = document.createElement("script");
+ scriptBlock.type = "text/javascript";
+ scriptBlock.appendChild(document.createTextNode(bodyScripts));
+ bodies[0].appendChild(scriptBlock);
+ }
}
}
-function injectCSS(styles) {
- debug("injecting CSS...");
- var m_heads = document.getElementsByTagName("head");
- if (m_heads.length > 0) {
- var m_styleBlock = document.createElement("style");
- m_styleBlock.type = "text/css";
- m_styleBlock.appendChild(document.createTextNode(styles));
- m_heads[0].appendChild(m_styleBlock);
+function injectHead() {
+ if (styles.length > 0 || headScripts.length > 0) {
+ debug("injecting CSS...");
+ var heads = document.getElementsByTagName("head");
+ if (heads.length > 0) {
+ if (styles.length > 0) {
+ var styleBlock = document.createElement("style");
+ styleBlock.type = "text/css";
+ styleBlock.appendChild(document.createTextNode(styles));
+ heads[0].appendChild(styleBlock);
+ }
+ if (headScripts.length > 0) {
+ var scriptBlock = document.createElement("script");
+ scriptBlock.type = "text/javascript";
+ scriptBlock.appendChild(document.createTextNode(headScripts));
+ heads[0].appendChild(scriptBlock);
+ }
+ }
}
-}
-function injectAll() {
- debug("startimg injection...");
- if (m_scripts != null) {
- injectJS(m_scripts);
- }
- if (m_dynamicStyles != null) {
- injectCSS(m_dynamicStyles);
- }
- // NOTE: static styles injected in bootstrap
}
View
4 js/libs/Module.js
@@ -7,9 +7,7 @@
var name = "",
includes = "",
- isEnabled = true,
- css = new Array(),
- js = new Array();
+ isEnabled = true;
function Module(params) {
debug("creating module: " + params.name);
View
68 js/libs/background.js
@@ -24,15 +24,15 @@ function checkUpdate() {
}
}
-function loadOptions() {
- debug("loading options...");
+function loadPrefs() {
+ debug("loading preferences...");
var value;
- for (var option in options) {
- value = localStorage[option];
+ for (var key in prefs) {
+ value = localStorage[key];
if (value != null) {
- options[option] = value;
+ prefs[key] = value;
} else {
- localStorage[option] = options[option];
+ localStorage[key] = prefs[key];
}
}
}
@@ -40,7 +40,7 @@ function loadOptions() {
function loadModules() {
debug("loading modules...");
if ((modules = localStorage["modules"]) != null) {
- modules = modules.split("|||");
+ modules = modules.split("|||");
try {
for (var i = 0, l = modules.length; i < l; i++) {
modules[i] = JSON.parse(modules[i]);
@@ -56,15 +56,29 @@ function loadModules() {
author: "Ansel Santosa",
includes: "http://minimalistsuite.com/*",
isEnabled: true,
- css: [
- "h1 {",
- " color: #09f;",
- "}"
- ],
- js: [
- "console.log(\"Subtract until it breaks\")",
- "console.log(\"The way of the Minimalist\")"
- ],
+ options: [
+ {
+ description: "theme",
+ isEnabled: true,
+ head: {
+ css: [
+ "h1 {",
+ " color: #09f;",
+ "}"
+ ],
+ js: [
+ "var line1 = \"Subtract until it breaks\",",
+ "\tline2 = \"The way of the Minimalist\";"
+ ]
+ },
+ load: {
+ js: [
+ "console.log(line1);",
+ "console.log(line2);"
+ ]
+ }
+ }
+ ]
});
save();
}
@@ -87,11 +101,12 @@ function activateBrowserAction(tab) {
debug("activating browser action for " + tab.url);
chrome.browserAction.setIcon({path: "img/icons/icon19_active.png", tabId: tab.id})
}
-
+/*
function deactivateBrowserAction(tab) {
debug("deactivating browser action for " + tab.url);
chrome.browserAction.setIcon({path: "img/icons/icon19.png", tabId: tab.id})
}
+*/
String.prototype.copy = function() {
var copyTextarea = document.createElement('textarea');
@@ -103,9 +118,9 @@ String.prototype.copy = function() {
};
function save() {
- debug("saving options...");
- for (var option in options) {
- localStorage[option] = options[option];
+ debug("saving preferences...");
+ for (var pref in prefs) {
+ localStorage[pref] = prefs[pref];
}
debug("saving modules...");
var modulesString = new Array();
@@ -129,8 +144,19 @@ function isMatch(target, includeString) {
}
}
+function arrayReplacer(key, value) {
+ if (Array.isArray(value)) {
+ for (var i = 0, l = value.length; i < l; i++) {
+ value[i] = JSON.stringify(value[i], arrayReplacer);
+ }
+ return value.join("|||");
+ } else {
+ return value;
+ }
+}
+
function debug(message) {
- if (options.isDebugging) {
+ if (prefs.isDebugging) {
console.log("Minimalist: " + message);
}
}
View
6 js/libs/general.js
@@ -1,6 +0,0 @@
-/**
- * Helper methods for Minimalist
- *
- * © 2011 Ansel Santosa
- * Licensed under GNU GPL v3
- **/
View
31 js/libs/runtime.js
@@ -5,8 +5,35 @@
* Licensed under GNU GPL v3
**/
- function debug(message) {
- if (options.isDebugging) {
+function debug(message) {
+ if (prefs.isDebugging) {
console.log("Minimalist: " + message);
}
+}
+
+function buildStyles(options) {
+ var styleData;
+ for (var i = 0, l = options.length; i < l; i++) {
+ if ((styleData = options[i].head.css) != null && options[i].isEnabled) {
+ for (var j = 0, e = styleData.length; j < e; j++) {
+ styles += "\n" + styleData[j];
+ }
+ }
+ }
+}
+
+function buildScripts(options) {
+ var scriptData;
+ for (var i = 0, l = options.length; i < l; i++) {
+ if (scriptData = options[i].head.js) {
+ for (var j = 0, e = scriptData.length; j < e; j++) {
+ headScripts += "\n" + scriptData[j];
+ }
+ }
+ if (scriptData = options[i].load.js) {
+ for (var j = 0, e = scriptData.length; j < e; j++) {
+ headScripts += "\n" + scriptData[j];
+ }
+ }
+ }
}
View
20 js/minimalist.js
@@ -8,18 +8,22 @@
// var modules [init.js]
/* === SETUP === */
-var m_scripts,
- m_staticStyles,
- m_dynamicStyles,
- options;
+var bodyScripts = "",
+ headScripts = "",
+ prefs,
+ styles = "";
-chrome.extension.sendRequest({name: "getOptions"}, function(response) {
- options = response.options;
+chrome.extension.sendRequest({name: "getPrefs"}, function(response) {
+ prefs = response.prefs;
});
/* === END SETUP === */
/* === CONSTRUCTION === */
-function runModules() {
- injectAll();
+function buildModules() {
+ for (var i = 0, l = modules.length; i < l; i++) {
+ var moduleOptions = modules[i].options;
+ buildStyles(moduleOptions);
+ buildScripts(moduleOptions);
+ }
}
/* === END CONSTURCTION === */
View
5 manifest.json
@@ -14,7 +14,7 @@
"background_page": "background.html",
"content_scripts": [ {
"matches": [ "http://*/*", "https://*/*" ],
- "js": [ "js/minimalist.js", "js/inject.js", "js/init.js" ],
+ "js": [ "js/libs/jquery.js", "js/libs/runtime.js" ],
"run_at": "document_start",
"all_frames": true
// }, {
@@ -27,7 +27,8 @@
// "run_at": "document_start"
}, {
"matches": [ "http://*/*", "https://*/*" ],
- "js": [ "js/libs/jquery.js", "js/libs/runtime.js", "js/libs/general.js" ],
+ "js": [ "js/minimalist.js", "js/inject.js", "js/init.js" ],
+ "run_at": "document_start",
"all_frames": true
} ],
"options_page": "options.html",
Please sign in to comment.
Something went wrong with that request. Please try again.