I am in need of creating a Chrome extension that will showcase the latest offers and coupons whenever I visit any affiliate store like Amazon, AliExpress, Flipkart, Myntra, etc.
Upon visiting their website with my extension installed, a popup with offers and coupons from that particular site should be displayed. (I have a webservice to fetch these offers and coupons).
The concept is illustrated in the image below. https://i.sstatic.net/QoUzb.png
I have experimented with something similar, but I'm not certain if it's the correct approach.
From Manifest.json
"content_scripts": [
{
"matches": ["*://*/*"],
"js": ["jquery.js","myscript.js"],
"css":["offers.css"]
}
],
"web_accessible_resources":[
"offers.html",
"offers.js"
]
myscript.js
var url = chrome.extension.getURL('offers.html');
var iframe = "<iframe src='"+url+"' id='IframeID'></iframe>";
var host = window.location.host;
var splittedHost = host.split('.');
var title = $(document).find("title").text();
if(title != ''){
$('body').append(iframe);
}
offers.html
<html>
<head>
<meta charset="UTF-8">
<title>test| Latest Offers</title>
<script src="jquery.js"></script>
<script src="offers.js"></script>
</head>
<body id="bodyText">
<h1>
Welcome
</h1>
...
</body>
</html>
With this setup, I see something similar to this on every page I visit:
https://i.sstatic.net/B0dJW.png
The iframe actually represents the offers page, and to retrieve offer data, I require the hostname from the URL. When trying to obtain window.localtion.host
within offers.js injected into offers.html, I receive chrome://extension
.
Could someone advise me on how to retrieve the hostname in offers.js? Is there a way to append data to offers.html from myscripts.js? And where should I call the API and how can I add the result to the body of the iframe?