PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/README.md

https://github.com/mikeedwards83/zeroclickinfo-spice
Markdown | 133 lines | 77 code | 56 blank | 0 comment | 0 complexity | 776d6d1119c703e0f334eaa0463fc4cc MD5 | raw file
  1. Spice Zero-click Info Plugins
  2. =================================
  3. See [the contribution wiki](https://github.com/duckduckgo/duckduckgo/wiki) for a general overview on contributing to DuckDuckGo.
  4. This repository is for contributing JavaScript-based Zero-click Info plugins. Each spice plugin will generally involve at least one HTTP(S) request to a third-party API, though in some cases could be completely stand-alone.
  5. We maintain a list of requested spice plugins, which are colored purple on [the Trello board](https://trello.com/board/duckduckgo-open-source-plugins/4f08e96d947729b526070890), but whatever you want to attempt is welcome!
  6. Contributing
  7. ------------
  8. First off, thank you!
  9. ### Process
  10. 1) Develop your plugin using the Structure below in either a fork or a branch (if a collaborator).
  11. 2) Test your plugin via the Testing procedure below.
  12. 3) Submit a pull request.
  13. Feel free to [ask questions](http://webchat.freenode.net/?channels=duckduckgo)!
  14. ### Structure
  15. Each spice plugin has its own directory. Some of the directories are in use on the live system, and some are still in development.
  16. Each directory has a structure like this:
  17. ```txt
  18. # Main file, which gets called by the client at the appropriate time.
  19. # To understand the flow, look at example/spice.js
  20. plugin/spice.js
  21. # Calls the js file and is used for testing.
  22. # Look at example/example.html for extensive workflow comments.
  23. plugin/spice.html
  24. # The js functions and files get segemented by a short namespace prefix.
  25. # This is usually two or three letters, e.g. xk for xkcd.
  26. # Just make something up you think makes sense.
  27. plugin/spice.namespace
  28. # Nginx conf to call the relevant external API.
  29. # To prevent search leakage (and for caching), we run
  30. # all calls through nginx.
  31. # Start with the xkcd conf and try modifying it appropriately.
  32. plugin/spice.conf
  33. # Perl block to determine when to call the spice plugin.
  34. # See xkcd plugin for a good example to start with.
  35. plugin/spice.pl
  36. # Example JSON object (returned from third-party API).
  37. plugin/spice.json
  38. ```
  39. ### Testing
  40. You should be able to test your spice plugin via the spice.html file in your plugin directory. That is, it should be able to run in your Web browser and display the information you want it to display in a stand-alone fashion.
  41. In the same token, you should be able to run the example.html to test that you have the repository set up right. If it works, you should get a line about weather at the top.
  42. You do not need a Web server to test, though it is of course fine if you do. That is, you should just be able to open the files locally, i.e. drag or open the appropriate HTML in your browser.
  43. ### spice.js flow
  44. The overall flow is as follows:
  45. 1) An external API is called with a callback function.
  46. 2) That callback function is defined by you, and takes the JSON object from the external API and parses out the information needed for display.
  47. 3) Your callback function calls the nra function with the appropriate variables. That is the internal function we use to display the Zero-click Info box for spice plugins. You pass nra a object that takes the following parameters.
  48. ```js
  49. // Requried snippet (abstract). It can be pure HTML in which case it is set via innerHTML, but better is it is an object, in which case onclick and other event handlers won't be destroyed.
  50. items[0]['a'] = snippet;
  51. // Optional header. If there is a relevant (and relatively short) title, then set it here.
  52. items[0]['h'] = title;
  53. // Required source name and url.
  54. // These are used to make the More at X link in all Zero-click Info boxes.
  55. // 's' should be the main name.
  56. items[0]['s'] = 'XKCD';
  57. items[0]['u'] = url
  58. // Optional force of a bigger box. Usually the box is auto-resized smaller with an expansion UI if needed. Generally you shouldn't force it to be bigger, but in the XKCD case you don't want the big image to be cutoff.
  59. items[0]['f'] = 1
  60. // Optional image. If there is a thumbnail image, we will display it on the right.
  61. items[0]['i'] = image_url
  62. */
  63. ```
  64. ### Notes
  65. 1) Look at the xkcd/spice.js file for a working live example.
  66. 2) If you use internal variables you should put a var statement at the top of the function so they don't leak scope.
  67. 3) If you make html, e.g. by createElement, note d is a global shortcut for document, i.e. d.createElement.
  68. 4) Any functions should exist in your namespace. For example, for twitter the namespace is tr, so the main callback would named nrtr and a helper function would be nrtr_helper_function.
  69. 5) The image is automatically right-floated by default. To avoid looking bad, use <span> and <div> (if you need line breaks) instead of <p>. Also it is good to end with a <span> so the More at X line is on the same line. See the twitter plugin for an example.
  70. 6) Don't use jQuery. We use [YUI2](http://developer.yahoo.com/yui/2/) internally. To set styles you can do:
  71. ```js
  72. YAHOO.util.Dom.setStyle(obj,'margin-top','5px');
  73. ```
  74. If the whole Zero-click Info is an image (like in the XKCD case) you can use this class on the img:
  75. ```js
  76. YAHOO.util.Dom.addClass(img,'cizb');
  77. ```