This article was first published on my blog: muyunyun.cn/posts/4c23b…

Plug-in address (integrated Github, Nuggets, Zhihu, Taobao and other search)

It’s easy to see how Alfred, the perennial top Mac gadget, makes it easy to find just about any API, especially workflow. You can quickly find your favorite movies, books and music on Douban. You can quickly upload pictures to the map bed and so on.

Some of the amway

Attached is a screenshot of the plugin installed personally. Caffeinate plug-in can make the computer at a designated time not black screen; You can easily look up any document on the Dash plugin. Youdao Translate is much more convenient than the system’s own version. The plugin also varies from person to person, so you can browse the Workflow List for your own needs.

After using other people’s plug-ins, I had the idea of writing a plug-in. I planned to collect the websites I often visit into a plug-in, and use specific acronyms to quickly search for data. Alfred Workflow can be developed using bash, ZSH, PHP, Ruby, Python, Perl, and Apple Script. So I chose Node.js as the development language and developed a commonSearch, and the results are as follows (integrating Github, Nuggets, Zhihu, Taobao and other searches).

The development phase

Before development, we must first have a certain understanding of some specific operating steps and knowledge points, so that there is basically no big obstacle when developing.

The front steps

You can first refer to the beginning of how to write a third-party workflow to complete the basic workflow setup. The following picture shows the basic workflow setup.

/usr/local/bin/node common_search.js creates a node service when the plugin is called. The following 1 is used to distinguish which search is being called manually to common_search.js, and {query} is the name of the user query.

Call the JSON API with Node.js

The initial development referred to the zhihu search project, which was based on the Cheerio module to analyze and crawl the requested webpage data. However, after the introduction of Cheerio, the volume of the plug-in increased by 2M, which was too unfriendly for a plug-in. So maybe a language like Python is better suited for developing plug-ins like this (guess: Python doesn’t need a third-party library to crawl), so I started to choose interfaces that provide JSON APIS, such as digging for nuggets to return data. Start by opening the Chrome console, which is probably familiar to front-end engineers.

The interface to return the search data is https://search-merger-ms.juejin.im/v1/search? query={query}&page=0&raw_result=false&src=web

We then happily use the HTTPS module provided by Node. One caveat here is that the res parameter in the http.get() callback is not a body, but an HTTP.clientResponse object, so we need to assemble the content.

var options = {
    host: 'search-merger-ms.juejin.im'.path: '/v1/search? query=' + encodeURI(keyword) + '&page=0&raw_result=false&src=web'
  }
  https.get(options, function (res) {
    res.on('data', (chunk) => {
      var content += chunk
    }).on('end'.function () {
      var jsonContent = JSON.parse(content) && JSON.parse(content).d
      var result_array = []
      for (var i = 0; i < jsonContent.length; i++) {
        if (jsonContent[i].user.jobTitle === ' ') {
          result_array.push({
            title:
            subtitle:
            arg:
            icon: {
              path: join(__dirname, 'xx.png'),},mods: {
              cmd: {}
            }
          })
        }
      }
      content = ' '
      console.log(JSON.stringify({
        items: result_array
      }))
    })
  })Copy the code

This method is the most direct way to call the JSON API, and can also be used by a third-party module request to parse JSON, as shown in the following example:

var request = require('request')

var url = 'search-merger-ms.juejin.im/v1/search? query=' + encodeURI(keyword) + '&page=0&raw_result=false&src=web'

request.get({
    url: url,
    json: true.headers: {'User-Agent': 'request'}
  }, (err, res, data) => {
    if (err) {
      console.log('Error:', err);
    } else if(res.statusCode ! = =200) {
      console.log('Status:', res.statusCode);
    } else {
      // data is already parsed as JSON:
      console.log(data.html_url); }});Copy the code

Another thing to note is that the field of return value is fixed. For details, you can refer to its official explanation. It took a long time to find out the custom format of Icon in JS.

Title: main title: content row ARg: jump link ICONS: mods: methods for customizing keyboard keysCopy the code

Github, Nuggets, Zhihu, and Taobao are all based on the above ideas, with different processing of the specific JSON data returned. Although rough, it is also the first Alfred Workflow plug-in development.

The end of the

The purpose of this article is to provide a brief overview of how to develop a plugin, and to let more people know that it is not difficult to develop a plugin. At the same time, let more people develop a meaningful and interesting Alfred-Workflow plugin.