Skip to content

Commit

Permalink
Added always add to autorun functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ZachSaucier committed Jul 16, 2018
1 parent 7e5004d commit 3908ad7
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 52 deletions.
88 changes: 76 additions & 12 deletions background.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
function isEmpty(obj) {
return Object.keys(obj).length === 0;
}
function checkArrayForString(arr, string) {
for(let i = 0; i < arr.length; i++) {
if(arr[i].indexOf(string) > -1) {
console.log("test");
return true;
}
};
return false;
}

function startJustRead(tab) {
var tabId = tab ? tab.id : null; // Defaults to the current tab
chrome.tabs.executeScript(tabId, {
Expand All @@ -9,6 +22,13 @@ function startJustRead(tab) {
chrome.browserAction.setBadgeBackgroundColor({color:[242, 38, 19, 230]});
chrome.browserAction.setBadgeText({text:"on"});

// Check if we need to add the site to JR's autorun list
chrome.storage.sync.get("alwaysAddAR", function(result) {
if(result && result["alwaysAddAR"]) {
addSiteToAutorunList(null, tab);
}
});

setTimeout(function() {
chrome.browserAction.setBadgeText({text:""});
}, 2000);
Expand Down Expand Up @@ -67,6 +87,45 @@ function createLinkCM() {
}
});
}
function createAutorunCM() {
// Create an entry to allow user to open a given link using Just read
autorunCMId = chrome.contextMenus.create({
title: "Add this site to Just Read Plus' auto-run list",
id: "autorunCM",
contexts:["page"],
onclick: addSiteToAutorunList
});
}
function addSiteToAutorunList(info, tab) {
chrome.storage.sync.get('auto-enable-site-list', function(result) {
let url = new URL((info != null && info.pageUrl) || tab.url);
let entry;
if(url.pathname !== "/"
&& url.pathname !== "") {
entry = url.hostname + "/.+";
} else {
entry = url.hostname;
}

let currentDomains = result['auto-enable-site-list'];

if(!isEmpty(currentDomains)) {
if(!currentDomains.includes(entry)) {
chrome.storage.sync.set({
'auto-enable-site-list': [...currentDomains, entry],
}, function() {
if(checkArrayForString(currentDomains, url.hostname)) {
console.log("Auto-run entry added.\n\nWarning: An auto-run entry with the same hostname has already been added. Be careful to not add two duplicates.");
} else {
console.log('Auto-run entry added.');
}
});
} else {
console.log("Entry already exists inside of Just Read Plus' auto-run list. Not adding new entry.")
}
}
});
}


var pageCMId = highlightCMId = linkCMId = undefined;
Expand Down Expand Up @@ -107,13 +166,24 @@ function updateCMs() {
linkCMId = undefined;
}
}
}
} else if(key === "enable-autorunCM") {
if(result[key]) {
if(typeof autorunCMId == "undefined")
createAutorunCM();
} else {
if(typeof autorunCMId != "undefined") {
chrome.contextMenus.remove("autorunCM");
autorunCMId = undefined;
}
}
}
}

if(size === 0) {
createPageCM();
createHighlightCM();
createLinkCM();
createAutorunCM();
}
});
}
Expand All @@ -139,10 +209,12 @@ chrome.commands.onCommand.addListener(function(command) {
});

// Listen for requests to open the options page
chrome.runtime.onMessage.addListener(function(data, sender) {
if(data === "Open options") {
chrome.runtime.onMessage.addListener(function(request, sender) {
if(request === "Open options") {
chrome.runtime.openOptionsPage();
}
} else if(request.updateCMs === "true") {
updateCMs();
}
});

// Create an entry to allow user to select an element to read from
Expand All @@ -154,14 +226,6 @@ chrome.contextMenus.create({
}
});

chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if(request.updateCMs === "true") {
updateCMs();
}
}
);

