且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Google Chrome扩展程序 - 需要帮助

更新时间:2023-12-05 20:18:46

在Chrome 19.0.1084.1中进行测试,这对我很有帮助....


$ b

popup.html



$ p $ <!doctype html>
< html>
< head>
< script src = jquery-1.7.1.min.js>< / script>
< script src =popup.js>< / script>
< / head>
< body>
< div id =extension_container>
< div id =header>
< p id =intro> ; / p>
< / div><! - END header - > $ p
< / div>按钮>按钮> ;
< div id =content>
< / div><! - END content - >
< / div>
< / body>
< / html>

popup.js

  $(document) .ready(function(){
$(#button)。click(function(){
$(#intro)。text(Hello,im added);
alert(点击);
});
});

可能的错误

我认为可能是什么你的问题是你的清单中有manifest_version:2,并且你的popup.html看起来像这样....

 <!doctype html> 
< html>
< head>
< script src =jquery-1.7.1.min.js>< / script>
<! - script src =popup.js>< / script - >
< script>
$(document).ready(function(){
$(#button)。click(function(){
$(#intro)。text(Hello, im添加);
alert(Clicked);
});
});
< / script>
< / head>
< body>
< div id =extension_container>
< div id =header>
< p id =intro>结果在这里< / p>
< button type =buttonid =button> Click Me!< / button>
< / div> <! - END标题 - >
< div id =content>
< / div> <! - END内容 - >
< / div>
< / body>
< / html>

...这不起作用,因为内联脚本不允许在清单版本2中使用。


Im new on Google Chrome Extensions coding, and i have some basic questions.

I want to make a Chrome Extension, and the scheme is the following:

-a popup window, containing buttons and result fields (popup.html)

-when a button is clicked, i want to trigger an event, this event should connect to a webserver (i make the servlet too), and gather information from the server. (XMLHttpRequest())

-after that, i want my extension to load the gathered information into one of the result fields.

Simple, isn't it? But i have several problems, right at the beginning:( I started developing with reading tutorials, but i have fog on the main structure of an extension. Now, i started an app, containing a popup.html, manifest.json ... In popup.html theres a result field, and a button

<div id="extension_container">
<div id="header">
    <p id="intro">Result here</p>
    <button type="button" id="button">Click Me!</button>
</div> <!-- END header -->
<div id="content">

</div> <!-- END content -->

When button is clicked, i trigger an event, handeled with jquery, code here:

<script>
    $(document).ready(function(){
        $("#button").click(function(){
            $("#intro").text("Hello, im added");
            alert("Clicked");
        });
    });
</script>

And here comes the problem, in popup.html this doesnt work, if i load it to Chrome, nothing happens. Otherwise, if i open popup.html in browser, not as an extension, everything works fine. So, i think i have basic misunderstandings on extension structures, starting with background pages, background javascript and so on.. :( Could anyone help me?

Testing in Chrome 19.0.1084.1 this worked for me....

popup.html

<!doctype html>
<html>
<head>
    <script src="jquery-1.7.1.min.js"></script>
    <script src="popup.js"></script>
</head>
<body>
    <div id="extension_container">
        <div id="header">
            <p id="intro">Result here</p>
            <button type="button" id="button">Click Me!</button>
        </div> <!-- END header -->
        <div id="content">
        </div> <!-- END content -->
    </div>
</body>
</html>

popup.js

$(document).ready(function() {
    $("#button").click(function() {
        $("#intro").text("Hello, im added");
        alert("Clicked");
    });
});

Possible Error
I think what MIGHT be your problem is that you have "manifest_version" : 2 in your manifest and your popup.html looks something like this....

 <!doctype html>
<html>
<head>
    <script src="jquery-1.7.1.min.js"></script>
    <!--script src="popup.js"></script-->
    <script>
    $(document).ready(function() {
        $("#button").click(function() {
            $("#intro").text("Hello, im added");
            alert("Clicked");
        });
    });
    </script>
</head>
<body>
    <div id="extension_container">
        <div id="header">
            <p id="intro">Result here</p>
            <button type="button" id="button">Click Me!</button>
        </div> <!-- END header -->
        <div id="content">
        </div> <!-- END content -->
     </div>  
</body>
</html>

...this wouldnt work because inline scripts arent allowed in manifest version 2.