I decided to venture a bit into creating a chrome extension, and decided to implement a simple extension which prevents me from visiting facebook / linkedin more than 5 times a day.
However, i keep getting a number of errors, which I cant seem to debug. They are:
It's strange how it says cannot read undefined when it seems to be defined in the chrome API.
Here are the files I used:
manifest.json
{
"manifest_version": 2,
"name": "Distract Me Not",
"version": "0.1",
"permissions": ["webNavigation"],
"content_scripts": [
{
"matches": [
"https://*.facebook.com/","https://*.linkedin.com/"
],
"js": ["content.js","jquery3.1.1.js","background.js"]
}
]
}
content.js
chrome.webNavigation.onCompleted.addListener(function(details) {
chrome.tabs.executeScript(details.tabId, {
file: 'background.js'
});
});
background.js
function getDate(){
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10) {
dd='0'+dd
}
if(mm<10) {
mm='0'+mm
}
today = dd+'/'+mm+'/'+yyyy;
return(today);
}
function main() {
appName = $.getJSON("settings.json" , function(json){
return json["appName"];
});
websiteLimits = $.getJSON("settings.json" , function(json){
return json["websiteLimits"];
});
currentPage = window.location.hostname;
currentWebsiteLimit = websiteLimits[currentPage];
checkFor = appName + getDate();
item = localStorage.getItem(checkFor);
if (item) {
item = Number(item);
if (item > currentWebsiteLimit) {
document.write("Sorry! Your limit for " + currentPage + " has been reached for today.");
return;
}
localStorage.setItem(checkFor,item+1);
return;
}
localStorage.setItem(checkFor,1);
}
main();
settings.json
{"appName":"distract me not",
"websiteLimits":{"facebook.com":5,
"linkedin.com":5}
}
It seems fairly straightforward, yet it alludes me. I'm probably missing something simple, and would love some guidance on how to solve this!
Thank you! :)
UPDATE:
I realized the files should be in the other order, and that i didnt clear the previous errors so I was misled that the same errors were happening.
However,the script doesn't seem to be counting properly (the alert line in content.js alerts twice for one reload). Is there a way around this? Also, I see $.getJSON does not work to get local files. Is there a way i can abstract out settings to another json and load it, or is this the only way?
thanks alot! :)
manifest.json
{
"manifest_version": 2,
"name": "Distract Me Not",
"version": "0.1",
"permissions": ["webNavigation","tabs","https://*.facebook.com/","https://*.linkedin.com/"],
"background":{
"scripts":["background.js"]
},
"content_scripts": [
{
"matches": [
"https://*.facebook.com/","https://*.linkedin.com/"
],
"js": ["jquery3.1.1.js","content.js"]
}
]
}
content.js
function getDate(){
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10) {
dd='0'+dd
}
if(mm<10) {
mm='0'+mm
}
today = dd+'/'+mm+'/'+yyyy;
return(today);
}
function main(){
var settings = {"appName":"distract me not",
"websiteLimits":{"www.facebook.com":5,
"www.linkedin.com":5}
};
appName = settings["appName"];
websiteLimits = settings["websiteLimits"];
currentPage = window.location.hostname;
currentWebsiteLimit = websiteLimits[currentPage];
alert(currentPage);
checkFor = appName + getDate();
item = localStorage.getItem(checkFor);
if (item) {
item = Number(item);
if (item > currentWebsiteLimit) {
document.write("Sorry! Your limit for " + currentPage + " has been reached for today.");
return;
}
localStorage.setItem(checkFor,item+1);
return;
}
localStorage.setItem(checkFor,1);
}
main();
background.js
chrome.webNavigation.onCompleted.addListener(function(details) {
chrome.tabs.executeScript(details.tabId, {
file: 'content.js'
});
});