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

Add search sample #1106

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions api-samples/search/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# chrome.search

This sample demonstrates how to use the [`chrome.search` API](https://developer.chrome.com/docs/extensions/reference/api/search).

## Overview

This extension provides a different way to search via the default provider.
A popup appears when the extension icon is clicked. From there you can specify your query and the location where search results should be displayed.

## Running this extension

1. Clone this repository.
2. Load this directory in Chrome as an [unpacked extension](https://developer.chrome.com/docs/extensions/mv3/getstarted/development-basics/#load-unpacked).
3. Click the extension icon to open the popup.
4. Type the query and select a location.
5. Click the **Search** button.
10 changes: 10 additions & 0 deletions api-samples/search/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"manifest_version": 3,
"name": "Search",
"version": "1.0",
"description": "Uses the chrome.search API to do a search query via the default provider",
"action": {
"default_popup": "popup.html"
},
"permissions": ["search"]
}
26 changes: 26 additions & 0 deletions api-samples/search/popup.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
body {
width: 200px;
}

.container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
}

.row {
margin: 3px;
}

input {
padding: 4px;
}

button {
width: 100%;
padding: 8px;
border-radius: 4px;
margin-top: 12px;
}
27 changes: 27 additions & 0 deletions api-samples/search/popup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!doctype html>
<html>
<head>
<title>Search</title>
<link rel="stylesheet" type="text/css" href="popup.css" />
</head>
<body>
<div class="container">
<h1>Search</h1>
<div class="row">
<label for="query">Query:</label>
<input type="text" id="query" />
</div>
<div class="row">
<label for="location">Location:</label>
<select id="location">
<option value="CURRENT_TAB">Current Tab</option>
<option value="NEW_TAB">New Tab</option>
<option value="NEW_WINDOW">New Window</option>
</select>
</div>
<button type="button" id="search" disabled>Search</button>
</div>

<script src="popup.js"></script>
</body>
</html>
14 changes: 14 additions & 0 deletions api-samples/search/popup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
document.getElementById('query').addEventListener('input', (evt) => {
// Enable the search button only if there is text in the query
document.getElementById('search').disabled = evt.target.value == '';
});

document.getElementById('search').addEventListener('click', () => {
const query = document.getElementById('query').value;
const location = document.getElementById('location').value;

// Don't search if the query is empty
if(query == '') return;

chrome.search.query({text: query, disposition: location});
});