Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ClientSideAPI): Provide standardized way for extensions and userscript to interact #55

Merged
merged 11 commits into from
Dec 11, 2023

Conversation

dboyd13
Copy link
Contributor

@dboyd13 dboyd13 commented Nov 20, 2023

Description of changes:

  • Provide standardized way for extensions and userscript to interact with Threat Composer, by exposing a client-side API on the window object in the threatcomposer namespace.
  • The following client-side APIs are part of this PR
    • getWorkspaceList
    • getCurrentWorkspaceMetadata
    • getCurrentWorkspaceData
    • setCurrentWorkspaceData
    • switchWorkspace
    • createWorkspace
    • deleteWorkspace
    • renameWorkspace

Examples of it's use:

exampleId = 'EXAMPLE_ThreatComposer'

console.log('Switching to ' + exampleId)

window.threatcomposer.switchWorkspace(exampleId)

console.log('Getting JSON object from workspace: ' + exampleId)

const exampleDataObject = await window.threatcomposer.getCurrentWorkspaceData()

console.log(exampleDataObject)

var workspaceName = 'Browser Extension - LocalStorage storage'
var workspaceStorage = 'LocalStorage'
var workspaceMetadata = {
    commitHash: '847ee6a3f3fca117ae1cd13dfa5d5281587c3047',
    creator: 'BrowserExtension'
}

const myWorkspaceObjectOne = await window.threatcomposer.createWorkspace(workspaceName, workspaceStorage)

console.log('Created workspace called ' + myWorkspaceObjectOne.name + ' with id: ' + myWorkspaceObjectOne.id + ' using ' + myWorkspaceObjectOne.storageType + ' storage.')

workspaceName = 'Browser Extension - LocalState storage'
workspaceStorage = 'LocalState'
workspaceMetadata = {
    commitHash: 'ab8e109fde513e9fdbdca0f1f16739ed9242b0c9',
    creator: 'BrowserExtension'
}

const myWorkspaceObjectTwo = await window.threatcomposer.createWorkspace(workspaceName, workspaceStorage, workspaceMetadata)

console.log('Created workspace called ' + myWorkspaceObjectTwo.name + ' with id: ' + myWorkspaceObjectTwo.id + ' using ' + myWorkspaceObjectTwo.storageType + 'storage.')


const listOfWorkspaces = window.threatcomposer.getWorkspaceList()

console.log('Listing workspaces:')

if (listOfWorkspaces) {
listOfWorkspaces.forEach(row => {
    console.log('- Workspace: ' + row.name + ' id:' + row.id)
});
} else {
    console.log('None.')
}

console.log('Loading data from export into: ' + myWorkspaceObjectTwo.name)

window.threatcomposer.setCurrentWorkspaceData(exampleDataObject)

console.log('Stringifing formatted data in preparation to save to file')

const exampleStringifiedData = window.threatcomposer.stringifyWorkspaceData(window.threatcomposer.getCurrentWorkspaceData())

console.log(exampleStringifiedData)

console.log('Switching to workspace: ' + myWorkspaceObjectOne.name)

window.threatcomposer.switchWorkspace(myWorkspaceObjectOne.id)

console.log('Getting details of current workspace')

const myWorkspaceMetadata = await window.threatcomposer.getCurrentWorkspaceMetadata()

console.log(myWorkspaceMetadata)

const newName = 'Bwsr extension - LocalStorage'

console.log('Renaming workspace to: ' + newName)

await window.threatcomposer.renameWorkspace(myWorkspaceMetadata.id, newName)

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@dboyd13 dboyd13 requested a review from jessieweiyi November 20, 2023 08:50
@dboyd13 dboyd13 changed the title Feat/window export feat(ClientSideAPI): Provide standardized way for extensions and userscript to interact Nov 20, 2023
@jessieweiyi jessieweiyi force-pushed the feat/window-export branch 2 times, most recently from 36826de to a0c067e Compare December 10, 2023 23:12
@dboyd13 dboyd13 marked this pull request as ready for review December 11, 2023 20:57
@jessieweiyi jessieweiyi merged commit 716aac3 into main Dec 11, 2023
2 checks passed
@jessieweiyi jessieweiyi deleted the feat/window-export branch December 11, 2023 21:14
Copy link

🎉 This PR is included in version 1.0.29 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants