Skip to content

Instantly share code, notes, and snippets.

@craigjperry2
Last active September 19, 2021 14:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save craigjperry2/83b81b7eaf5cf68ca042a9a0c638a00e to your computer and use it in GitHub Desktop.
Save craigjperry2/83b81b7eaf5cf68ca042a9a0c638a00e to your computer and use it in GitHub Desktop.
Simple Example of Augmenting a Vendor App with a Browser Extension
Simple example of a browser extension to augment a vendor web-app for a poster on HN.
console.log("Loaded extension when visiting http://127.0.0.1:8080");
function populate(items) {
console.log(items);
let datalist = document.createElement('datalist');
datalist.id = 'titlesDatalist';
items.forEach((i) => {
let o = document.createElement('option');
o.value = i;
datalist.appendChild(o);
});
let titlesInput = document.getElementById('titles');
titlesInput.setAttribute('list', 'titlesDatalist');
titlesInput.appendChild(datalist);
}
fetch('https://jsonplaceholder.typicode.com/posts')
.then((response) => response.json())
.then((json) => json.map(({title}) => title))
.then((titles) => populate(titles));
<html>
<head>
<title>HN Example</title>
<link rel="stylesheet" href="https://unpkg.com/mvp.css">
</head>
<body>
<main>
<h1>Vendor App</h1>
<p>You can't change this vendor app's source code.</p>
<section>
<aside>
<label class="label" for="titles">Resource Name</label>
<input class="input" type="text" placeholder="Enter title" id="titles" />
<button>Locate Resource</button>
</aside>
</section>
</main>
</body>
</html>
{
"name": "Example",
"description": "Simple extension example for firefox",
"homepage_url": "https://news.ycombinator.com/item?id=28574733",
"version": "1.0",
"manifest_version": 2,
"content_scripts": [
{
"matches": ["*://localhost/*", "*://127.0.0.1/*"],
"js": ["extension-demo.js"]
}
],
"permissions": [
"https://jsonplaceholder.typicode.com/posts"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment