在Javascript for Chrome扩展程序的多个函数中打开新选项卡 [英] Issue opening new tab in multiple functions in Javascript for Chrome Extension

查看:151
本文介绍了在Javascript for Chrome扩展程序的多个函数中打开新选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Chrome扩展程序,但出于某种原因,我只能在一个函数中创建一个新选项卡,而不是在多个函数中。

代码 -

  function editorial(){
chrome.tabs.query({currentWindow:true,active:true},函数(标签){
var tab_url = tabs [0] .url;
var new_url = tab_url.slice(11);
chrome.tabs.create({url:http:/ /www.discuss。+ new_url});
});

$ b document.addEventListener('DOMContentLoaded',function(){
var btn = document.getElementById('viewmanding');
if(btn){
btn.addEventListener('click',editorial);
}
});

函数friends()
{
var frnd_name = document.getElementById('frnd1')。value;
alert(frnd_name +rocks);
/*chrome.tabs.query({currentWindow:true,active:true},function(tabs){
var tab_url = tabs [0] .url;
var new_url = tab_url。 slice(11);
chrome.tabs.create({url:http://www.discuss。+ new_url}); * /


}
document.addEventListener('DOMContentLoaded',function(){
var btn2 = document.getElementById('viewFriends');
if(btn2){
btn2.addEventListener('click' ,朋友);
}
});

我希望能够使用frnd_name在函数friends()中的新选项卡中打开一个url。



如果注释部分未使用,那么它工作正常,并且警报消息也是即将到来的,但如果评论部分因为某种原因而被使用,那么它就不起作用。



关于错误/问题的任何想法?

解决方案

好的,问题是由于缺少'}'而导致的。
我浪费了很多时间在这一点上,最终这个错误非常简单。
现在,我已经吸取了教训,总是检查你的代码两次,以确保正确的括号。


I am working on a Chrome extension, but for some reason I am able to create a new tab only in a single function and not in multiple functions.

Code-

function editorial() {
  chrome.tabs.query({currentWindow: true, active: true}, function(tabs){
    var tab_url=tabs[0].url;
    var new_url=tab_url.slice(11);
    chrome.tabs.create({ url:"http://www.discuss." + new_url});        
  });
}

document.addEventListener('DOMContentLoaded', function () {
  var btn = document.getElementById('viewEditorial');
  if (btn) {
    btn.addEventListener('click', editorial);
  }
});

function friends()
{
    var frnd_name=document.getElementById('frnd1').value;
    alert(frnd_name+"rocks");
    /*chrome.tabs.query({currentWindow: true, active: true}, function(tabs){
        var tab_url=tabs[0].url;
        var new_url=tab_url.slice(11);
        chrome.tabs.create({ url:"http://www.discuss." + new_url});*/


}
document.addEventListener('DOMContentLoaded', function () {
  var btn2 = document.getElementById('viewFriends');
  if (btn2) {
    btn2.addEventListener('click', friends);
  }
});

I want to be able to use the frnd_name to open a url in a new tab in the function friends().

If the commented part is not used then it is working fine and the alert message is also coming but if the commented part is used then for some reason, it does not work.

Any ideas as to what the bug/problem is?

解决方案

Well, the problem was due to a missing '}' that was not there. I wasted a lot of time over this and ultimately the bug was such a simple one. Now, I have learnt a lesson, always check ur code twice for proper braces.

这篇关于在Javascript for Chrome扩展程序的多个函数中打开新选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