Permalink
Browse files

removed references to old name. Added a bookmarklet wrapper. converte…

…d to POST to avoid ridiculously long GET URLs
  • Loading branch information...
1 parent 83455fe commit 4b021467afedc9f180bdfeb3370afa2c01460d27 @slifty committed Jan 31, 2012
View
10 api.php
@@ -1,7 +1,12 @@
<?PHP
set_include_path($_SERVER['DOCUMENT_ROOT']);
+
require_once("conf.php");
header('Content-type: text/javascript');
+ header('Access-Control-Allow-Origin: *');
+ header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
+ header('Access-Control-Max-Age: 1000');
+ header('Access-Control-Allow-Headers: Content-Type');
$params = explode('/',(isset($_GET['q'])?$_GET['q']:""));
$resourceType = preg_replace('[^A-Za-z0-9_]',"",isset($params[0])?$params[0]:"");
@@ -10,10 +15,11 @@
$resourceIdentifier = isset($params[1])?$params[1]:"";
$jsonp = isset($_GET['jsonp'])?true:false;
- $callback = isset($_GET['callback'])?$_GET['callback']:"goggles_".$requestMethod."_".$resourceType."_callback";
- $resourceIdentifier = isset($_GET['r'])?$_GET['r']:null|$resourceIdentifier|null;
+ $callback = isset($_GET['callback'])?$_GET['callback']:"goggles_".$requestMethod."_".$resourceType."_callback";
+ $resourceIdentifier = isset($_GET['r'])?$_GET['r']:null|$resourceIdentifier|null;
$path = "api/".$resourceType."/".$requestMethod."_".$resourceType.".php";
+
if(file_exists($path))
include_once($path);
else {
View
@@ -1,38 +0,0 @@
-<?PHP
- set_include_path($_SERVER['DOCUMENT_ROOT']);
- require_once("conf.php");
- include_once("models/Claim.php");
-
- $collectionJSON = "";
- if($resourceIdentifier) {
- // Return a single object
- $object = Claim::getObject((int)$resourceIdentifier);
- $collectionJSON = '{"claims": ['.$object->toJSON()."]}";
- } else {
- // Return a list of objects
- if (isset($_GET['context'])) {
- // Get a list of related claims from the API
- $ch = curl_init($API_RELATED_CLAIMS.'?c='.urlencode($_GET['context']));
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
-
- $collectionJSON = curl_exec($ch);
- curl_close($ch);
-
- } else {
- $claims = Claim::getObjects('select claims.id from claims');
-
- // Run through the claims and generate JS Objects
- $objectJSON = array();
- foreach($claims as $claim)
- $objectJSON[] = $claim->toJSON();
-
- $collectionJSON = '{"claims": ['.implode(",", $objectJSON).']}';
- }
- }
-
- if(isset($jsonp) && $jsonp)
- echo($callback.'('.$collectionJSON.');');
- else
- echo($collectionJSON);
-?>
View
@@ -0,0 +1,38 @@
+<?PHP
+ set_include_path($_SERVER['DOCUMENT_ROOT']);
+ require_once("conf.php");
+ include_once("models/Claim.php");
+
+ $collectionJSON = "";
+ if($resourceIdentifier) {
+ // Return a single object
+ $object = Claim::getObject((int)$resourceIdentifier);
+ $collectionJSON = '{"claims": ['.$object->toJSON()."]}";
+ } else {
+ // Return a list of objects
+ if (isset($_POST['context'])) {
+ // Get a list of related claims from the API
+ $ch = curl_init($API_RELATED_CLAIMS.'?c='.urlencode($_POST['context']));
+ curl_setopt($ch, CURLOPT_HEADER, 0);
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
+
+ $collectionJSON = curl_exec($ch);
+ curl_close($ch);
+
+ } else {
+ $claims = Claim::getObjects('select claims.id from claims');
+
+ // Run through the claims and generate JS Objects
+ $objectJSON = array();
+ foreach($claims as $claim)
+ $objectJSON[] = $claim->toJSON();
+
+ $collectionJSON = '{"claims": ['.implode(",", $objectJSON).']}';
+ }
+ }
+
+ if(isset($jsonp) && $jsonp)
+ echo($callback.'('.$collectionJSON.');');
+ else
+ echo($collectionJSON);
+?>
@@ -1,83 +0,0 @@
-<?PHP
- set_include_path($_SERVER['DOCUMENT_ROOT']);
- require_once("conf.php");
- include_once("models/Claim.php");
- include_once("models/Snippet.php");
-
- $collectionJSON = "";
- if($resourceIdentifier) {
- // Return a single object
- $object = Snippet::getObject((int)$resourceIdentifier);
- $collectionJSON = '{"snippets": ['.$object->toJSON().']}';
- } else {
- // Return a list of objects
- if(isset($_GET['url'])) {
- // Curl the URL and pull any snippets that are contained
- } elseif (isset($_GET['context'])) {
- // Look at each piece of context and pull any snippets that are contained
- $context = utf8_decode($_GET['context']);
- $oid = isset($_GET['oid'])?$_GET['oid']:0;
-
- // Scan the snippet DB for known matches
- $snippets = Snippet::getSnippetsByContext($context);
-
- // Run through the snippets and generate JS Objects
- $snippetJSON = array();
- foreach($snippets as $snippet) {
- $contentRegExp = '/'.preg_replace('/\s+/','\\s+',$snippet->getContent()).'/';
- $contentSplit = preg_split($contentRegExp, $context, null, PREG_SPLIT_OFFSET_CAPTURE);
- $contentStart = strlen($contentSplit[0][0]);
-
- for($x = 1; $x < sizeof($contentSplit) ; $x += 1 ) {
- $contentLength = $contentSplit[$x][1] - $contentStart;
- $snippetJSON[] = $snippet->toJSON($contentStart, $contentLength);
-
- // Update the content start for the next instance of the snippet (if it exists)
- $contentStart = $contentSplit[$x][1] + strlen( $contentSplit[$x][0]);
- }
- }
-
- // Run through the related claims and generate JS Objects
- $relatedClaimsJS = array();
-
- // Create the collection
- $collectionJSON = '{
- "oid": '.DBConn::clean($oid).',
- "snippets": ['.implode(",", $snippetJSON).']
- }';
-
- } elseif (isset($_GET['claim_id'])) {
- // Load a claim and return it's snippets
- $claimID = $_GET['claim_id'];
- $claim = Claim::getObject($claimID);
- $snippets = $claim->getSnippets();
-
- // Run through the snippets and generate JS Objects
- $snippetJSON = array();
- foreach($snippets as $snippet) {
- $snippetJSON[] = $snippet->toJSON();
- }
-
- $collectionJSON = '{
- "snippets": ['.implode(',', $snippetJSON).']
- }';
- } else {
- $snippets = Snippet::getObjects('select snippets.id from snippets');
-
- // Run through the snippets and generate JS Objects
- $snippetJSON = array();
- foreach($snippets as $snippet) {
- $snippetJSON[] = $snippet->toJSON();
- }
-
- $collectionJSON = '{
- "snippets": ['.implode(',', $snippetJSON).']
- }';
- }
- }
-
- if(isset($jsonp) && $jsonp)
- echo($callback.'('.$collectionJSON.');');
- else
- echo($collectionJSON);
-?>
@@ -0,0 +1,83 @@
+<?PHP
+ set_include_path($_SERVER['DOCUMENT_ROOT']);
+ require_once("conf.php");
+ include_once("models/Claim.php");
+ include_once("models/Snippet.php");
+
+ $collectionJSON = "";
+ if($resourceIdentifier) {
+ // Return a single object
+ $object = Snippet::getObject((int)$resourceIdentifier);
+ $collectionJSON = '{"snippets": ['.$object->toJSON().']}';
+ } else {
+ // Return a list of objects
+ if(isset($_POST['url'])) {
+ // Curl the URL and pull any snippets that are contained
+ } elseif (isset($_POST['context'])) {
+ // Look at each piece of context and pull any snippets that are contained
+ $context = utf8_decode($_POST['context']);
+ $oid = isset($_POST['oid'])?$_POST['oid']:0;
+
+ // Scan the snippet DB for known matches
+ $snippets = Snippet::getSnippetsByContext($context);
+
+ // Run through the snippets and generate JS Objects
+ $snippetJSON = array();
+ foreach($snippets as $snippet) {
+ $contentRegExp = '/'.preg_replace('/\s+/','\\s+',$snippet->getContent()).'/';
+ $contentSplit = preg_split($contentRegExp, $context, null, PREG_SPLIT_OFFSET_CAPTURE);
+ $contentStart = strlen($contentSplit[0][0]);
+
+ for($x = 1; $x < sizeof($contentSplit) ; $x += 1 ) {
+ $contentLength = $contentSplit[$x][1] - $contentStart;
+ $snippetJSON[] = $snippet->toJSON($contentStart, $contentLength);
+
+ // Update the content start for the next instance of the snippet (if it exists)
+ $contentStart = $contentSplit[$x][1] + strlen( $contentSplit[$x][0]);
+ }
+ }
+
+ // Run through the related claims and generate JS Objects
+ $relatedClaimsJS = array();
+
+ // Create the collection
+ $collectionJSON = '{
+ "oid": '.DBConn::clean($oid).',
+ "snippets": ['.implode(",", $snippetJSON).']
+ }';
+
+ } elseif (isset($_POST['claim_id'])) {
+ // Load a claim and return it's snippets
+ $claimID = $_POST['claim_id'];
+ $claim = Claim::getObject($claimID);
+ $snippets = $claim->getSnippets();
+
+ // Run through the snippets and generate JS Objects
+ $snippetJSON = array();
+ foreach($snippets as $snippet) {
+ $snippetJSON[] = $snippet->toJSON();
+ }
+
+ $collectionJSON = '{
+ "snippets": ['.implode(',', $snippetJSON).']
+ }';
+ } else {
+ $snippets = Snippet::getObjects('select snippets.id from snippets');
+
+ // Run through the snippets and generate JS Objects
+ $snippetJSON = array();
+ foreach($snippets as $snippet) {
+ $snippetJSON[] = $snippet->toJSON();
+ }
+
+ $collectionJSON = '{
+ "snippets": ['.implode(',', $snippetJSON).']
+ }';
+ }
+ }
+
+ if(isset($jsonp) && $jsonp)
+ echo($callback.'('.$collectionJSON.');');
+ else
+ echo($collectionJSON);
+?>
@@ -1,14 +1,4 @@
-(function() {
-
- var CSS_URL = criticalDomain + "/styles/critical.min.css";
-
- // Step 0: Import the css
- s = document.createElement('link');
- s.rel = 'stylesheet';
- s.type = 'text/css';
- s.href = CSS_URL;
- s.media = 'all';
- document.getElementsByTagName("head")[0].appendChild(s);
+activate_truth_goggles = function($) {
// Step 1: Collect the text
var paragraphs = document.getElementsByTagName("body");
@@ -17,15 +7,31 @@
var claims_script = document.createElement('script');
var text = paragraphs[x].textContent?paragraphs[x].textContent:paragraphs[x].innerText;
- snippets_script.src = criticalDomain + "/api/snippets/" + '?jsonp=1&oid=' + x + '&context=' + encodeURIComponent(text);
+ $.ajax({
+ type: 'POST',
+ url: goggles_domain + '/api/snippets/',
+ crossDomain: true,
+ data: {
+ oid:x,
+ context: encodeURIComponent(text)
+ },
+ dataType: 'json',
+ success: function(responseData, textStatus, jqXHR) {
+ console.log(responseData);
+ },
+ error: function (responseData, textStatus, errorThrown) {
+ alert('POST failed.');
+ }
+ });
+
+/* snippets_script.src = goggles_domain + "/api/snippets/" + '?jsonp=1&oid=' + x + '&context=' + encodeURIComponent(text);
document.getElementsByTagName('head')[0].appendChild(snippets_script);
- claims_script.src = criticalDomain + "/api/claims/" + '?jsonp=1&oid=' + x + '&context=' + encodeURIComponent(text);
- document.getElementsByTagName('head')[0].appendChild(claims_script);
+ claims_script.src = goggles_domain + "/api/claims/" + '?jsonp=1&oid=' + x + '&context=' + encodeURIComponent(text);
+ document.getElementsByTagName('head')[0].appendChild(claims_script);*/
}
window.goggles_get_claims_callback = function(data) {
- console.log(data);
}
window.goggles_get_snippets_callback = function(data) {
@@ -150,4 +156,4 @@
// }
}
}
-})();
+};
View
@@ -0,0 +1,15 @@
+/*
+ * Code is adapted from the script found here: http://latentmotion.com/how-to-create-a-jquery-bookmarklet/
+ */
+
+window.bookmarklet = function(opts){fullFunc(opts)};
+ window.bookmarklet({
+ css: [goggles_domain + '/styles/goggles.min.css'],
+ js: [goggles_domain + '/goggles.min.js'],
+ //jqpath: 'myCustomjQueryPath.js', <-- option to include your own path to jquery
+ ready: function() {
+ activate_truth_goggles($);
+ }
+})
+
+function fullFunc(a){function d(b){if(b.length===0){a.ready();return false}$.getScript(b[0],function(){d(b.slice(1))})}function e(b){$.each(b,function(c,f){$("<link>").attr({href:f,rel:"stylesheet"}).appendTo("head")})}a.jqpath=a.jqpath||"http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js";(function(b){var c=document.createElement("script");c.type="text/javascript";c.src=b;c.onload=function(){e(a.css);d(a.js)};document.body.appendChild(c)})(a.jqpath)};
View
@@ -19,7 +19,7 @@
<p>As with most tools, the largest challenges are not technical but are social. With that in mind, what you see on this page is just a tech demo for an idea that will have a full demo in the coming month. The goggles work in many places, but they aren't that smart yet. Facts are not scraped, and associations with content aren't being systematically generated just yet.</p><br />
<p>Follow me at <a href="http://www.twitter.com/slifty">@slifty</a> to get updates about the project.</p>
<h2>The Demo</h2>
- <p><a href="javascript:var%20criticalDomain='http://<?PHP echo($SITE_DOMAIN.$SITE_ROOT); ?>';var%20s=document.createElement('script');s.type='text/javascript';document.body.appendChild(s);s.src=criticalDomain+'/critical.min.js';void(0);">Apply Truth Goggles</a> <-- Bookmark this link! (Drag it to your bookmarks)</p>
+ <p><a href="javascript:var%20goggles_domain='http://<?PHP echo($SITE_DOMAIN.$SITE_ROOT); ?>';var%20s=document.createElement('script');s.type='text/javascript';document.body.appendChild(s);s.src=goggles_domain+'/goggles_bookmarklet.min.js';void(0);">Apply Truth Goggles</a> <-- Bookmark this link! (Drag it to your bookmarks)</p>
<br />
<p><b>While</b> advising his Fox News viewers to talk about inflation at their Thanksgiving dinners, Glenn Beck falsely claimed that the government removed food and energy prices from <b>its measure <i>of</i> inflation to hide rising prices, that a survey showed economists are “worried”</b> about inflation, and that Social Security recipients are not receiving a cost-of-living adjustment because the government “changed the calculation.”</p>
<br />
File renamed without changes.

0 comments on commit 4b02146

Please sign in to comment.