# Open with Sci-Hub Chrome Extension
#### prompt:
help me implement a chrome extension, the specifications are listed below:
main features:
1. detect doi from the selected text on the webpage and open a new tab with a auto generated url consists of scihub and the doi as suffix
2. the extension should have add a new option in the right-click dropdown list, and triggers from the option
3. the extension should have its own settings page, allowing user to configure the scihub address since it frequently changes its main domain
4. (optional) the feature can detect the online status of current scihub url can switch to the available mirror automatically
you can decide which program language/framework you want to implement with
#### response:
Creating a Chrome extension involves using web technologies like HTML, JavaScript, and CSS. Below is a basic guide on how to implement the Chrome extension according to your specifications.
### Directory Structure
Create a new directory and inside it create the following files:
- `manifest.json`
- `popup.html`
- `popup.js`
- `background.js`
- `options.html`
- `options.js`
### manifest.json
This file specifies metadata and settings for the extension.
```json
{
"manifest_version": 2,
"name": "DOI Finder",
"version": "1.0",
"description": "Find and open DOI with SciHub",
"permissions": ["contextMenus", "storage", "tabs"],
"background": {
"scripts": ["background.js"]
},
"options_page": "options.html",
"browser_action": {
"default_popup": "popup.html"
}
}
```
### options.html
Create an HTML file for the settings page.
```html
<!DOCTYPE html>
<html>
<head>
<title>Settings</title>
</head>
<body>
<h1>Settings</h1>
<label>
SciHub URL: <input type="text" id="scihub_url">
</label>
<button id="save">Save</button>
<script src="options.js"></script>
</body>
</html>
```
### options.js
Handle saving and restoring of SciHub URL.
```javascript
document.getElementById('save').addEventListener('click', function() {
var url = document.getElementById('scihub_url').value;
chrome.storage.sync.set({"scihub_url": url});
});
chrome.storage.sync.get("scihub_url", function(data) {
document.getElementById('scihub_url').value = data.scihub_url;
});
```
### background.js
Register a context menu and handle its click event.
```javascript
function findDOI(text) {
const regex = /10\.\d{4,9}\/[-._;()\/:A-Z0-9]+/gi;
let m;
let doiList = [];
while ((m = regex.exec(text)) !== null) {
// Avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
doiList.push(m[0]);
}
return doiList;
}
chrome.contextMenus.create({
title: "Find DOI",
contexts: ["selection"],
onclick: function(info) {
var doiList = findDOI(info.selectionText);
if(doiList.length === 0) return; // Exit if no DOI found
chrome.storage.sync.get("scihub_url", function(data) {
doiList.forEach(doi => {
var url = data.scihub_url + '/' + doi;
chrome.tabs.create({ url: url });
});
});
}
});
```
### popup.html and popup.js
This is optional and you can use them to show additional information when the user clicks on the extension icon.
### Installing the Extension
1. Open `chrome://extensions/` in your Chrome browser.
2. Enable "Developer mode".
3. Click on "Load unpacked" and select the directory where your files are stored.
### Note
Please note that creating such an extension might violate the terms of service for some websites. This is just an educational guide, and you should consult legal advice to understand the implications fully.