简体   繁体   English

Chrome扩展程序-激活popup.html中当前选项卡的脚本

[英]Chrome Extension - Activating a script within the popup.html for the current tab

I'm not sure how to properly ask this question because it's difficult to explain so I will use an example. 我不确定如何正确地问这个问题,因为很难解释,因此我将举一个例子。

What I am trying to do is figure out how to activate something like a bookmarklet using a link inside of the popup.html of a chrome extension that will interact with the current tab. 我正在尝试做的事情是弄清楚如何使用chrome扩展程序的popup.html内的链接激活与小书签类似的东西,该链接将与当前选项卡交互。

For example: If you click on the chrome extension icon, a popup appears (the usual drop-down window). 例如:如果您单击chrome扩展程序图标,则会显示一个弹出窗口(通常为下拉窗口)。 In that window would be say a button that increases the size of images (like a bookmarklet for your bookmarks bar). 在该窗口中会说一个增加图像大小的按钮(例如书签栏的小书签)。

What I want is for that button to interact with the currently opened tab rather than the popup.html page. 我想要的是该按钮与当前打开的选项卡而不是popup.html页面进行交互。

I have the proper permissions (all urls and tab) inside the manifest as well as the scripts being defined. 我在清单以及定义的脚本中具有适当的权限(所有url和选项卡)。 Everything tests out ok there. 一切都测试正常。 But I know that my solution (not working) is probably way off the mark and I may not even be close at this point. 但是我知道我的解决方案(无法正常工作)可能超出了预期范围,并且在这一点上我可能还没有接近。 So I am asking for help here. 所以我在这里寻求帮助。

background.js background.js

function zii() {

    function zoomImage(image, amt) {
        if (image.initialHeight == null) { /* avoid accumulating integer-rounding error */
            image.initialHeight = image.height;
            image.initialWidth = image.width;
            image.scalingFactor = 1;
        }
        image.scalingFactor *= amt;
        image.width = image.scalingFactor * image.initialWidth;
        image.height = image.scalingFactor * image.initialHeight;
    }
    var i, L = document.images.length;
    for (i = 0; i < L; ++i) zoomImage(document.images[i], 2);
    if (!L) alert("This page contains no images."); 

}

popup.html popup.html

<button onClick="zii()">Test</button>

The background function is also linked inside of the popup.html page as well. 后台功能也链接在popup.html页面内部。 The actual popup.html page is very large so I just showed the basics of it here without including all of the relevant code that pertains to other aspects of the extension. 实际的popup.html页面非常大,因此我仅在此处显示了它的基础,而没有包括与扩展的其他方面有关的所有相关代码。

I obviously have no idea what I am doing wrong or if what I am attempting is even possible. 我显然不知道自己在做错什么,甚至尝试做不到。

The Objective Summary: Simply put, I just want to click on the button within the popup.html and increase the size of the images on the currently open tab (in the same way that you would do this with a bookmarklet). 目标摘要:简而言之,我只想单击popup.html中的按钮,并增加当前打开的选项卡上图像的大小(与使用书签的方式相同)。

First I recommend reading about the extensions architecture . 首先,我建议阅读有关扩展体系结构的文章

If you want to inject a .js file or some code in the current window by clicking a button in the popup you can simply do so by using chrome.tabs.executeScript inside your button onclick handler while passing null as the first tabId argument as it defaults to the active tab of the current window. 如果要通过单击弹出窗口中的按钮在当前窗口中注入.js文件或某些代码,只需在按钮onclick处理程序内使用chrome.tabs.executeScript即可,同时将null作为第一个tabId参数传递给它。默认为当前窗口的活动选项卡。

For example: 例如:

chrome.tabs.executeScript(null, {
    code: "console.log('Injected.')"
});

Or: 要么:

chrome.tabs.executeScript(null, {
    file: "content.js"
});

Be sure to also have "activeTab" in your manifest.json permissions. 确保您的manifest.json权限中也包含"activeTab"

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

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