Skip to content

Instantly share code, notes, and snippets.

@gdakram
Created January 20, 2011 23:29
Show Gist options
  • Save gdakram/788932 to your computer and use it in GitHub Desktop.
Save gdakram/788932 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>3rd party website using the bookmarking widget</title>
</head>
<body>
....content....
<!-- Embed the script tag to make the widget show here -->
<script src="http://bookmarking-site.com/javascripts/widget.js"></script>
....content....
</body>
</html>
class BookmarksController < ApplicationController
# The rails route file declaration to for save.json
# action to repond to GET request.
#
# resources :bookmarks do
# collection do
# get :save
# end
# end
#
def save
# if there's a user found
if current_user
Bookmark.create(:url => params[:url], :user => current_user)
response = "window.Academia.bookmark_callback(true);"
else
response = "window.Academia.bookmark_callback(false);"
end
respond_to do |wants|
wants.json { render :text => response }
end
end
private
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
end
// write the widget into the page
document.write('<a href="#" class="save_to_academia_bookmarks">Bookmark This Page</a>');
window.Academia = window.Academia || {};
(function(a){
// This is the callback function the json response from the server
a.bookmark_callback = function(saved){
if (!saved) {
console.log("page was not saved, respond appropriately... open up authentication window etc");
}
else {
console.log("page was saved, respond appropriately... indicate to user it was saved");
}
}
// the click event handler for bookmarking widget link
a.a_links = document.getElementsByClassName("save_to_academia_bookmarks");
for (i = 0; i < a.a_links.length; i++) {
a.a_links[i].onclick = function(e){
var script = document.createElement('script');
script.src = "http://bookmarking-site.com/bookmarks/save.json?url=" + window.location.href;
document.body.appendChild(script);
return false;
}
}
})(Academia);
@gdakram
Copy link
Author

gdakram commented Jan 20, 2011

Cross-domain Javascript using JSONP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment