从Chrome扩展程序中打开新标签页以在Google中进行搜索

时间:2017-07-15 19:27:53

标签: javascript google-chrome google-chrome-extension

我们的想法是制作一个简单的Chrome扩展程序,以便在特定网站上进行Google搜索,而不是在Google搜索字段中编写网站:what.whatever。 这是我的popup.html:

<html>

<head>
    <title>HTP Searcher Extension's Popup</title>
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <form action="https://www.google.com/search" method="get" target="_blank" onsubmit="companiesSearch()">
        <input type="text" id="finalquery" name="q" value="" style="display:none;">
        <input type="text" id="site" name="othername" value=" site:www.park.by" style="display:none;">
        <input type="text" id="entertext" name="entertext">
        <input type="submit" class="sendsubmit" name="submit">
    </form>
</body>

</html>

popup.js:

function companiesSearch() {
    var value1 = document.getElementById('entertext').value;
    var value2 = document.getElementById('site').value;
    var value3 = value1 + value2;
    document.getElementById('finalquery').setAttribute('value', value3);
}

如果在上面的构造中制作一个简单的html文件+ js文件,一切都很顺利。如果要使其成为扩展,它只能在不添加javascript的情况下工作:如果要创建一个简单的输入表单,它会打开一个新选项卡并显示搜索结果。但是在添加脚本之后,在提交后,搜索请求会在网址行中闪烁,而不会显示空的Google搜索。在控制台中可以看到这样的错误:

Uncaught (in promise) TypeError: Request scheme 'chrome-extension' is unsupported
at newtab-serviceworker.js:61
at <anonymous>

顺便说一句,我的manifest.json:

{
    "manifest_version": 2,
    "name": "HTP Searcher",
    "description": "This extension searches companies",
    "version": "1.0",
    "browser_action": {
        "default_icon": "icon.png",
        "default_popup": "popup.html"
    },
    "background": {
        "scripts": ["popup.js"]
    },
    "permissions": [
        "activeTab",
        "tabs",
        "https://ajax.googleapis.com/"
    ]
}

提前感谢大家,对编写扩展程序全新。

0 个答案:

没有答案
相关问题