Inserisci qui il codice...
<?php
header('Content-type: text/html');
header('Access-Control-Allow-Origin: *');
$url = $_GET['url'];
$tags = get_meta_tags($url);
$description = $tags["description"];
echo '<br /> url: ';
print_r($url);
echo '<br /> description: ';
print_r($description);
?>
/*\
title: $:/paulin/demo/getdata.js
type: application/javascript
module-type: macro
<<getdata "url">>
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
/*
* Testing: get data with ajax
*/
exports.name = "getdata";
exports.params = [
{ name: "url" }
];
/*
* Run the macro
*/
exports.run = function(url) {
var url = url || "None";
function ajax(url) {
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.onload = function() {
resolve(this.responseText);
};
xhr.onerror = reject;
xhr.open('GET', url);
xhr.send();
});
}
var ret_val;
ajax('./test-2.php?url='+url).then(function(result) {
console.log('**** > '+result);
ret_val = result;
return result;
}).catch(function() {
console.log('Error');
ret_val = "An error occurred";
return "An error occurred";
});
console.log('----> '+ret_val);
return ret_val;
};
})();
In fact several plugins do this, filesystem plugin, tiddlyweb, NoteSelf, etc. In fact your code seems to be correct for the task. What are you missing?
url: http://www.nytimes.com/
description: The New York Times: Find breaking news, multimedia, reviews & opinion on Washington, business, sports, movies, travel, books, jobs, education, real estate, cars & more at nytimes.com
/*
* Run the macro
*/
exports.run = function(url, callback) {
var url = url || "None";
[ ........ I REMOVED AJAX CODE .........]
ajax('./test-2.php?url='+url).then(function(result) {
console.log('**** > '+result);
callback(null,result); // it is a convention tu return the error in first place, and then the results. We don't get any error, so put null in place
}).catch(function(error) {
console.log('Error');
callback(error)
});
};
//this function will handle the results
function my_ResultHandler(err,result){
if(err){
return; //finalize execution, we got an error. You are already handling this on the promise, so no need for an extra handling here
}
//doo whatever you want with your result here, maybe creating a new tiddler?
tw.addTiddler({title:'ajaxResult', text:result})
}
//here you call your macro, widget or whatever
url('my/url',my_ResultHandler);
//This time we don't need a callback because we are using promises
exports.run = function(url) {
var url = url || "None";
[.... I removed code here because it is the same ....]
return ajax('./test-2.php?url='+url).catch(
function(err) {
console.log('Error!', err);
});
// you can handle errors, but it is not mandatory, you can just return the promise, so code above can become
// return ajax('./test-2.php?url='+url)
}
function my_ResultHandler(result){....} // we don't need the error here, because that is handled inside the .catch() statement.
//then calling it
url('my/url').then(my_ResultHandler);
/*\
title: $:/paulin/demo/getDataFromUrl.js
type: application/javascript
module-type: macro
<<getDataFromUrl "tiddlername" "url">>
Example to get data from wikipedia: <<getDataFromUrl "Eureka" "https://en.wikipedia.org/wiki/Eureka_(word)">>
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
exports.name = "getDataFromUrl";
exports.params = [ {name: "tiddlername"},{name: "url"}];
/*
Run the macro
*/
exports.run = function(tiddlername,url) {
var url = url || "None";
var tiddlername = tiddlername || "None";
//console.log("**** url > "+url);
//console.log("**** tiddler > "+tiddlername);
//thanks to http://heckyesmarkdown.com/ for the api to get markdown
$tw.utils.httpRequest({url: "http://heckyesmarkdown.com/go/?u="+url, callback: function (error,data){
if (error){
console.log("ERROR:"+error);
}
$tw.wiki.addTiddler({title:tiddlername, text:data, type: "text/x-markdown"});
console.log(data);
}})
return "[["+tiddlername+"]]";
};
})();