在 chrome 扩展弹出窗口 window 中保存当前选项卡 link
Save current tab link inside of chrome extension popup window
我正在尝试创建一个 chrome 扩展程序,单击该扩展程序时,会打开一个弹出窗口 - window,其中 window 是一个按钮。单击该按钮时,弹出窗口的 window 应更新为当前选项卡的 link。
See this picture, this is my window popup
但是,当我点击按钮时,没有任何反应...
这是我目前所拥有的:
manifest.json:
{
"manifest_version": 2,
"name": "Save",
"description": "Save tab link.",
"version": "0.1",
"browser_action": {
"default_icon": "/img/icon.png",
"default_popup": "popup.html",
"default_title": "See your saved websites!"
},
"permissions": [
"tabs"
]
}
popup.html:
<html>
<head>
<title>Your articles</title>
<link href="/css/style.css" rel="stylesheet"/>
<script src="popup.js"></script>
</head>
<body>
<div id="div">No content yet! Click the button to add the link of the current website!</div>
<br/>
<button id="button">Add link!</button>
</body>
</html>
最后,popup.js:
// event listener for the button inside popup window
document.addEventListener('DOMContentLoaded', function() {
var button = document.getElementById('button');
button.addEventListener('click', function() {
addURL();
});
});
// add the URL inside the popup-window's <div>
function addURL() {
// store info in the the queryInfo object as per: https://developer.chrome.com/extensions/tabs#method-query
var queryInfo = {
currentWindow: true,
active: true
};
chrome.tabs.query(queryInfo, function(tabs) {
// tabs is an array so fetch the first (and only) object-elemnt in tab
// put URL propery of tab in another variable as per: https://developer.chrome.com/extensions/tabs#type-Tab
var url = tabs[0].url;
// put the content into the popup-window's <div>
document.getElementById("div").innerHTML(url);
});
}
我希望有人能帮助我 :)
替换
document.getElementById("div").innerHTML(url);
和
document.getElementById("div").innerHTML = url;
我正在尝试创建一个 chrome 扩展程序,单击该扩展程序时,会打开一个弹出窗口 - window,其中 window 是一个按钮。单击该按钮时,弹出窗口的 window 应更新为当前选项卡的 link。
See this picture, this is my window popup
但是,当我点击按钮时,没有任何反应... 这是我目前所拥有的:
manifest.json:
{
"manifest_version": 2,
"name": "Save",
"description": "Save tab link.",
"version": "0.1",
"browser_action": {
"default_icon": "/img/icon.png",
"default_popup": "popup.html",
"default_title": "See your saved websites!"
},
"permissions": [
"tabs"
]
}
popup.html:
<html>
<head>
<title>Your articles</title>
<link href="/css/style.css" rel="stylesheet"/>
<script src="popup.js"></script>
</head>
<body>
<div id="div">No content yet! Click the button to add the link of the current website!</div>
<br/>
<button id="button">Add link!</button>
</body>
</html>
最后,popup.js:
// event listener for the button inside popup window
document.addEventListener('DOMContentLoaded', function() {
var button = document.getElementById('button');
button.addEventListener('click', function() {
addURL();
});
});
// add the URL inside the popup-window's <div>
function addURL() {
// store info in the the queryInfo object as per: https://developer.chrome.com/extensions/tabs#method-query
var queryInfo = {
currentWindow: true,
active: true
};
chrome.tabs.query(queryInfo, function(tabs) {
// tabs is an array so fetch the first (and only) object-elemnt in tab
// put URL propery of tab in another variable as per: https://developer.chrome.com/extensions/tabs#type-Tab
var url = tabs[0].url;
// put the content into the popup-window's <div>
document.getElementById("div").innerHTML(url);
});
}
我希望有人能帮助我 :)
替换
document.getElementById("div").innerHTML(url);
和
document.getElementById("div").innerHTML = url;