I’m trying to write a browser extension (for chrome) which queries a specific link on an initial page, opens it, queries a specific link on the new page and opens it again. I managed it to get the first link opened. However, the second link won’t be opened. Is it some permission issue or what can I do? Creating browser extensions is very new to me so maybe I’m missing something obvious.
manifest.json
{
"manifest_version": 3,
"name": "link opener",
"version": "1.0",
"description": "opens links. I hope?",
"permissions": [
"tabs",
"activeTab",
"scripting"
],
"action": {
"default_popup": "popup.html"
}
}
popup.html
<!DOCTYPE html>
<html>
<head>
<title>link opener</title>
<style>
body { width: 400px; padding: 20px; }
#count { font-size: 16px; font-weight: bold; }
</style>
</head>
<body>
<div>
<span>Links opened</span>
<span id="count">-</span>
</div>
<script src="popup.js"></script>
</body>
</html>
popup.js
function openLinks(){
const linksA = document.querySelectorAll("li[id='3']");
for (const linkA of linksA){
const iconList = linkA.querySelectorAll("i.icon.NextPage");
if (iconList.length > 0){
const link = iconList[0].parentElement.href;
if (link != null){
var pageA = window.open(link, "_blank");
const linkB = pageA.document.querySelector("source").src;
window.open(linkB, "_blank");
return linkB;
}
}
}
return "_err";
};
document.addEventListener('DOMContentLoaded', () => {
chrome.tabs.query({ active: true, currentWindow: true }, tabs => {
chrome.scripting.executeScript({
target: {tabId: tabs[0].id},
func: openLinks
});
});
});
I was also able to get with only the second part of the code from page A to page B. But as soon as I want to chain it it won’t work.
Corvin Laube is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.