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 update #29

Merged
merged 3 commits into from
Aug 17, 2017
Merged
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
54 changes: 33 additions & 21 deletions capture.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
var capture = function(){
var replace_all = function(str, find, replace) { return str.replace(new RegExp(find, 'g'), replace); };
var esc = function (text) { return replace_all(replace_all(replace_all(encodeURIComponent(text),"[(]",escape("(")),"[)]",escape(")")),"[']",escape("'")); };
var selection = window.getSelection().toString();

var uri = 'org-protocol://';
if (selection != "")
{
uri += 'capture:/p/';
}
else
{
uri += 'capture:/L/'
};
function replace_all(str, find, replace) {
return str.replace(new RegExp(find, 'g'), replace);
}

uri += encodeURIComponent(location.href) + '/' +
esc(document.title) ;
function escapeIt(text) {
return replace_all(replace_all(replace_all(encodeURIComponent(text), "[(]", escape("(")),
"[)]", escape(")")),
"[']" ,escape("'"));
}

if (selection != "") { uri += '/' + esc(selection); };
function createCaptureURL(template, title, url, selection, oldStyle) {
if (oldStyle == true)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if oldStyle is a boolean, there is no need for == true

return "org-protocol://capture:/"+template+'/'+url+'/'+title + ((selection === '') ? '' : ('/' + selection));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use consistent equality operator (see below)

else
return "org-protocol://capture?template="+template+'&url='+url+'&title='+title+((selection === '') ? '' : ('&body=' + selection));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you are using a regular URL syntax here, the selection should be escaped using encodeURIComponent as well.

}

console.log("Capturing the following URI with org-protocol:", uri);
function captureIt() {
var selection = window.getSelection().toString();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please escape the selection as well, not just the title.

var url = encodeURIComponent(location.href);
var title = escapeIt(document.title);

location.href=uri;
};
chrome.storage.sync.get({
selectedTemplate: 'p',
unselectedTemplate: 'L',
useOldStyleLinks: false
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make the default to old style links, for the benefit of those who already have the extension installed.

}, function(items) {
var uri = '';
if (selection != '')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use consistent equality operator (see above)

uri = createCaptureURL(items.selectedTemplate, title, url, selection, items.useOldStyleLinks);
else
uri = createCaptureURL(items.unselectedTemplate, title, url, selection, items.useOldStyleLinks);
alert(uri);
location.href = uri;
});
}

capture();
captureIt();
6 changes: 5 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
"scripts": ["background.js"]
},

"permissions": ["activeTab"],
"permissions": ["activeTab", "storage"],

"options_ui": {
"page": "options.html"
},

"browser_action": {
"default_icon": "org-mode-unicorn.png"
Expand Down
43 changes: 43 additions & 0 deletions options.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html>
<head>
<title>Org Capture Settings</title>
<style>
body: { padding: 10px; }
</style>
</head>

<body>

<table>
<tr>
<td>
Selected Template
</td>
<td>
<input id="selTemplate" />
</td>
</tr>
<tr>
<td>
Unselected Template
</td>
<td>
<input id="unselTemplate">
</td>
</tr>
<tr>
<td>
Use Old-Style Links?
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add write that new-style is the recommended option for supported org-mode versions above 9.0

</td>
<td>
<input id="useOldstyle" type="checkbox" />
</td>
</table>

<div id="status"></div>
<button id="save">Save</button>

<script src="options.js"></script>
</body>
</html>
36 changes: 36 additions & 0 deletions options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Saves options to chrome.storage.sync.
function save_options() {
var selTemp = document.getElementById('selTemplate').value;
var unselTemp = document.getElementById('unselTemplate').value;
var oldStyleP = document.getElementById('useOldstyle').checked;
chrome.storage.sync.set({
selectedTemplate: selTemp,
unselectedTemplate: unselTemp,
useOldStyleLinks: oldStyleP
}, function() {
// Update status to let user know options were saved.
var status = document.getElementById('status');
status.textContent = 'Options saved.';
setTimeout(function() {
status.textContent = '';
}, 750);
});
}

// Restores select box and checkbox state using the preferences
// stored in chrome.storage.
function restore_options() {
// Use default value color = 'red' and likesColor = true.
chrome.storage.sync.get({
selectedTemplate: 'L',
unselectedTemplate: 'p',
useOldStyleLinks: false
}, function(items) {
document.getElementById('unselTemplate').value = items.unselectedTemplate;
document.getElementById('selTemplate').value = items.selectedTemplate;
document.getElementById('useOldstyle').checked = items.useOldStyleLinks;
});
}
document.addEventListener('DOMContentLoaded', restore_options);
document.getElementById('save').addEventListener('click',
save_options);