forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredirect-users-based-on-input-codes.json
23 lines (23 loc) · 2.76 KB
/
redirect-users-based-on-input-codes.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
"docs": "Use this task to prompt your users for a code, redirecting them to an appropriate destination based on what they enter. Some assembly required, with sample Javascript and HTML provided. :)\n\nConfigure this task by filling in redirect URLs on the left, and a series of codes for each redirect on the right. The intent is to route users to the appropriate redirect, based on the code they enter.\r\n\r\nThis task doesn't run automatically. Instead, it generates some Javascript that's automatically embedded into your online storefront, redirecting the user when they enter a code that matches one of the redirects you've configured.\r\n\r\nYou can prompt the user for a code yourself, and pass the result to the function `attemptRedirectByCode`, like this:\r\n\r\n```js\r\n<script>\r\n attemptRedirectByCode(prompt('Enter your code here:'))\r\n</script>\r\n```\r\n\r\nOr, modify this HTML to set up a form for your users:\r\n\r\n```\r\n<form id=\"redirect-form\">\r\n <input type=\"text\" placeholder=\"Enter your code here\">\r\n</form>\r\n<script>\r\n document.getElementById('redirect-form').onsubmit = function (event) {\r\n event.preventDefault();\r\n var input = event.target.children[0];\r\n attemptRedirectByCode(input.value) || input.select();\r\n };\r\n</script>\r\n```",
"halt_action_run_sequence_on_error": false,
"name": "Redirect users based on input codes",
"online_store_javascript": "var redirects = {{ options.redirects_and_codes__keyval_multiline | json }};\nvar codesToRedirects = {};\n\nObject.getOwnPropertyNames(redirects).forEach(function (redirect) {\n redirects[redirect].split(\"\\n\").map(function (code) {\n {% if options.require_case_sensitive_codes__boolean %}\n return code.toLowerCase().trim();\n {% else %}\n return code.trim();\n {% endif %}\n }).forEach(function (code) {\n codesToRedirects[code] = redirect;\n });\n});\n\nwindow.attemptRedirectByCode = function (code) {\n {% unless options.require_case_sensitive_codes__boolean %}code = code.toLowerCase();{% endunless %}\n code = code.trim();\n \n if (codesToRedirects[code]) {\n window.location.href = codesToRedirects[code];\n return true;\n } else {\n alert({{ options.invalid_code_message__required | json }});\n return false;\n }\n};",
"options": {
"redirects_and_codes__keyval_multiline": {
"/pages/secret-one": "one\num\nuno",
"/pages/secret-two": "two\ndois\ndos",
"/pages/secret-three": "three\ntrês\ntres"
},
"require_case_sensitive_codes__boolean": false,
"invalid_code_message__required": "That code wasn't valid - try again."
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "",
"subscriptions": [],
"subscriptions_template": "",
"tags": [
"Redirects"
]
}