Based on your requirements, here's how you could approach building a Chrome extension for Microsoft Edge that redirects downloads to specific folders based on keywords:
- Keyword Detection:
You can use regular expressions or string matching to detect keywords in URLs. For example:
function detectKeyword(url) {
const keywords = {
"the-sims-4": /the[-_]sims[-_]4/i,
"minecraft": /minecraft/i
};
for (let [key, regex] of Object.entries(keywords)) {
if (regex.test(url)) return key;
}
return null;
}
- Website Content Analysis:
If the URL doesn't provide enough information, you can analyze the webpage content:
function analyzePageContent() {
const text = document.body.innerText;
const keywords = ["The Sims 4", "Minecraft"];
for (let keyword of keywords) {
if (text.toLowerCase().includes(keyword.toLowerCase())) {
return keyword;
}
}
return null;
}
- User Interface:
Create a popup HTML file for settings:
<!DOCTYPE html>
<html>
<body>
<h2>Download Redirector Settings</h2>
<label for="sims4-path">The Sims 4 Path:</label>
<input id="sims4-path" type="text"/><br/><br/>
<label for="minecraft-path">Minecraft Path:</label>
<input id="minecraft-path" type="text"/><br/><br/>
<button id="save">Save</button>
<script src="popup.js"></script>
</body>
</html>
- Extension Setup:
Create a
manifest.json file:
{
"manifest_version": 3,
"name": "Download Redirector",
"version": "1.0",
"description": "Redirects downloads to specific folders",
"permissions": ["downloads", "storage", "activeTab"],
"action": {
"default_popup": "popup.html"
},
"background": {
"service_worker": "background.js"
}
}
- Background Script:
Create a
background.js file to handle downloads:
chrome.downloads.onDeterminingFilename.addListener((item, suggest) => {
chrome.storage.sync.get(['sims4Path', 'minecraftPath'], (result) => {
const keyword = detectKeyword(item.url) || analyzePageContent();
let path = '';
if (keyword === 'the-sims-4' && result.sims4Path) {
path = result.sims4Path;
} else if (keyword === 'minecraft' && result.minecraftPath) {
path = result.minecraftPath;
}
if (path) {
suggest({filename: `${path}/${item.filename}`});
} else {
suggest({filename: item.filename});
}
});
return true;
});
- Installing in Microsoft Edge:
- Open Edge and go to
edge://extensions/