2

嗨我想創建一個擴展chrome,爲了使我的應用擴展我正在按照這個步驟,在Chrome擴展中使用什麼來代替window.location.href?

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"] 
     } 
    } 
} 

background.js

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

同時還包括對標籤我得到個e-以下警告權限,

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. 

我的應用程序是不考慮作爲一個擴展ered。而我正在嘗試這個頁面導航。我通常在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); 
}); 

這個代碼snippent也沒有working.Can有人請幫助我使我的應用程序擴展,並幫助我在頁面導航。提前感謝。

+0

嘗試'祿ation.replace()'function – claudios

+0

我仍然遇到這個錯誤。無法打開同窗鏈接到「chrome-extension://hnbkidfmkmjfoafmcmficmejfcfdjghc/service1.html」;嘗試目標=「_空白」。 @claudios – Anu

+0

首先從清單中刪除**應用程序**,以便Chrome不會將其視爲應用程序(應用程序包裝,離開背景......)。然後你*選項卡*權限將工作 –

回答

0

建議查看Offical Guide for extensions,以下是基於您的需求的基本示例,當瀏覽器啓動時(您的擴展程序也會執行),它會打開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"] 
    } 
} 

background.js

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

的login.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> 

login.js

document.addEventListener("DOMContentLoaded", function() { 
    document.getElementById("click-me").addEventListener("click", function() { 
     window.location.href = "sample.html";  
    }, false); 
}); 
+0

login.js沒有被調用@Haibara Ai – Anu