Browse Source

add user whitelist

Andrew Dolgov 2 years ago
parent
commit
9630632503
6 changed files with 7687 additions and 11 deletions
  1. 11 0
      background.js
  2. 7590 0
      lib/prototype.js
  3. 6 2
      manifest.json
  4. 22 9
      noselect.js
  5. 23 0
      options.html
  6. 35 0
      options.js

+ 11 - 0
background.js

@@ -0,0 +1,11 @@
+chrome.storage.sync.get('settings', function(val) {
+    if (typeof val.settings !== "undefined")
+        localStorage.settings = JSON.stringify(val.settings);
+});
+
+chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
+	if (request.method == "getSettings")
+			sendResponse({settings: localStorage['settings']});
+	else
+		sendResponse({});
+});

File diff suppressed because it is too large
+ 7590 - 0
lib/prototype.js


+ 6 - 2
manifest.json

@@ -1,9 +1,13 @@
 {
   "name": "Don't screw with selected text",
-  "version": "0.4",
+  "version": "0.5",
   "manifest_version": 2,
   "description": "Prevents sites from modifying selected text (typically adding read more... links)",
-  "permissions": [ "http://*/*", "https://*/*" ],
+  "permissions": [ "http://*/*", "https://*/*", "storage" ],
+  "background": {
+        "scripts": [ "background.js" ]
+    },
+  "options_page": "options.html",
    "content_scripts": [ {
       "all_frames": true,
       "js": [ "noselect.js" ],

+ 22 - 9
noselect.js

@@ -1,4 +1,4 @@
-var whitelist = [ "google.com", "youtu.be", "youtube.com", "forum.ixbt.com" ];
+var whitelist = [ "google.com", "youtu.be", "youtube.com", "forum.ixbt.com", "duckduckgo.com" ];
 
 function inject(f) {
     var script = document.createElement("script");
@@ -13,14 +13,27 @@ function removeSelectionStuff() {
     });
 }
 
-var whitelisted = false;
+chrome.runtime.sendMessage({method: "getSettings"}, function(response) {
+	try {
+		var settings = JSON.parse(response.settings);
+		var user_whitelist = settings.whitelist.split(/\n/);
 
-if (document.documentElement instanceof HTMLHtmlElement) {
-    for (var i = 0; i < whitelist.length; i++) {
-	if (location.href.indexOf(whitelist[i]) != -1) {
-		whitelisted = true;
+		whitelist = whitelist.concat(user_whitelist);
+	} catch (e) {
+		console.log(e);
 	}
-    }
 
-    if (!whitelisted) removeSelectionStuff();
-}
+	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();
+	}
+});
+
+

+ 23 - 0
options.html

@@ -0,0 +1,23 @@
+<html>
+<body>
+
+<script type="text/javascript" src="lib/prototype.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>
+
+<h1>Options</h1>
+
+<p><strong>Domain whitelist:</strong></p>
+
+<form method="post" id="options_form">
+<textarea cols="60" rows="10" name="whitelist"></textarea></p>
+
+<p><button type="submit">Save</button> <span id="message"></span></p>
+
+</form>

+ 35 - 0
options.js

@@ -0,0 +1,35 @@
+function save() {
+	var form = $("options_form");
+	var settings = form.serialize(true);
+
+	localStorage.settings = JSON.stringify(settings);
+
+	chrome.storage.sync.set({'settings' : settings}, function() {
+		console.log("settings saved");
+	});
+
+	$("message").innerHTML = "Data saved.";
+
+	return false;
+}
+
+window.onload = function() {
+
+	var form = $("options_form");
+
+	console.log("init options form: " + form);
+
+	form.onsubmit = function() { return save(); }
+
+	var settings;
+
+	try {
+		settings = JSON.parse(localStorage.settings);
+
+		form.whitelist.value = settings.whitelist;
+
+	} catch (e) {
+		console.warn(e);
+	}
+
+};