diff --git a/core/modules/filters/url-ops.js b/core/modules/filters/url-ops.js new file mode 100644 index 000000000..392e510be --- /dev/null +++ b/core/modules/filters/url-ops.js @@ -0,0 +1,39 @@ +/*\ +title: $:/core/modules/filters/url-ops.js +type: application/javascript +module-type: filteroperator + +Filter operators for URL operations + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +exports["setquerystring"] = function(source,operator,options) { + var URL = $tw.browser ? window.URL : require("url").URL, + URLSearchParams = $tw.browser ? window.URLSearchParams : require("url").URLSearchParams, + name = operator.operands.length >= 1 ? operator.operands[0] : null, + value = operator.operands.length >= 2 ? operator.operands[1] : "", + results = []; + source(function(tiddler,title) { + var url; + try { + url = new URL(title); + } catch(e) { + } + if(url) { + var params = new URLSearchParams(url.search); + if(name) { + params.set(name,value); + } + url.search = params.toString(); + results.push(url.toString()); + } + }); + return results; +}; + +})(); diff --git a/core/modules/utils/dom/http.js b/core/modules/utils/dom/http.js index 3eca4985b..6b9ef4bb2 100644 --- a/core/modules/utils/dom/http.js +++ b/core/modules/utils/dom/http.js @@ -66,9 +66,9 @@ HttpClient.prototype.handleHttpRequest = function(event) { callback: function(err,data,xhr) { var headers = {}; $tw.utils.each(xhr.getAllResponseHeaders().split("\r\n"),function(line) { - var parts = line.split(":"); - if(parts.length === 2) { - headers[parts[0].toLowerCase()] = parts[1].trim(); + var pos = line.indexOf(":"); + if(pos !== -1) { + headers[line.substr(0,pos)] = line.substr(pos + 1).trim(); } }); setBinding(bindStatus,xhr.status === 200 ? "complete" : "error"); diff --git a/editions/test/tiddlers/tests/data/filters/querystrings.tid b/editions/test/tiddlers/tests/data/filters/querystrings.tid new file mode 100644 index 000000000..fb3dd7687 --- /dev/null +++ b/editions/test/tiddlers/tests/data/filters/querystrings.tid @@ -0,0 +1,27 @@ +title: Filters/QueryStrings +description: Query string filter operators +type: text/vnd.tiddlywiki-multiple +tags: [[$:/tags/wiki-test-spec]] + +title: Output + +\define url() https://tiddlywiki.com/ + +<$let lf={{{ [charcode[10],[13]]}}}> + +<$text text={{{ + [setquerystring[]] + [setquerystring[animal],[elephant]] + [setquerystring[animal],[elephant]setquerystring[animal],[ostrich]] + [setquerystring[animal],[elephant]setquerystring[animal],[ostrich]setquerystring[animal]] + +[join] +}}}/> + + ++ +title: ExpectedResult + +https://tiddlywiki.com/ +https://tiddlywiki.com/?animal=elephant +https://tiddlywiki.com/?animal=ostrich +https://tiddlywiki.com/?animal= \ No newline at end of file diff --git a/editions/tw5.com/tiddlers/messages/WidgetMessage_ tm-http-request Example Zotero.tid b/editions/tw5.com/tiddlers/messages/WidgetMessage_ tm-http-request Example Zotero.tid new file mode 100644 index 000000000..0a05ca6e1 --- /dev/null +++ b/editions/tw5.com/tiddlers/messages/WidgetMessage_ tm-http-request Example Zotero.tid @@ -0,0 +1,78 @@ +title: WidgetMessage: tm-http-request Example - Zotero +tags: $:/tags/Macro + +\define zotero-save-item(item) +<$action-createtiddler + $basetitle={{{ =[[_zotero_import ]] =[jsonget[key]] =[[ ]] =[jsonget[title]] +[join[]] }}} + text={{{ [jsonget[title]] }}} + tags="$:/tags/ZoteroImport" +> + <$action-setmultiplefields $tiddler=<> $fields="[jsonindexes[]addprefix[zotero-]]" $values="[jsonindexes[]] :map[jsongetelse[.XXXXX.]]"/> + +\end zotero-save-item + +\define zotero-save-items(data) +<$list filter="[jsonindexes[]] :map[jsonextract,[data]]" variable="item"> + <$macrocall $name="zotero-save-item" item=<>/> + +\end zotero-save-items + +\define zotero-get-items(start:"0",limit:"25") + +\define completion() +\import [[$:/core/ui/PageMacros]] [all[shadows+tiddlers]tag[$:/tags/Macro]!has[draft.of]] + <$action-log msg="In completion"/> + <$action-log/> + + <$list filter="[compare:number:gteq[200]compare:number:lteq[299]]" variable="ignore"> + + <$macrocall $name="zotero-save-items" data=<>/> + + <$list filter="[jsonget[total-results]subtractsubtractcompare:number:gt[0]]" variable="ignore"> + <$macrocall $name="zotero-get-items" start={{{ [add] }}} limit=<>/> + + +\end completion + +\define progress() +\import [[$:/core/ui/PageMacros]] [all[shadows+tiddlers]tag[$:/tags/Macro]!has[draft.of]] + <$action-log message="In progress-actions"/> +\end progress + +\define request-url() +https://api.zotero.org/groups/27508/items/?format=json&sort=title +\end request-url + +<$action-sendmessage + $message="tm-http-request" + url={{{ [setquerystring[start],<__start__>setquerystring[limit],<__limit__>] }}} + method="GET" + header-accept="application/json" + bind-status="$:/temp/zotero/status" + bind-progress="$:/temp/zotero/progress" + oncompletion=<> + onprogress=<> + var-start=<<__start__>> + var-limit=<<__limit__>> +/> +\end + +\define zotero-actions() +<$macrocall $name="zotero-get-items" start="0" limit="5"/> +\end + +<$button actions=<>> +Call Zotero + + +Tiddlers: + +
    +<$list filter="[tag[$:/tags/ZoteroImport]]"> +
  1. +<$link> +<$view field="title"/> + +
  2. + +
diff --git a/editions/tw5.com/tiddlers/messages/WidgetMessage_ tm-http-request.tid b/editions/tw5.com/tiddlers/messages/WidgetMessage_ tm-http-request.tid index 5befb6f1b..826ca3032 100644 --- a/editions/tw5.com/tiddlers/messages/WidgetMessage_ tm-http-request.tid +++ b/editions/tw5.com/tiddlers/messages/WidgetMessage_ tm-http-request.tid @@ -41,3 +41,7 @@ The following variables are passed to the progress handler: |lengthComputable |Whether the progress loaded and total figures are valid - "yes" or "no" | |loaded |Number of bytes loaded so far | |total |Total number bytes to be loaded | + +!! Examples + +* [[Zotero's|https://www.zotero.org/]] API for retrieving reference items: [[WidgetMessage: tm-http-request Example - Zotero]]