node.js - Error: "There is no data provider registered that can provide view data", while coding Visual Studio

While coding and running an extension in debug mode, I get the error mentioned in the title.Here is my

While coding and running an extension in debug mode, I get the error mentioned in the title.

Here is my extension.js:

// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');

// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed

/**
 * @param {vscode.ExtensionContext} context
 */

function getWebviewContent() {
    return `<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Video Player</title>
</head>
<body>
    <h1>Video Player</h1>
    <video width="640" height="360" controls>
        <source src=".mp4" type="video/mp4">
        Your browser does not support the video tag.
    </video>
</body>
</html>`;
}

function activate(context) {

    // Use the console to output diagnostic information (console.log) and errors (console.error)
    // This line of code will only be executed once when your extension is activated
    console.log('Congratulations, your extension "YourApp" is now active!');
    
    let disposable = vscodemands.registerCommand('extension.openVideo', function () {
        // Create and show a new webview
        const panel = vscode.window.createWebviewPanel(
            'videoView', // Identifies the type of the webview. Used internally
            'Video Player', // Title of the panel displayed to the user
            vscode.ViewColumn.One, // Editor column to show the new webview panel in.
            {
                enableScripts: true, // Enable JavaScript in the webview
                retainContextWhenHidden: true // Keep the webview's state even when it's hidden
            }
        );

        // Set the HTML content for the webview
        panel.webview.html = getWebviewContent();
    });

    context.subscriptions.push(disposable);
}

// This method is called when your extension is deactivated
function deactivate() {}

module.exports = {
    activate,
    deactivate
}

This is my package.json:

{
    "name": "financenews",
    "displayName": "FinanceNews",
    "description": "",
    "version": "0.0.1",
    "engines": {
        "vscode": "^1.98.0"
    },
    "categories": [
        "Other"
    ],
    "activationEvents": 
    [ 
        "onView:videoView", "onCommand:extension.openVideo" 
    ],
    "main": "./extension.js",
    "contributes": {
        "commands": [{
            "command": "financenews.helloWorld",
            "title": "Hello World"
        },
        {
            "command": "extension.openVideo",
            "title": "Open Video"
        }],
    "viewsContainers": {
        "activitybar": [
            {
                "id": "videoSidebar",
                "title": "Video Sidebar",
                "icon": "media/mario.svg"
            }
        ]
    },
    "views": {
      "videoSidebar": [
        {
          "id": "videoView",
          "name": "Video Player"
        }
      ]
    }
  },
  "scripts": {
    "lint": "eslint .",
    "pretest": "npm run lint",
    "test": "vscode-test"
  },
  "devDependencies": {
    "@types/vscode": "^1.98.0",
    "@types/mocha": "^10.0.10",
    "@types/node": "20.x",
    "eslint": "^9.21.0",
    "@vscode/test-cli": "^0.0.10",
    "@vscode/test-electron": "^2.4.1"
  }
}

I have tried the following things:

  1. Used 'explorer' instead of 'videoSidebar'
  2. Tried creating a class for VideoWebView and then tried to place getWebView() as a function inside the class.
  3. Tried loading a video from a directory instead of using .mp4 for source

While coding and running an extension in debug mode, I get the error mentioned in the title.

Here is my extension.js:

// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');

// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed

/**
 * @param {vscode.ExtensionContext} context
 */

function getWebviewContent() {
    return `<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Video Player</title>
</head>
<body>
    <h1>Video Player</h1>
    <video width="640" height="360" controls>
        <source src="https://www.w3schools/html/mov_bbb.mp4" type="video/mp4">
        Your browser does not support the video tag.
    </video>
</body>
</html>`;
}

function activate(context) {

    // Use the console to output diagnostic information (console.log) and errors (console.error)
    // This line of code will only be executed once when your extension is activated
    console.log('Congratulations, your extension "YourApp" is now active!');
    
    let disposable = vscodemands.registerCommand('extension.openVideo', function () {
        // Create and show a new webview
        const panel = vscode.window.createWebviewPanel(
            'videoView', // Identifies the type of the webview. Used internally
            'Video Player', // Title of the panel displayed to the user
            vscode.ViewColumn.One, // Editor column to show the new webview panel in.
            {
                enableScripts: true, // Enable JavaScript in the webview
                retainContextWhenHidden: true // Keep the webview's state even when it's hidden
            }
        );

        // Set the HTML content for the webview
        panel.webview.html = getWebviewContent();
    });

    context.subscriptions.push(disposable);
}

