首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Chrome扩展似乎忽略了javascript来启动一个新的url

问Chrome扩展似乎忽略了javascript来启动一个新的url
EN

Stack Overflow用户
提问于 2020-08-31 12:18:25
回答 1查看 86关注 0票数 0

所以当我点击加载解包时,扩展就安装好了,我就可以填写这些框了。在点击时,我将其设置为只启动google.com,直到我找到它不工作的原因。在我看来,javascript根本没有运行。脚本文件似乎被正确调用,我仔细检查了名称。

清单文件-我不认为这里需要调用js文件。

代码语言:javascript
复制
{
"manifest_version":2, 
"name": "Market Refresher Ext", 
"description": "This extension will push a url with parameters for facebook marketplace",
"version": "1.0", 

"browser_action":{
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": [
"activeTab"]
}

html文件

代码语言:javascript
复制
<!DOCTYPE html>
<html>
   <head>
       <title>Market Refresher</title>
       <script src="popup.js"></script>
   </head>
   <body>
   
   <h1>Market Refresher</h1>
    <label for="City">City:</label>
    <input type="text" id="City" name="City"><br><br>

    <label for="daysSinceListed">DaysSinceListed:</label>
    <input type="text" id="daysSinceListed" name="daysSinceListed"><br><br>

    <label for="query">Query:</label>
    <input type="text" id="Query" name="query"><br><br>

    <label for="category_id">category_id:</label>
    <input type="text" id="CategoryId" name="Category Id"><br><br>

    <label for="Refresh">Refresh:</label>
    <input type="text" id="Refresh" name="Refresh"><br><br>

    <label for="MaxPrice">MaxPrice:</label>
    <input type="text" id="MaxPrice" name="Max Price"><br><br>

     
    <button id="StartRefreshing">Start Rotation!</button><br></br>


    
</body>
</html>

代码语言:javascript
复制
document.getElementById("StartRefreshing").addEventListener("click", build);

//make a function that builds the string and then passes it to 
function build(){
//alert ("Hello World!");
 
//collect data from a text box if filled
let a = document.getElementById("City").value;
let b = document.getElementById("daysSinceListed").value;
let c = document.getElementById("Query").value;
let d = document.getElementById("CategoryId").value;
let e = document.getElementById("Refresh").value;
let f = document.getElementById("MaxPrice").value;
//alert (a+b+c+d+e+f);
//append to string if filled
//temporarily assume that it is all filled
let urlstring = "https://www.facebook.com/marketplace/" + a + "/search?daysSinceListed=1&query=" + c + "&category_id=" + d + "&exact=false";
//console.log(urlstring);
//window.location.href = "google.com";
chrome.tabs.update({
     url: "http://www.google.com/"
});
//place string into URL
}
//place string into URL
EN

回答 1

Stack Overflow用户

发布于 2020-08-31 12:48:21

因为弹出窗口是一个单独的窗口,所以它有自己的devtools。在Chrome中,你可以在弹出窗口中右击,然后点击“检查”,你会在devtools控制台中看到一个关于尝试使用null的addEventListener的错误。在火狐中,这个方法是different。

的问题是,popup.js在DOM中添加其他元素之前运行。

解决方案1将在页面末尾加载脚本:

代码语言:javascript
复制
  <script src="popup.js"></script>
</body>
</html>

解决方案2将在popup.js中等待DOMContentLoaded

代码语言:javascript
复制
document.addEventListener('DOMContentLoaded', () => {
  // here goes your old contents of popup.js
  // ....................
}, {once: true});

如果你使用jQuery,你可以像这样使用$,$(() => { /* your code here */ }),documentation。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63664722

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档