简体   繁体   English

打开和关闭Chrome扩展程序

[英]Opening and closing chrome extension

It seems to be the simplest task, but I'm having a hard time with it... I want to open my extension by clicking the extension icon, not every time a new tab opens. 这似乎是最简单的任务,但是我很难过...我想通过单击扩展图标来打开扩展,而不是每次打开新选项卡时。 I also want to close it fully when the icon is pressed. 我也想在按下图标时将其完全关闭。 I insert an iframe into the website's DOM, it's not a standard popup. 我将iframe插入网站的DOM中,这不是标准的弹出窗口。 Right now I'm hiding it, whenever the icon is clicked, but it still inserts the iframe whenever a new page/tab is loaded. 现在,无论何时单击图标,我都将其隐藏,但只要加载新页面/标签,它仍会插入iframe。 I tried removing the iframe, which works, but triggers the content_script every time I re-insert it, re-scraping the website and adding to the array, which makes the TTS repeat words for every time that the extension has been reopened. 我尝试删除有效的iframe,但是每次重新插入iframe时都会触发content_script,重新刮擦网站并添加到数组中,这会使TTS在每次重新打开扩展名时都重复单词。

Manifest: 表现:

  {
  "manifest_version": 2,
  "name": "Caliban",
  "description": "Chrome extension that reads text aloud",
  "version": "1.0",
  "permissions": [
    "tabs",
    "storage",
    "<all_urls>",
    "tts"
  ],
  "content_scripts": [
    {
      "matches": [
        "<all_urls>"
      ],
      "js": [
        "js/translations.js",
        "js/enums.js",
        "js/element-wrapper.js",
        "js/content_script.js"
      ],
      "css": [
        "css/parent.css"
      ]
    }
  ],
  "background": {
    "scripts": [
      "js/translations.js",
      "js/enums.js",
      "js/element-wrapper.js",
      "js/background.js"
    ]
  },
  "browser_action": {
    "default_icon": "img/icon.png",
    "default_title": "Caliban"
  },
  "web_accessible_resources": [
    "css/*.css",
    "images/*.png",
    "images/*.svg",
    "popup.html"
  ]
}

content_script.js: content_script.js:

function createIFrame()
{
    iframe.style.position = "fixed";
    iframe.style.height = "300px";
    iframe.style.top = "0px";
    iframe.style.right = "0px";
    iframe.style.zIndex = "9000000000000000000";
    iframe.frameBorder = "1"; //temporary, so we can see the bounds while developing.
    iframe.overflow = "hidden";
    iframe.id = "CalibanMainUI";
    iframe.src = chrome.extension.getURL("popup.html");

    body.appendChild(iframe);
}

This function is called at the start of content_script.js. 在content_script.js的开头调用此函数。 Also in content_script, I use below function to hide/show, but was used before to create/remove iframe. 同样在content_script中,我使用下面的函数隐藏/显示,但之前曾用于创建/删除iframe。

function toggleIFrame() {
    if (isFrameOpen === true)
    {
        iframe.style.display = "none";
        isFrameOpen = false;
    }
    else
    {
        iframe.style.display = "initial";
        isFrameOpen = true;
    }

What I'm looking for is: 我正在寻找的是:

  1. Open: User-controlled, not new tab-controlled. 打开:用户控制,而不是新的选项卡控制。 Create and insert iframe, scrape website. 创建并插入iframe,抓取网站。
  2. Close: Remove iframe, unload scripts, release variables. 关闭:删除iframe,卸载脚本,释放变量。

Does any such thing exist in boilerplate ? 样板中是否存在这样的东西? Or do I need to manually code these seemingly standard requirements? 还是我需要手动编写这些看似标准的要求?

I'll be happy to provide more code if needed. 如果需要,我很乐意提供更多代码。

Instead of injecting the content scripts through manifest "content_scripts" key, inject them manually through chrome.tabs.executeScript in the background page in response to chrome.browserAction.onClicked . 代替通过清单"content_scripts"键注入内容脚本,而是响应于chrome.browserAction.onClicked通过后台页面中的chrome.tabs.executeScript手动注入内容脚本。

This way you control when to inject them and can also implement the logic to disable them if they were already injected (left as an exercise for the reader :) 这样,您可以控制何时注入它们,还可以实现禁用它们的逻辑(如果已注入它们)(留给读者练习:)

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

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