Is it possible to use code for filtering extension?

Currently, the only way to determine whether extension should appear in bar or not based on selected text is regex. However, some things are easily parsed with code, but hard with regex.

Start from simple example: I want extension to show up when user selected number between 0 and 255. In code, it’s simple: parse number, compare with bounds. In regex, it’s either imprecise: “\d{1,3}” or complex: “25[0-5]|2[0-4]\d|\d{1,2}”.

Now imagine regex for ipv6 address.

Is it possible to add code filter for extensions?

Hi! Yes it absolutely possible using JavaScript (or TypeScript code)

You need to use the dynamic entitlement and then a population function (exported actions() function) in a module-based extension.

If that sounds complex maybe take a look at this example:
https://github.com/pilotmoon/PopClip-Extensions/blob/master/source/Convert.popclipext/convert.ts

Look at the exported actions function at the bottom – it’s checking to see if the input text meets the criteria then exports an action if appropriate.

Here’s a minimal snippet example:

// #popclip
// name: Dynamic Action Example
// entitlements: [dynamic]
// language: javascript
// module: true
function isValidInput(inputString) {
  const num = parseInt(inputString, 10);
  return !isNaN(num) && num >= 0 && num <= 255;
}
exports.actions = (input, options, context) => {
  const result = [];
  if (isValidInput(input.text)) {
    result.push({
      title: "winner!",
      code: (input, options, context) => {
        popclip.showText(`Your text was ${input.text}`);
      }
    });
  };
  return result;  // can either return undefined, null, or empty array if there are no actions
};

Let me know if you’d like anything explaining more.

2 Likes