I am making a very basic chrome extension (this will be my first) where the extension is simply opening another site in a popup window when clicked.
it uses urls from 2 sites
site.a = static url = example.com
site.b = dynamic url = current site domain
it is simply supposed to take the primary domain from site.b, and append it to the end of site.a, so it should read something like this:
site.a/site.b
or
example.com/current_site_domain.com
I managed to get the site.a to show up correctly but the tricky bit is getting site.b to add onto the end.
can someone input their expertise here?
The Manifest.json i have is as follows:
{
"manifest_version": 3,
"name": "Website Worth",
"version": "0.2",
"description": "Shows estimated worth of current website",
"icons": {
"32": "/images/icon.png"
},
"action": {
"default_icon": "/images/icon.png",
"default_popup": "popup.html"
},
"permissions": [
"tabs"
]
}
The popup html i have is as follows:
<html>
<div>
<iframe
width="400"
height="1000"
scrolling="yes"
id="currentsite"
src=""
></iframe>
<script src="script.js"></script>
</div>
</html>
the script i have is as follows:
var url = window.location.host;
var shorturl = url.replace(/^(?:https?:\/\/)?(?:www\.)?/i, "").split("/")[0];
document.getElementById("currentsite").src =
"https://www.example.com/" + shorturl;
the error i seem to be getting is that the url gets calculated and displayed as the following:
instead of:
site.a/site.b
It returns:
site.a/js/cookieconsent.latest.min.js
which obviously just isnt working.
I have found posts here saying to get the url to be calculated correctly, use the following string:
chrome.tabs.query(
{
active: true,
currentWindow: true,
},
(tabs) => {
var url = tabs[0].url;
}
);
I have tried putting this in place of the current first line on the script but it is not having the desired effect, im quite new to Javascript so im not sure what to do here.
please can someone give offer some advice?
it would be much appreciated!!