Andrew Dolgov 1 year ago
commit
68b19defe1
7 changed files with 9765 additions and 0 deletions
  1. 1 0
      README
  2. 39 0
      background.js
  3. 48 0
      content.js
  4. 9597 0
      jquery.js
  5. 23 0
      manifest.json
  6. 21 0
      options.html
  7. 36 0
      options.js

+ 1 - 0
README

@@ -0,0 +1 @@
+Bloat-free user stylesheet injector for Chrome

+ 39 - 0
background.js

@@ -0,0 +1,39 @@
+chrome.storage.sync.get('user_css', function(val) {
+	console.log('got sync', val);
+
+	if (typeof val.user_css != 'undefined') {
+		$("#user_css").val(val.user_css);
+		localStorage['user_css'] = val.user_css;
+	}
+});
+
+chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
+	if (request.method == "getCSS") {
+		var href = request.href;
+
+		console.log('requested styles for', href);
+
+		var user_css = localStorage['user_css'];
+		var lines = user_css.split("\n");
+		var tmp = [];
+
+		var section = "";
+		for (var i = 0; i < lines.length; i++) {
+			var line = lines[i];
+
+			if (line.indexOf("##") == 0) {
+				section = line.substr(2).trim();
+				continue;
+			}
+
+			if (section == "" || section == "GLOBAL" || href.indexOf(section) != -1)
+				tmp.push(line);
+		}
+
+		sendResponse({user_css: tmp.join("\n")});
+
+	} else {
+		console.warn('unknown method', request.method);
+		sendResponse({});
+	}
+});

+ 48 - 0
content.js

@@ -0,0 +1,48 @@
+function inject_css(css) {
+
+	var style = $("<style>")
+		.attr('type', 'text/css')
+		.text(css);
+
+	console.log(style[0]);
+
+	$("head").append(style);
+
+//    document.documentElement.appendChild(script);
+}
+
+chrome.runtime.sendMessage({method: "getCSS", href: location.href}, function(response) {
+	console.log('got styles', response);
+
+	var user_css = response.user_css;
+
+	inject_css(user_css);
+
+});
+
+/*chrome.runtime.sendMessage({method: "getSettings"}, function(response) {
+	try {
+		var settings = JSON.parse(response.settings);
+		var user_whitelist = settings.whitelist
+			.split(/\n/)
+			.filter(function(v) { return v.length > 0 });
+
+		whitelist = whitelist.concat(user_whitelist);
+	} catch (e) {
+		//
+	}
+
+	var whitelisted = false;
+
+	if (document.documentElement instanceof HTMLHtmlElement) {
+		for (var i = 0; i < whitelist.length; i++) {
+			if (location.href.indexOf(whitelist[i]) != -1) {
+				whitelisted = true;
+			}
+		}
+
+		if (!whitelisted) removeSelectionStuff();
+	}
+}); */
+
+

File diff suppressed because it is too large
+ 9597 - 0
jquery.js


+ 23 - 0
manifest.json

@@ -0,0 +1,23 @@
+{
+	"applications": {
+		"gecko": { "id": "[email protected]", "update_url": "https://fakecake.org/xpi/updates.php" }
+	},
+  "name": "Bloat-free user stylesheets",
+  "version": "0.1",
+  "manifest_version": 2,
+  "description": "Injects user stylesheets, without bloat.",
+  "permissions": [ "http://*/*", "https://*/*", "storage" ],
+  "background": {
+        "scripts": [ "jquery.js", "background.js" ]
+    },
+  "options_ui": { 
+		"chrome_style": false,
+		"page": "options.html"
+	},
+    "content_scripts": [ {
+      "all_frames": true,
+      "js": [ "jquery.js", "content.js" ],
+      "matches": [ "http://*/*", "https://*/*" ],
+      "run_at": "document_start"
+   } ]
+}

+ 21 - 0
options.html

@@ -0,0 +1,21 @@
+<html>
+<body>
+
+<script type="text/javascript" src="jquery.js"></script>
+<script type="text/javascript" src="options.js"></script>
+
+<style type="text/css">
+body { margin : 1em; }
+#message { color : red; }
+fieldset { border-width : 0px; }
+fieldset label { width : 250px; font-weight : bold; display : inline-block; }
+</style>
+
+<p><strong>List of user styles:</strong></p>
+
+<form method="post" id="options">
+<textarea cols="80" rows="30" id="user_css"></textarea></p>
+
+<p><button type="submit">Save</button> <span id="message"></span></p>
+
+</form>

+ 36 - 0
options.js

@@ -0,0 +1,36 @@
+$(document).ready(function() {
+	console.log('init options');
+
+	chrome.storage.sync.get('user_css', function(val) {
+		console.log('got sync', val);
+
+		if (typeof val.user_css != 'undefined') {
+			$("#user_css").val(val.user_css);
+		}
+	});
+
+	var editor = $("#user_css");
+
+	editor.attr('placeholder',
+		"## GLOBAL\n" +
+		"{ global rules go here }\n" +
+		"## example.com\n"+
+		"{ rules for example.com}\n");
+
+	editor.attr('title', editor.attr('placeholder'));
+
+	$("#options").on('submit', function() {
+		var user_css = editor.val();
+
+		localStorage['user_css'] = user_css;
+
+		chrome.storage.sync.set({user_css: user_css}, function() {
+			$("#message").html("Data saved");
+		});
+
+		return false;
+	});
+
+
+});
+