Skip to content

Commit

Permalink
added clipper chrome extension
Browse files Browse the repository at this point in the history
this allows you to select a part of a page.
When you fill int the form a message is sent toe the server
and a snippet is created
  • Loading branch information
joearms committed Feb 19, 2011
1 parent 917ddf5 commit 72aac30
Show file tree
Hide file tree
Showing 10 changed files with 307 additions and 21 deletions.
20 changes: 20 additions & 0 deletions addbookmark.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-module(addbookmark).
-compile(export_all).

start(Str) ->
%% io:format("Str=~p~n",[Str]),
T = sebg:parse_uri_args(Str),
%% io:format("add bookmark:~p~n",[T]),
Time = proplists:get_value("time", T),
File = time_to_file(Time),
file:write_file(File, term_to_binary(T)).

time_to_file([$\s|T]) -> [$_|time_to_file(T)];
time_to_file([$(|T]) -> [$_|time_to_file(T)];
time_to_file([$)|T]) -> [$_|time_to_file(T)];
time_to_file([H|T]) -> [H|time_to_file(T)];
time_to_file([]) -> ".snip".




37 changes: 37 additions & 0 deletions clipper/Readme
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Clipper 1.2

This is a chrome unpacked extension

To install goto
Chrome -> Window -> Extensions

click on "Load unpacked extension"
and select the directory where this extension is contained

Help Needed
===========

This is just a beginning. Better analysis of the selection
is highly desirable

Credits
=======

The code in this extension is a modified version of
a chrome extension that I found on the net (and a blog)
article. The name of the extension was

"name": "Linx Bookmark 1.1",
"description": "Adds the current page to the Linx bookmarking system.",
"version": "1.1",

Unfortunately I cannot give a more precise reference, and
have not been able to find the original. Mail my if you stumble over
the origonal


Some the code in content_script.js
came form

http://dev.day.com/content/ddc/blog/2010/06.html

26 changes: 26 additions & 0 deletions clipper/background.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<script>
// Array to hold callback functions
var callbacks = [];

// This function is called onload in the popup code
function getPageInfo(callback)
{
// Add the callback to the queue
callbacks.push(callback);

// Inject the content script into the current page
// console.log('injecting');
chrome.tabs.executeScript(null, { file: "content_script.js" });
};

// Perform the callback when a request is received from the content script
chrome.extension.onRequest.addListener(function(request)
{
// Get the first callback in the callbacks array
// and remove it from the array
var callback = callbacks.shift();

// Call the callback function
callback(request);
});
</script>
50 changes: 50 additions & 0 deletions clipper/content_script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// code cut-and-paste from
// http://dev.day.com/content/ddc/blog/2010/06.html

// console.log('injecting code');

function getContent() {
// console.log('getting selection');
var selection = window.getSelection( );
var markup = serializeSelection( selection );
// var finalMarkup = formatPage( markup );
// return finalMarkup;
return markup;
}

function serializeSelection( selection ) {
var xmlFragment = "";
try {
var n = 0, ranges = selection.rangeCount;
// console.log('ranges='+ranges);
while ( n != ranges ) {
var range = selection.getRangeAt( n++ );
// console.log('here',range);
var content = range.cloneContents( );
var serializer = new XMLSerializer( );
xmlFragment += serializer.serializeToString( content );
}
}
catch( msg ) { }
return xmlFragment;
}

content = getContent();

// console.log('content',content);


var pageInfo = {
"title": document.title,
"url": window.location.href,
"text": window.getSelection().toString(),
"html": content
};

// console.log('sending page',pageInfo)
// Send the information back to the extension

chrome.extension.sendRequest(pageInfo);



Binary file added clipper/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions clipper/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "Clipper 1.2",
"description": "Clipper release 1 - first production version",
"version": "1.2",
"background_page": "background.html",
"permissions": [
"tabs",
"http://localhost:*//*/*",
"http://*/*",
"https://*/*"
],
"browser_action": {
"default_title": "Clipper",
"default_icon": "icon.png",
"popup": "popup.html"
}
}
85 changes: 85 additions & 0 deletions clipper/popup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<html>
<head>
<style>
body
{
min-width: 420px;
overflow-x: hidden;
font-family: Arial, sans-serif;
font-size: 12px;
}
input, textarea { width: 420px; }
input#save { font-weight: bold; width: auto; }
</style>
<script>

var text,html;

// This onPageInfo is called when the content script has been
// injected and returned its results
// it builds the form

function onPageInfo(o)
{
document.getElementById("title").value = o.title;
document.getElementById("url").value = o.url;
document.getElementById("notes").innerText = "";
text = o.text; // store in global
html = o.html; // store in global
}



// POST the data to the server using XMLHttpRequest
function addBookmark(f)
{
var req = new XMLHttpRequest();
req.open("POST", "http://localhost:1234/mod/addbookmark/", true);

var params =
"title=" + escape(document.getElementById("title").value) +
"&time=" + escape(new Date().toLocaleString()) +
"&text=" + escape(text) +
"&html=" + escape(html) +
"&url=" + escape(document.getElementById("url").value) +
"&notes=" + escape(document.getElementById("notes").value) +
"&tags=" + escape(document.getElementById("tags").value);
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
req.setRequestHeader("Content-length", params.length);
req.setRequestHeader("Connection", "close");

req.send(params);

req.onreadystatechange = function()
{
// If the request completed, close the extension popup
if (req.readyState == 4)
if (req.status == 200) window.close();
};

return false;
}

// Call the getPageInfo function in the background page, passing in
// our onPageInfo function as the callback
window.onload = function()
{
var bg = chrome.extension.getBackgroundPage();
bg.getPageInfo(onPageInfo);
}
</script>
</head>
<body>
<form id="addbookmark" onsubmit="addBookmark(this); return false;">
<p><label for="title">Title</label><br />
<input type="text" id="title" name="title" size="50" value="" /></p>
<p><label for="url">Url</label><br />
<input type="text" id="url" name="url" size="50" value="" /></p>
<p><label for="notes">Notes</label><br />
<textarea id="notes" name="notes" rows="6" cols="35"></textarea></p>
<p><label for="tags">Tags</label><br />
<input type="text" id="tags" name="tags" size="50" value="" /></p>
<p><input id="save" type="submit" value="Save Bookmark" /></p>
</form>
</body>
</html>
2 changes: 1 addition & 1 deletion generic.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var logdiv;

window.onload =
function() {
console.log('<p>started');
//console.log('<p>started');
}

function loadScript(File) {
Expand Down
8 changes: 7 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
</style>
<title>Simple Erlang Browser Graphics</title>

<h2>Lively</h2>

<a href="http://127.0.0.1:1234/ConnectLivelyToErlang.xhtml">Lively</a>

<h2>SEBG</h2>

<p>This code is changing rapidly. Always get the latest verions from
Expand Down Expand Up @@ -344,4 +348,6 @@ <h2>Exercises</h2>
analyse the data.

</ul>

<h2>References</h2>
http://www.switchonthecode.com/tutorials/javascript-draggable-elements
http://billmill.org/static/canvastutorial/bricks.html
Loading

0 comments on commit 72aac30

Please sign in to comment.