I'm trying to write a chrome extension which should pick up url of the currently open tab. The extension is popup based so I made an html the code of which is as follows:
<html>
<head>
<title>popup</title>
</head>
<body>
<img src="www.insertrandom.com/query=" id="urlstore">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="content.js"></script>
</body>
</html>
when I open this webpage and inspect element (ctrl+shift+i), there are NO errors at all. But if I inspect elements on other webpages(which are actually on internet) like, stackoverflow I get the error:
Cannot set property 'src' of null
Previously asked questions on stackoverflow, like these: Cannot set property 'src' of null and Error: Cannot set property 'src' of null are unable to solve the issue.
//content.js
var curr_url = window.location.href;
var base_url = "www.insertrandom.com/query=";
document.getElementById("urlstore").src = base_url+curr_url;
And another problem is that when I open the popup on other web pages, the url that I get is chrome-extension://insertrandomid/popup.html
and not the current url