From 90bc5d871960543e0bfbc579d6203a27795300e8 Mon Sep 17 00:00:00 2001 From: Karim Hamidou Date: Sat, 29 May 2021 15:29:02 -0700 Subject: [PATCH] basic timer functionality working --- manifest.json | 9 ++-- start.js | 126 +++++++++++++++++++++++++++++++++++--------------- 2 files changed, 94 insertions(+), 41 deletions(-) diff --git a/manifest.json b/manifest.json index 608b768..f17bb8f 100644 --- a/manifest.json +++ b/manifest.json @@ -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"] } ] diff --git a/start.js b/start.js index 02766ef..a5ddbdc 100644 --- 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(); -- 2.43.0