chrome.tabs.onUpdated.addListener( function (tabId, changeInfo, tab) {
if (changeInfo.status == 'loading') {
// Auto enable on sites specified
Expand Down
68 changes: 32 additions & 36 deletions content_script.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,11 @@ function startDeleteElement(doc) {
var elem = e.target;

if(!elem.classList.contains("simple-container")
&& !elem.classList.contains("simple-close")
&& !elem.classList.contains("simple-print")
&& !elem.parentNode.classList.contains("simple-print")
&& !elem.classList.contains("simple-control")
&& !elem.classList.contains("simple-add-comment")
&& !elem.classList.contains("simple-comments")
&& !elem.classList.contains("simple-edit")
&& !elem.classList.contains("simple-edit-theme")
&& !elem.parentNode.classList.contains("simple-edit-theme")
&& !elem.classList.contains("simple-delete")
&& !elem.parentNode.classList.contains("simple-delete")
&& (elem.parentNode.classList && !elem.parentNode.classList.contains("simple-control"))
&& doc.body != elem
&& doc.documentElement != elem
&& elem.tagName !== "path"
Expand All @@ -146,14 +143,11 @@ function startDeleteElement(doc) {
selected = e.target;

if(!selected.classList.contains("simple-container")
&& !selected.classList.contains("simple-close")
&& !selected.classList.contains("simple-print")
&& !selected.parentNode.classList.contains("simple-print")
&& !selected.classList.contains("simple-control")
&& !selected.classList.contains("simple-add-comment")
&& !selected.classList.contains("simple-comments")
&& !selected.classList.contains("simple-edit")
&& !selected.classList.contains("simple-edit-theme")
&& !selected.parentNode.classList.contains("simple-edit-theme")
&& !selected.classList.contains("simple-delete")
&& !selected.parentNode.classList.contains("simple-delete")
&& (selected.parentNode.classList && !selected.parentNode.classList.contains("simple-control"))
&& doc.body != selected
&& doc.documentElement != selected
&& selected.tagName !== "path"
Expand Down Expand Up @@ -915,35 +909,37 @@ function addGUI() {

// Add edit meta functionality
function editText(elem) {
// Hide the item
elem.style.display = "none";
if(!simpleArticleIframe.body.classList.contains("simple-deleting")) {
// Hide the item
elem.style.display = "none";

// Insert an input temporarily
var textInput = document.createElement("input");
textInput.type = "text";
textInput.value = elem.innerText;
// Insert an input temporarily
var textInput = document.createElement("input");
textInput.type = "text";
textInput.value = elem.innerText;

// Update the element on blur
textInput.onblur = function() {
// Change the value
elem.innerText = textInput.value;
// Update the element on blur
textInput.onblur = function() {
// Change the value
elem.innerText = textInput.value;

// Un-hide the elem
elem.style.display = "block";
// Un-hide the elem
elem.style.display = "block";

// Remove the input
textInput.parentNode.removeChild(textInput);
}
// Remove the input
textInput.parentNode.removeChild(textInput);
}

// Allow enter to be used to save the edit
textInput.onkeydown = function(e) {
if(e.keyCode === 13)
textInput.onblur();
}
// Allow enter to be used to save the edit
textInput.onkeydown = function(e) {
if(e.keyCode === 13)
textInput.onblur();
}

elem.parentNode.appendChild(textInput);
elem.parentNode.appendChild(textInput);

textInput.focus();
textInput.focus();
}
}


Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Just Read",
"version": "1.1.12",
"version": "1.1.13",
"manifest_version": 2,
"description": "A customizable reader extension.",
"homepage_url": "https://github.com/ZachSaucier/Just-Read",
Expand Down
3 changes: 2 additions & 1 deletion options.css
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ body, .editor-container {
width: calc(100% - 100px);
height: calc(100% - 120px);
padding: 8px;
overflow: auto;
}
.additional-options label {
display: block;
Expand All @@ -164,7 +165,7 @@ body, .editor-container {
cursor: pointer;
}

@media (max-width: 600px) {
@media (max-width: 740px) {
body {
padding-top: 0px;
}
Expand Down
6 changes: 4 additions & 2 deletions options.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<link href="options.css" rel="stylesheet"/>
</head>
<body>
<h1>Edit <i>Just Read</i> styles</h1>
<h1>Configure <i>Just Read</i></h1>

<div class="flex-container">
<div class="left-column">
Expand Down Expand Up @@ -47,11 +47,13 @@ <h3>Context menu entries</h3>
<label><input id="pageCM" type="checkbox" checked /> Enable page context menu entry.</label>
<label><input id="highlightCM" type="checkbox" checked /> Enable highlight context menu entry.</label>
<label><input id="linkCM" type="checkbox" checked /> Enable link context menu entry.</label>
<label><input id="autorunCM" type="checkbox" checked /> Enable auto-run context menu entry.</label>

<h3>Other options</h3>
<h3>Additional preferences</h3>
<label><input id="delModeBtn" type="checkbox"/> Show a button for deletion mode.</label>
<label><input id="leavePres" type="checkbox" /> Don't reformat pre tags.</label>
<label><input id="fullScrn" type="checkbox" /> Always auto-enable fullscreen when Just Read is started.</label>
<label><input id="alwaysAddAR" type="checkbox" /> Always add current site to Just Read's autorun list when Just Read is started on a page.</label>

<button id="closeAdditional">X</button>
</div>
Expand Down
15 changes: 15 additions & 0 deletions options.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,14 @@ function getStylesFromStorage(storage) {
highlightCM.checked = storage[key];
} else if(key === "enable-linkCM") {
linkCM.checked = storage[key];
} else if(key === "enable-autorunCM") {
autorunCM.checked = storage[key];
} else if(key === "show-del-btn") {
delMode.checked = storage[key];
} else if(key === "leave-pres") {
leavePres.checked = storage[key];
} else if(key === "alwaysAddAR") {
alwaysAddAR.checked = storage[key];
} else if(key === "fullscreen") {
fullScrn.checked = storage[key];
} else if(key.substring(0, 3) === "jr-") // Get the user's stylesheets
Expand Down Expand Up @@ -519,6 +523,8 @@ removeButton.onclick = function() {
var pageCM = document.getElementById("pageCM"),
highlightCM = document.getElementById("highlightCM"),
linkCM = document.getElementById("linkCM"),
autorunCM = document.getElementById("autorunCM"),
alwaysAddAR = document.getElementById("alwaysAddAR"),
fullScrn = document.getElementById("fullScrn");

pageCM.onchange = function() {
Expand All @@ -533,6 +539,15 @@ linkCM.onchange = function() {
chrome.storage.sync.set({"enable-linkCM": this.checked});
chrome.runtime.sendMessage({updateCMs: "true"});
}
autorunCM.onchange = function() {
console.log(this.checked);
chrome.storage.sync.set({"enable-autorunCM": this.checked});
chrome.runtime.sendMessage({updateCMs: "true"});
}

alwaysAddAR.onchange = function() {
chrome.storage.sync.set({"alwaysAddAR": this.checked});
}

fullScrn.onchange = function() {
chrome.storage.sync.set({"fullscreen": this.checked});
Expand Down

0 comments on commit 3908ad7

Please sign in to comment.