简体   繁体   English

扩展:Chrome.tabs.query 不适用于“url”

[英]Extension:Chrome.tabs.query does not work with 'url'

My extension to be click button on another page.我的扩展是点击另一个页面上的按钮。 I do not know why the code does not work.我不知道为什么代码不起作用。 The function does not click on the item 'ac_play' function 没有点击'ac_play'项

manifest显现

{
    "manifest_version": 2,

    "name": "VkPlayer", // Название расширения
    "version": "1.0", // Номер версии
    "description": "Switching music in the VK with the keyboard", // Описание расширения
    "permissions": [
        "tabs",
        "*://vk.com/audios*"
    ],
    "background": {
    "scripts": ["index.js"]
   },
    "browser_action": {
    "default_title": "VKPlayer", // Название кнопки
    "default_icon": "1.png", // Иконка для кнопки
    "default_popup": "popup.html" // Всплывающее окно
    }
}

popup.html弹出窗口.html

<script src="index.js"></script>
<style>body{
    background: #2f4d9a;
    width: 0;
    height:0;
}</style>

index.js索引.js

function (tab) {
    chrome.tabs.query({'active': true},{'url':"http://vk.com/audio*"},function (tabs){
        console.log(tabs);
        chrome.tabs.executeScript(tabs[0].id, {code : "document.getElementById('ac_play').onclick();"});
    })
}();

This code my website:此代码我的网站:

  <div id="ac_controls" class="fl_l">
    <div id="ac_play" class="fl_l"></div>
    <div class="next_prev fl_l">
      <div id="ac_prev" class="ctrl_wrap">
        <div class="prev ctrl"></div>
      </div>
      <div id="ac_next" class="ctrl_wrap">
        <div class="next ctrl"></div>
      </div>
    </div>
  </div>
chrome.tabs.query(
  {'active': true}, {'url':"http://vk.com/audio*"},
  function (tabs){/*...*/}
)

is an invalid query.是无效查询。 I have no idea what the logic is supposed to be, but..我不知道逻辑应该是什么,但是..

  1. If you wanted AND between those conditions, you need to make a query with them both: {'active': true, 'url': "http://vk.com/audio*"}如果你想在这些条件之间进行 AND,则需要同时对它们进行查询: {'active': true, 'url': "http://vk.com/audio*"}
  2. If you wanted OR between those conditions, you'll need to query for one and then filter manually for another.如果你想在这些条件之间进行 OR,则需要查询一个,然后手动过滤另一个。

     chrome.tabs.query( {'url':"http://vk.com/audio*"}, function (tabs) { tabs.forEach(function(tab) { if(tab.active) { /*... */ } }); } });

Most probably this is not the only problem with your code, but unfortunately you fail to explain in English what you need.很可能这不是您的代码的唯一问题,但不幸的是您无法用英语解释您需要什么。 ru.SO may be more adapted for your needs. ru.SO可能更适合您的需求。

In addition to the accepted answer: I had to add the "tabs" permission to my manifest to make the "url" query work.除了已接受的答案之外:我必须向我的清单添加“tabs”权限才能使“url”查询正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM