StackExchange chat Onebox plugin module

JSON manifest of XPATH data
- Provide a URL
- Provide a token ID/XPATH pair
- Provide HTML template
Example: Setting up the plugin
Note:
URL fragment "Section_5.1.3" implies XPATH selections starts
at that ID (this could resolve the section-specific linking
such as seen with WikiPedia links): /id("Section_5.1.4")/
The objective is to classify a URL into grouping based on URL components
which we could in theory run an XOR or some other inclusive matching over
(no server-side regex, only string matching).
The source-code for the plugin (ie, user submitted code) would look like this:
{
"URL" : {
"scheme" : ["http", "https"],
"authority" : ["ecma262-5.com", "www.ecma262-5.com"],
"path" : [], //No special matching
"query" : [],
"fragment" : []
}
"Tokens:"[
{
"id" : "{Title}",
"" //implied ID matches element
},
{
"id" : "{Text}",
"/following-sibling::text()" //Grab the adjacent text node
},
],
"Template" : "<h1>{Title}</h1> <br/><p>{Text}</p>"
}
Examine URLs for matching plugins based on string matches of URL components
Example: Onebox request client-side:
function URLToComponents(url){
//RFC 2396 appendix B-- URL regex
var urlRegex=new RegExp("^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?");
var data = url.split(urlRegex);
return {
"scheme" : data[2],
"authority" : data[4],
"path" : data[5],
"query" : data[7],
"fragment" : data[9]
};
}
var components = URLToComponents("http://ecma262-5.com/ELS5_Section_5.htm#Section_5.1.3");
We then submit the onebox reguest with the provided URL and the template JSON attached to the room.
Matching calls plugin
The provided URL "http://ecma262-5.com/ELS5_Section_5.htm#Section_5.1.3" matches the URL components
as described in the plugin source, thus we request the page and assign token matches from XPath of the DOM Document.
Request then XPath
- Send request to the URL, get the HTML.
- Scan HTML with XPath to find the corresponding DOM nodes
- Return content truncated at 512 chars, or empty string (if missing).
- Convert token data to sanitized inputs (ie, no alert("xss")), flat text elements should suffice.
Build the template
We don't really need massive customization here, simply a few basic CSS classes to keep things
looking clean on the SE side. Just provide the same classes, and let semantic HTML take care of the rest.
Insert the text into the tokens and drop in the HTML content.
Fin.