点击popup.html(铬扩展名)后执行脚本 [英] Execute script after click in popup.html (chrome extension)

查看:106
本文介绍了点击popup.html(铬扩展名)后执行脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击 popup.html 中的一个按钮时,我试图在页面上执行javascript。我尝试使用这种方式:在$ strong> background.js 中:

 chrome.tabs.onUpdated.addListener(function(tabId,changeInfo){
if(changeInfo.status ==loading){
insert(tabId);
}
});
函数insert(tabId){
chrome.tabs.get(tabId,function(tab){
$('button')。click(function(){
chrome。 tabs.executeScript(tab.id,{file:'js / alert.js'});
});
});
}

Alert.js只包含一个字符串: alert ('works');



Alert只是一个例子。真正的脚本应该在用户点击一个按钮弹出popup.html后用打开的标签进行一些DOM操作。 我写了一个演示为您的需要。



https://gist.github.com/greatghoul / 8120275


I am trying to execute javascript on a page when I click on a button in popup.html. I tried to use such a way:

In background.js:

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo){
    if(changeInfo.status == "loading") {
        insert(tabId);
    }
});
function insert(tabId) {
    chrome.tabs.get(tabId, function(tab) {
        $('button').click(function() {
            chrome.tabs.executeScript(tab.id, {file: 'js/alert.js'});
        });
    });
}

Alert.js consists only of one string: alert('works');

Alert is just an example. Real script should do some DOM manipulation with opened tab after user clicks on a button im popup.html.

解决方案

I wrote a demo for your need.

https://gist.github.com/greatghoul/8120275

这篇关于点击popup.html(铬扩展名)后执行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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