vscode-express
Use webview in VS Code for user interface of VS Code extension.
Get Started
Install
npm install vscode-express --save
Import and Initialize
;;
In Web
Insert script below in your web page:
const callbackStack = ;const vscode = ; { if !cmd return; let args = Array; if typeof argsargslength - 1 === 'function' callback = argsargslength - 1; argslength = argslength - 1; else callback = undefined; args; const messageId = + Math; callbackStack; vscode;} window;
And call VS Code command with command
function. Such as:
;
You can also do something after the command executed with callback:
;
If you need pass parameter to the command, send it with command
:
;
location
Object Access for Web
Generally, you can pass some initial data with location.search
or location.hash
to JavaScript in web. The VS Code webview doesn't support search
or hash
as it uses file system to open the file you request. As a workaround, you can use a built-in variable of VSCExpress, _location
to accesee location.search
, location.hash
and location.href
.
Thus, you need to change your origin code a bit:
const href = _location ? _locationhref : locationhref;
Note: Different from
location
,_location
has onlysearch
,hash
andhref
property.
CORS and Open Links in Browser
Unfortunately, VS Code webview doesn't support CORS. However, it's not hard to make a workaround by using message passing. You can register the action to fetch remote content (with request
module or something else) as a VS Code command, and call the action with command
function mentioned above in the web.
vscode.commands.registerCommand'myExtension.httpRequest', ;
Open links in browser is similar:
vscode.commands.registerCommand'myExtension.openUri',;
Example
https://github.com/Sneezry/vscode-express-example
Build from Source
git clone https://github.com/Sneezry/vscode-express.git
cd vscode express
And you need add prepare
script in package.json
for npm install
:
...
"fix": "gts fix",
"prepare": "node ./node_modules/vscode/bin/install"
}
Then run npm install
npm install
npm run compile
To run the example, compile the code first, then open the example folder with VS Code:
code example
In VS Code, press F5
to start debug.
License
MIT License