在chrome扩展中使用什么来代替window.location.href?

问题描述 投票:0回答:1

您好,我正在尝试创建一个 chrome 扩展,为了使我的应用程序扩展,我正在按照以下步骤操作,

1.进入chrome选项->更多工具->扩展。

2.我在该窗口中选择了开发者模式。

3.我选择了解压的扩展程序,然后选择了我的应用程序。

4.我点击了打包扩展。

在我的应用程序中,我有几个 html 页面、CSS、JS 和清单文件和 background.js。

清单文件

{
    "name": "...",
    "description": "........",
    "manifest_version": 2,
    "minimum_chrome_version": "23",
    "version": "0.1.1",
    "offline_enabled": true,
    "icons": {
        "16": "sample.png"  
    },
    "permissions": [
    "storage","tabs","<all_urls>","webview"
    ],
    "app": {
        "background": {
            "scripts": ["background.js"]
        }
    }
}

背景.js

chrome.app.runtime.onLaunched.addListener(function() {
  chrome.app.window.create('login.html', {
    id: 'main',
    bounds: { width: 1024, height: 768 }
  });
});

虽然包含选项卡的权限,但我收到以下警告,

There were warnings when trying to install this extension:
'tabs' is only allowed for extensions and legacy packaged apps, but this is a packaged app.

我的应用程序不被视为扩展程序。我正在尝试使用此功能进行页面导航。 我通常在jquery中使用

window.location.href="sample.html"
。为此,我的 chrome 扩展程序出现错误,

Use blank _target

然后我尝试使用这行代码,

function clickHandler(e){
  chrome.tabs.update({url:"service1.html"});
  window.close();
}
document.addEventListener('DOMContentLoaded',function(){
  document.getElementById('click-me').addEventListener('click',clickHandler);
});

这段代码也不起作用。有人可以帮助我使我的应用程序成为一个扩展并帮助我进行页面导航吗。提前致谢。

jquery google-chrome google-chrome-extension google-chrome-app
1个回答
0
投票

建议看一下官方扩展指南,以下是根据您的需求提供的基本示例,当浏览器启动时(您的扩展程序也被执行),它会打开

login.html
,有一个登录按钮,一旦点击它,
sample.html
就会打开。

manifest.json

{
    "name": "...",
    "description": "........",
    "manifest_version": 2,
    "minimum_chrome_version": "23",
    "version": "0.1.1",
    "icons": {
        "16": "sample.png"  
    },
    "background": {
        "scripts": ["background.js"]
    }
}

背景.js

chrome.windows.create({url: "login.html"});

登录.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <button id="click-me">Click me</button>
    <script src="login.js"></script>
</body>
</html>

登录.js

document.addEventListener("DOMContentLoaded", function() {
    document.getElementById("click-me").addEventListener("click", function() {
        window.location.href = "sample.html";    
    }, false);
});
© www.soinside.com 2019 - 2024. All rights reserved.