我认为这种方式比@Chris的方式更好。
document.location.href = url;
更新:我测试了它,并在我的chrome上成功。虽然这种方式也很慢,但它比@Chris的方式更快。
这是明显的。JSON
{
"name" : "my extension",
"description" : "my extension",
"version" : "0.1",
"manifest_version" : 2,
"chrome_url_overrides" : {
"newtab" : "html/index.html#newTab"
},
"permissions" : [
"tabs",
"<all_urls>",
"history",
"bookmarks",
"chrome://favicon/*",
"unlimitedStorage",
"management",
"clipboardWrite",
"clipboardRead",
"contextMenus",
"notifications",
"storage"
],
"browser_action" : {
"default_icon" : "icon.png",
"default_popup" : "html/popup.html",
"default_title": "Click here!"
},
"options_page": "html/options.html"
}
这是index.html的
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="/js/newTabCheck.js"></script>
<title></title>
</head>
<body></body>
</html>
这是newTabCheck.js
if (window.location.hash == '#newTab') {
window.document.title = 'Loading...';
chrome.storage.sync.get({
file_url : 'http://www.google.com'
}, function (items) {
document.location.href = items.file_url;
});
}
这是options.html
<html>
<head>
<title>My Test Extension Options</title>
</head>
<body>file path:
<form>
<input type="text" id="file_url" />
<br />
<button id="save">save</button>
<label id="status"></label>
<script type="text/javascript" src="/js/options.js"></script></form>
</body>
</html>
这是运tions.js
// Saves options to chrome.storage
function save_options() {
var file_url = document.getElementById("file_url").value;
chrome.storage.sync.set({
file_url : file_url
}, function() {
// Update status to let user know options were saved.
var status = document.getElementById('status');
status.textContent = 'save success!';
setTimeout(function() {
status.textContent = '';
}, 750);
});
}
// Restores select box and checkbox state using the preferences
// stored in chrome.storage.
function restore_options() {
chrome.storage.sync.get({
file_url : 'http://www.google.com'
}, function (items) {
document.getElementById("file_url").value = items.file_url;
});
}
document.addEventListener('DOMContentLoaded', restore_options);
document.getElementById('save').addEventListener('click', save_options);
只需设置FILE_URL为focus.html
<html>
<head><title></title></head>
<body>
save path:
<input type="text" id="file_url"></input>
<script type="text/javascript">
document.getElementById('file_url').focus();
</script>
</body>
</html>
这就是全部。
写这个扩展的原因是扩展Vimium。然后,当我输入“t”打开一个新标签时,我可以使用vimiun而不用鼠标。因此,您不必编写javascript代码document.getElementById('file_url').focus();
。
非常完美!非常感谢!我甚至不知道chrome.commands。这看起来非常有用! – GreysonP
此外,Fauxbar看起来非常酷:) – GreysonP
只是注意,你最终会具有地址栏显示是这样的: “铬扩展:// /html/page.html” 这不会是空的之前 –
sg88