]> khamidou — GitHub mirrors - infinity-pools.git/commitdiff
basic timer functionality working
authorKarim Hamidou <khamidou@gmail.com>
Sat, 29 May 2021 22:29:02 +0000 (15:29 -0700)
committerKarim Hamidou <khamidou@gmail.com>
Sat, 29 May 2021 22:29:02 +0000 (15:29 -0700)
manifest.json
start.js

index 608b768b2f430e5a90385bd789639ebc21ca6643..f17bb8f383cf8efe34f4a43599f0edef6833e7d3 100644 (file)
@@ -1,15 +1,14 @@
 {
   "manifest_version": 2,
   "name": "Infinity Pools",
-  "version": "1.2",
+  "version": "1.3",
 
-  "description": "An extension to hide various feeds like Facebook's, Pocket, etc. ",
+  "description": "An extension to hide Twitter's feed after 30 mins every day.",
   "update_url": "https://khamidou.com/extensions/update.xml",
+  "permissions": ["storage"],
   "content_scripts": [
     {
-      "matches": ["*://*.facebook.com/*", "*://*.getpocket.com/*",
-                  "*://*.twitter.com/*", "*://*.reddit.com/*",
-                  "*://news.ycombinator.com/*"],
+      "matches": ["*://*.twitter.com/*"],
       "js": ["start.js"]
     }
   ]
index 02766efcc983d75a87981b5fbef734012155519e..a5ddbdc12c9cc0bce646b5d9d190a084d2a60e25 100644 (file)
--- a/start.js
+++ b/start.js
@@ -1,17 +1,4 @@
 // Strongly inspired by https://github.com/AntonOsika/newsfeed-eradicator/blob/master/erad.js
-var hideFacebookFeed = function() {
-  var divs = document.getElementsByTagName("div");
-  var item;
-
-  for (var i = 0; i < divs.length; i++) {
-    item = divs[i];
-    if (item != null && item.id.match("topnews_main_stream") != null) {
-      item.remove();
-    }
-  }
-}
-
-
 var hideAll = function(className) {
   var divs = document.getElementsByClassName(className);
   for (var i = 0; i < divs.length; i++) {
@@ -23,47 +10,114 @@ var hideAll = function(className) {
   }
 };
 
-var hidePocketFeed = function() {
-  hideAll("queue_togglesection_recommended");
-  hideAll("queue_explore_option");
+var getFormattedDate = function() {
+  var today = new Date();
+  var dd = String(today.getDate()).padStart(2, '0');
+  var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
+  var yyyy = today.getFullYear();
+
+  today = mm + '/' + dd + '/' + yyyy;
+  return today;
 }
 
+// In-page cache of the user's options
+var config = {};
 
-var hideTwitterFeed = function() {
-  hideAll("stream-items");
-  hideAll("home");
-}
+var refreshConfig = async function() {
+  // Initialize the form with the user's option settings
+  let p = new Promise(function(resolve, reject) {
+    chrome.storage.sync.get('config', (options) => {
+      resolve(options);
+    });
+  });
 
+  let data = await p;
+
+  let defaultConfig = {currentDay: getFormattedDate(), remainingMinutes: 15}
+  if (data.config == {} || data.config == undefined) {
+    Object.assign(config, data.config);
+    return;
+  }
 
-var hideRedditComments = function() {
-  console.log("Hiding reddit comments");
-  hideAll("commentarea");
+  if (data.config.currentDay != defaultConfig.currentDay) {
+    // if this is a new day, reset the config.
+    Object.assign(config, defaultConfig);
+    return;
+  else {
+    Object.assign(config, data.config);
+    return;
+  }
 }
 
+var saveConfig = async function(config) {
+  // Initialize the form with the user's option settings
+  let p = new Promise(function(resolve, reject) {
+    chrome.storage.sync.set({config}, () => {
+      resolve();
+    });
+  });
 
-var hideHNComments = function() {
-  hideAll("comment");
+  return p;
+}
+var createInfoDiv = function() {
+  elapsed_time_div = document.createElement('div');
+  elapsed_time_div.id = 'infinitypools_id';
+  elapsed_time_div.innerHTML = "" + config.remainingMinutes;
+  elapsed_time_div.style.position = 'fixed';
+  elapsed_time_div.style.bottom = '70px';
+  elapsed_time_div.style.left = '120px';
+  elapsed_time_div.style.color = 'darkblue';
+  elapsed_time_div.style.fontFamily = 'Helvetica Neue';
+  elapsed_time_div.style.fontSize = '30px';
+
+  document.body.appendChild(elapsed_time_div)
 }
 
+var hideTwitterFeed = async function() {
+  await refreshConfig();
+
+}
 
-var hideFeeds = function() {
+function hideFeeds() {
   var domain = document.domain;
 
-  if (domain.match(/facebook\.com/)) {
-    hideFacebookFeed();
-  } else if (domain.match(/getpocket\.com/)) {
-    hidePocketFeed();
-  } else if (domain.match(/twitter\.com/)) {
+  if (domain.match(/twitter\.com/)) {
     hideTwitterFeed();
-  } else if (domain.match(/reddit\.com/)) {
-    hideRedditComments();
-  } else if (domain.match(/news\.ycombinator\.com/)) {
-    hideHNComments();
   }
+}
 
+var previousState = 'active'
+async function mainEventHandler() {
+  console.log("mainEventHandler started");
+  if (document.hidden) {
+    previousState = 'hidden';
+    setTimeout(mainEventHandler, 10 * 1000);
+    return;
+  }
 
+  await refreshConfig();
+  console.log(config.remainingMinutes);
+  if (config.remainingMinutes <= 0) {
+    alert("you're at time!");
+    return;
+  }
+
+  if (previousState == 'active') {
+    // if we were active, decrement counter.
+    config.remainingMinutes -= 2;
+    await saveConfig(config);
+  }
 
+  previousState = 'active';
+  //setTimeout(mainEventHandler, 2 * 60 * 1000);
+  setTimeout(mainEventHandler, 10000);
 }
 
+async function main() {
+  await refreshConfig();
+  document.addEventListener('visibilityChange', mainEventHandler, false);
+
+  setTimeout(mainEventHandler, 100);
+}
 
-setTimeout(hideFeeds, 100);
+main();