This guide provides a comprehensive, step-by-step process to create your first custom widget using Vanilla JavaScript and the TakeProfit Widget SDK (version 0.2.3). By following this guide, you’ll set up a simple project structure, integrate the SDK, configure necessary security headers, and implement basic functionalities to interact with the TakeProfit platform. This example serves as a foundation for building more complex and feature-rich widgets tailored to your specific needs.
index.html
Open src/index.html
and include the SDK script:
http-server
Use http-server
to serve your files with custom headers.
server.js
File
Create a simple Node.js server to serve your widget with the required headers.
server.js
and add the following code:
src
directory.X-Frame-Options
and Content-Security-Policy
headers to allow embedding only from https://takeprofit.com
.404.html
page (create this file if desired).404.html
Page (Optional)
http://localhost:8080
.
app.js
to Initialize the SDK
Open src/app.js
and add the following code:
initializeSDK
: Connects to the TakeProfit platform and hides the loader. Subscribes to widget and channel state changes.cleanupSDK
: Disconnects from the SDK when the widget is unloaded.index.html
with Interactive Elements
Ensure your index.html
includes buttons to interact with the SDK:
styles.css
Add basic styling to src/styles.css
:
app.js
to Handle Button Clicks
Enhance src/app.js
to handle interactions:
if (result.isSuccessful)
to if (result.payload.isSuccessful)
to correctly access the isSuccessful
property within the payload
.${result.error}
to ${result.payload.error}
to correctly access the error
message within the payload
.app.js
The menu item functions are already implemented in Step 6. Here’s a recap:
http://localhost:8080
.
F12
or Ctrl+Shift+I
to open the developer tools.
'http://localhost:8080'
with your actual widget URL if different.imageUrl
for the widget icon.index.html
, styles.css
, app.js
, etc.) are correctly set up and that your server configuration (server.js
) is compatible with your hosting service. Some hosting services like Netlify and Vercel handle headers via configuration files.
netlify.toml
File
netlify.toml
configuration.iframeUrl
in the TakeProfit platform’s local storage to point to your deployed widget’s URL:
requestId
and menuItemId
are unique to prevent conflicts.
takeprofit.com
).iframeUrl
and ensure it points to the correct widget URL.TPWidgetSDK.connect()
and TPWidgetSDK.hideLoader()
are called successfully.securityID
you are requesting is valid and recognized by the platform..catch
Blocks: Ensure that all asynchronous SDK methods have proper error handling using .catch
to manage rejected promises.