// This method is called when your extension is deactivated
function deactivate() {}

module.exports = {
    activate,
    deactivate
}

This is my package.json:

{
    "name": "financenews",
    "displayName": "FinanceNews",
    "description": "",
    "version": "0.0.1",
    "engines": {
        "vscode": "^1.98.0"
    },
    "categories": [
        "Other"
    ],
    "activationEvents": 
    [ 
        "onView:videoView", "onCommand:extension.openVideo" 
    ],
    "main": "./extension.js",
    "contributes": {
        "commands": [{
            "command": "financenews.helloWorld",
            "title": "Hello World"
        },
        {
            "command": "extension.openVideo",
            "title": "Open Video"
        }],
    "viewsContainers": {
        "activitybar": [
            {
                "id": "videoSidebar",
                "title": "Video Sidebar",
                "icon": "media/mario.svg"
            }
        ]
    },
    "views": {
      "videoSidebar": [
        {
          "id": "videoView",
          "name": "Video Player"
        }
      ]
    }
  },
  "scripts": {
    "lint": "eslint .",
    "pretest": "npm run lint",
    "test": "vscode-test"
  },
  "devDependencies": {
    "@types/vscode": "^1.98.0",
    "@types/mocha": "^10.0.10",
    "@types/node": "20.x",
    "eslint": "^9.21.0",
    "@vscode/test-cli": "^0.0.10",
    "@vscode/test-electron": "^2.4.1"
  }
}

I have tried the following things:

  1. Used 'explorer' instead of 'videoSidebar'
  2. Tried creating a class for VideoWebView and then tried to place getWebView() as a function inside the class.
  3. Tried loading a video from a directory instead of using https://www.w3schools/html/mov_bbb.mp4 for source
Share Improve this question edited Mar 8 at 19:02 marc_s 756k184 gold badges1.4k silver badges1.5k bronze badges asked Mar 8 at 18:45 Anubhav PandeyAnubhav Pandey 1,2952 gold badges14 silver badges19 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I made it work.

Extension.js

const vscode = require('vscode');

function activate() {
    // Register the custom view
    vscode.window.registerWebviewViewProvider('videoView', {
      resolveWebviewView(webviewView) {
        webviewView.webview.options = {
          enableScripts: true,
        };
  
        // Set the HTML content for the webview
        webviewView.webview.html = getWebviewContent();
      },
    });
  }
  
  function getWebviewContent() {
    return `
      <!DOCTYPE html>
      <html lang="en">
      <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Video Slayer</title>
      </head>
      <body>
        <h1>Video Player</h1>
        <video width="320" height="240" controls>
          <source src="https://www.w3schools/html/mov_bbb.mp4" type="video/mp4">
          Your browser does not support the video tag.
        </video>
      </body>
      </html>
    `;
  }
function deactivate() {}

module.exports = {
    activate,
    deactivate
};

package.json

{
  "name": "YourApp",
  "displayName": "YourApp",
  "description": "",
  "version": "0.0.1",
  "engines": {
    "vscode": "^1.98.0"
  },
  "categories": [
    "Other"
  ],
  "main": "./extension.js",
  "activationEvents": [
        "onCommand:extension.openVideo"
    ],
  "contributes": {
    "viewsContainers": {
      "activitybar": [
        {
          "id": "videoSidebar",
          "title": "Video Sidebar",
          "icon": "media/mario.svg"
        }
      ]
    },
    "views": {
      "videoSidebar": [
        {
          "id": "videoView",
          "name": "Video Player",
          "type": "webview"
        }
      ]
    }
    },
  "scripts": {
    "lint": "eslint .",
    "pretest": "npm run lint",
    "test": "vscode-test"
  },
  "devDependencies": {
    "@types/vscode": "^1.98.0",
    "@types/mocha": "^10.0.10",
    "@types/node": "20.x",
    "eslint": "^9.21.0",
    "@vscode/test-cli": "^0.0.10",
    "@vscode/test-electron": "^2.4.1"
  }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744888762a4599278.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信