Chrome.tabs.executeScript - 制表符未定义 [英] Chrome.tabs.executeScript - tabs are not defined

查看:115
本文介绍了Chrome.tabs.executeScript - 制表符未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个名为'btn3'的按钮的Chrome扩展。当我点击Chrome扩展程序中的那个按钮(popup.html)时,它会点击网页上的一个按钮。网页上的按钮具有以下ID:常规 - 辅助按钮发送消息



2个问题:


  1. 我在下面的代码中得到了chrome.tabs.executeScript的制表符未定义错误。我该如何解决这个问题?

  2. 我在content_scripts.js中写错了什么?

感谢!



chrome扩展窗口中的按钮脚本
$ b

  document.addEventListener('DOMContentLoaded',function(){
document.getElementById('btn3')。addEventListener('click',sendInMail)
});

sendInMail = function(){


chrome.tabs.executeScript(tabs [0],{file:content_script.js});

$ b

content_scripts.js

  alert(content_script is working!); 

函数clickSendInMailButton(){
var SendInMailButton = document.getElementsByClassName(regular-secondary-button send-message),

SendInMailButton.click();
}

clickSendInMailButton();

Manifest.json

  {
manifest_version:2,

name:LinkedIn助手,
description: ,
version:1.0,

browser_action:{
default_icon:icon.png,
default_popup:popup.html
},

content_scripts:[
{
matches:[http:// * b * b,
chrome_url_overrides: {
newtab:newtab.html
},

background:{
scripts:[bg.js]
},

权限:[
标签,< all_urls>
]


}


解决方案

标签[0] - 你需要找到它。您的脚本在popup.html的上下文中执行。我希望popup.html包含

 < head> 
...
< script src =popup.js>< / script>
< / head>


在Chrome扩展窗口中按钮的脚本

来自popup.js。你永远不会尝试在popup.html中运行代码。



因此,在这种情况下,没有人知道选项卡[0] 。您需要询问chrome哪个窗口和哪个选项卡现在处于活动状态。它可以是ShwetaM写的,或者像这样来自样本 a>

  chrome.windows.getCurrent(function(currentWindow)){
chrome.tabs.query({active: true,windowId:currentWindow.id},function(activeTabs){
activeTabs.map(function(tab){
chrome.tabs.executeScript(tab.id,{file:'content_script.js', allFrames:false});
});
});
});

或者您可以尝试不使用 tab.id,

  chrome.tabs.executeScript({file:'content_script.js',allFrames:false}); 

因为在这种情况下,脚本将在当前窗口的活动标签



当您在Manifest.json中指定content_scripts:脚本将在您创建标签并执行每刷新一次。我不确定,但也许在加载DOM之前。但是,您不应在Manifest.json中指定content_scripts:,因为这个内容脚本的代码总是被注入



另外,你必须确定active标签包含您正在查找的按钮。对某些对象使用 console.dir ,特别是函数中的参数; console.log 用于某些文本; 调试器用于快速调试。


I am trying to write a chrome extension that has a button called 'btn3'. When I click on that button in the chrome extension (popup.html), it will click a button on the webpage. The button on the webpage has the following id: "regular-secondary-button send-message"

2 questions:

  1. I get the error of "tabs are not defined" for chrome.tabs.executeScript in the following codes. How can I fix that?
  2. Did I write anything wrong in the content_scripts.js?

Thanks!

Script of the button in chrome extension window

document.addEventListener('DOMContentLoaded', function (){
    document.getElementById('btn3').addEventListener('click', sendInMail)
});

sendInMail = function(){


    chrome.tabs.executeScript(tabs[0], {file: "content_script.js"});

}

content_scripts.js

alert("content_script is working!");

function clickSendInMailButton() {
    var SendInMailButton = document.getElementsByClassName("regular-secondary-button send-message"),

    SendInMailButton.click();
}

clickSendInMailButton();

Manifest.json

{
  "manifest_version": 2,

  "name": "LinkedIn Assistant",
  "description": "This extension makes a LSS AE successful.",
  "version": "1.0",

  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },

  "content_scripts": [
    {
      "matches": ["http://*/*", "https://*/*"],
      "js": ["content_script.js"]
    }
  ],
   "chrome_url_overrides" : {
    "newtab": "newtab.html"
  },

    "background": {
    "scripts": ["bg.js"]
  },

  "permissions": [
    "tabs", "<all_urls>"
  ]


}

解决方案

the error of "tabs are not defined"

tabs[0] - you need to find it. Your script is executed in the context of popup.html. I hope popup.html contain

<head>
...
    <script src="popup.js"></script>
</head>

and

Script of the button in chrome extension window

is from popup.js. And you never try to run code inside popup.html.

So, in this context no one knows about tabs[0]. You need to ask chrome which window and which tab is active now. It can be as ShwetaM wrote, or like this from samples

chrome.windows.getCurrent(function (currentWindow) {
    chrome.tabs.query({ active: true, windowId: currentWindow.id }, function (activeTabs) {
        activeTabs.map(function (tab) {
            chrome.tabs.executeScript(tab.id, { file: 'content_script.js', allFrames: false });
        });
    });
});

or you can try without the tab.id,

chrome.tabs.executeScript({ file: 'content_script.js', allFrames: false });

because in this case the script will run in the active tab of the current window

When you specify in Manifest.json the "content_scripts": script will be executed when you create a tab and every refresh. I'm not sure, but maybe before loading the DOM. However, you should not specify the "content_scripts": in Manifest.json, because this content script's code will be always injected

Also you must be sure that the active tab contains a button that you are looking for. Use console.dir for some object, especially arguments in a function; console.log for some text; debugger for happy debugging.

这篇关于Chrome.tabs.executeScript - 制表符未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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