Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Options page #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 90 additions & 27 deletions background.html
Original file line number Diff line number Diff line change
@@ -1,37 +1,100 @@
<script>
chrome.contextMenus.create({
"title": "Open frame in this tab",
"contexts": ["frame"],
"onclick": function(clickData, tab) {
chrome.tabs.update(tab.id, {url: getURL(clickData)});
}
});

chrome.contextMenus.create({
"title": "Open frame in new tab",
"contexts": ["frame"],
"onclick": function(clickData) {
chrome.tabs.create({url: getURL(clickData)});
}
});
function setDefaults() {
//determine default values in a single place, called on start.
setBoolOption("checkthistab", getBoolOption("checkthistab", true));
setBoolOption("checknewtab", getBoolOption("checknewtab", true));
setBoolOption("checknewwindow", getBoolOption("checknewwindow", true));
setBoolOption("checkincognitowindow", getBoolOption("checkincognitowindow", true));
}

chrome.contextMenus.create({
"title": "Open frame in new window",
"contexts": ["frame"],
"onclick": function(clickData) {
chrome.windows.create({url: getURL(clickData)});
}
});
function createContextMenus() {

chrome.contextMenus.create({
"title": "Open frame in incognito window",
"contexts": ["frame"],
"onclick": function(clickData) {
chrome.windows.create({url: getURL(clickData), incognito: true});
if (getBoolOption("checkthistab")) {
chrome.contextMenus.create({
"title": "Open frame in this tab",
"contexts": ["frame"],
"onclick": function(clickData, tab) {
chrome.tabs.update(tab.id, {url: getURL(clickData)});
}
});
}
});

if (getBoolOption("checknewtab")) {
chrome.contextMenus.create({
"title": "Open frame in new tab",
"contexts": ["frame"],
"onclick": function(clickData) {
chrome.tabs.create({url: getURL(clickData)});
}
});
}

if (getBoolOption("checknewwindow")) {
chrome.contextMenus.create({
"title": "Open frame in new window",
"contexts": ["frame"],
"onclick": function(clickData) {
chrome.windows.create({url: getURL(clickData)});
}
});
}

if (getBoolOption("checkincognitowindow")) {
chrome.contextMenus.create({
"title": "Open frame in incognito window",
"contexts": ["frame"],
"onclick": function(clickData) {
chrome.windows.create({url: getURL(clickData), incognito: true});
}
});
}
}

function removeContextMenus() {
chrome.contextMenus.removeAll();
}

function getURL(clickData) {
return clickData.frameUrl || clickData.pageUrl
}

function getBoolOption(optionName, defaultValue) {
var tentativeValue = localStorage[optionName];
if (tentativeValue === 'true')
return true;
else if (tentativeValue == undefined)
return defaultValue;
else
return false;
}

function setBoolOption(optionName, newValue) {
localStorage[optionName] = newValue;
}

//actually start:
setDefaults();
createContextMenus();

//set up messaging for options page:
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
switch(request.action){
case "getbool":
sendResponse({optionValue: getBoolOption(request.optionName)});
break;
case "setbool":
setBoolOption(request.optionName, request.optionValue)
//also update current context menus
removeContextMenus();
createContextMenus();
sendResponse({}); //no real response
break;
default:
sendResponse({}); //ignore the request.
break;
}
});

</script>
3 changes: 2 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"description": "Adds context menu items to open a frame in the current tab, a new tab, new window, or incognito window.",
"version": "5",
"permissions": ["contextMenus", "tabs"],
"background_page": "background.html"
"background_page": "background.html",
"options_page": "options.html"
}
62 changes: 62 additions & 0 deletions options.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<html>
<head>
<title>Open Frame Options</title>
<script>

function OpenFrameLoadOptions () {
OpenFrameLoadCheckboxSetting("checkthistab");
OpenFrameLoadCheckboxSetting("checknewtab");
OpenFrameLoadCheckboxSetting("checknewwindow");
OpenFrameLoadCheckboxSetting("checkincognitowindow");
}

function OpenFrameLoadCheckboxSetting (optionName) {
chrome.extension.sendRequest({action: "getbool", optionName: optionName}, function(response) {
document.forms[0][optionName].checked = response.optionValue;
});
}

function OpenFrameSaveCheckboxSetting (optionName) {
chrome.extension.sendRequest({
action: "setbool",
optionName: optionName,
optionValue: document.forms[0][optionName].checked
});
}

</script>
</head>
<body
onload="OpenFrameLoadOptions();"
>
<form
onsubmit="OpenFrameSaveOptions();"
>
Show the following options:<br>
<input
type="checkbox"
name="checkthistab"
id="checkthistab"
onclick="OpenFrameSaveCheckboxSetting(this.name);"
> Open frame in this tab<br>
<input
type="checkbox"
name="checknewtab"
id="checknewtab"
onclick="OpenFrameSaveCheckboxSetting(this.name);"
> Open frame in new tab<br>
<input
type="checkbox"
name="checknewwindow"
id="checknewwindow"
onclick="OpenFrameSaveCheckboxSetting(this.name)";
> Open frame in new window<br>
<input
type="checkbox"
name="checkincognitowindow"
id="checkincognitowindow"
onclick="OpenFrameSaveCheckboxSetting(this.name);"
> Open frame in incognito window<br>
</form>
</body>
</html>