-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
71 lines (67 loc) · 2.01 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// 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');
const fetch = require('node-fetch');
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
console.log('Congratulations, your extension "search-overflow" is now active!');
context.subscriptions.push(vscode.commands.registerCommand('search-overflow.searchOverflow', async () => {
(async() =>{
var intitle = await vscode.window.showInputBox({placeHolder: "Enter keywords !!"});
var url = 'https://api.stackexchange.com/2.2/search?order=desc&sort=votes&intitle='+intitle+'&site=stackoverflow';
const response = await fetch(url);
const data = await response.json();
const panel = vscode.window.createWebviewPanel(
'searchOverflow',
'Search Overflow',
vscode.ViewColumn.Two,
{
// Enable scripts in the webview
enableScripts: true
}
);
var len = data['items'].length;
if(len==0){
panel.webview.html = `<h1>No Questions Found</h1>`;
}
else{
var dataList = "";
for(let i=1; i<=Math.min(10,len);i++){
dataList += `<a href=\"`;
dataList += data['items'][i-1]['link'];
dataList += `" class=\"list-group-item list-group-item-action active\">`;
dataList += data['items'][i-1]['title'];
dataList += `</a>`;
}
panel.webview.html = getWebviewContent(dataList);
}
})()
}));
}
function getWebviewContent(data) {
// return `<!DOC
return `<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
<div>
<h1> Questions </h1>
</div>
<div>
<ul class="list-group">`+
data +
`</ul>
</div>
</body>
</html>`;
}
// this method is called when your extension is deactivated
function deactivate() {}
module.exports = {
activate,
deactivate
}