Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法设置null-Chrome扩展名的属性“src”_Javascript_Html_Google Chrome Extension - Fatal编程技术网

Javascript 无法设置null-Chrome扩展名的属性“src”

Javascript 无法设置null-Chrome扩展名的属性“src”,javascript,html,google-chrome-extension,Javascript,Html,Google Chrome Extension,我正在尝试编写一个chrome扩展,它应该能够获取当前打开选项卡的url。 扩展是基于弹出窗口的,因此我制作了一个html,其代码如下: <html> <head> <title>popup</title> </head> <body> <img src="www.insertrandom.com/query=" id="urlstore"> <script src="https://aja

我正在尝试编写一个chrome扩展,它应该能够获取当前打开选项卡的url。 扩展是基于弹出窗口的,因此我制作了一个html,其代码如下:

<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>
另一个问题是,当我打开其他网页上的弹出窗口时,我得到的url是chrome-extension://insertrandomid/popup.html 注意document.getElementByIdurlstore.src=base\u url+curr\u url;,而不是当前url;。 这意味着您的页面上需要一个id为urlstore的元素。如果id为urlstore的元素不存在/include src属性,则会出现无法将属性“src”设置为null的错误。
如果存在document.getElementByIdurlstore,则应记录此元素以检查src属性是否存在:console.logdocument.getElementByIdurlstore

但是如果你看一下html代码,我很清楚id=urlstore1 content.js的标签是一个误导性的名称,因为你在弹出页面中加载它是一个普通脚本,而不是一个内容脚本。看。2弹出窗口是一个单独的页面,有自己的位置。href,因此它不是您想要的,请注意,它是异步的,因此请在回调中使用结果。src属性中的3个URL应以http://或https://开头,否则它将被解释为相对路径。此外,请确保您正在使用弹出窗口。感谢@wOxxOm 1我将更改js文件的名称,这是我的第一个扩展名,因此在复制粘贴时无法管理2确定,我将使用chrome tabs API,但我不理解您编写的部分:它是异步的,所以请使用。。。你能解释一下吗?3 URL以https://开头,我在发布问题时更改了链接。@wOxxOm当我使用代码chrome.tabs.queryactive时,我得到一个错误,无法读取未定义的属性'query'。我的重点是手头的任务不需要内容脚本,只有一个弹出脚本,您可以在其中使用chrome.tabs API,而内容脚本不能使用chrome.tabs和大多数chrome API,因为它们与网页一起运行在一个特殊的隔离环境中。
//content.js
var curr_url = window.location.href;
var base_url = "www.insertrandom.com/query=";
document.getElementById("urlstore").src = base_url+curr_url;