我正在尝试为 Google Chrome 制作一个非常基本的 javascript 扩展。我想基本上从当前页面中获取标题/url/元描述。document.*
我为此使用javascript 。
从理论上讲,它有效,但它没有做我想要的。在扩展中加载时,它只显示popup.html
页面的信息,而不是用户当前页面的信息。
我该怎么做呢?
我发现了一些相关的问题:Google Chrome extensions document.title not working。我想我需要添加某种chrome.*
功能?
这是我的代码:
popup.js
var title = document.title;
if (title.length > 69) {
var title = title.substring(0,69);
var title = title+" <strong>...</strong>";
}
var url = document.URL;
var desc = document.getElementsByName('description')[0].getAttribute('content');
if (desc.length > 156) {
var desc = desc.substring(0,156);
var desc = desc+" <strong>...</strong>";
}
document.getElementById('link').innerHTML = title;
document.getElementById('url').innerHTML = url;
document.getElementById('meta').innerHTML = desc;
popup.html
<!doctype html>
<html>
<head>
<title>Preview</title>
<meta name="description" content="Preview" />
<style>
body {
background-color: #ffffff;
margin:0px;
padding:0px;
font-family:arial,sans-serif;
}
#result { line-height: 1.2; width: 584px;}
.r { margin:0px; padding:0px;}
.r a { color:#2518b5; font-size:medium; font-weight:normal;}
#url { color: #00802a; font-style: normal; }
#meta { line-height:1.24; font-size: small; width:512px;}
</style>
</head>
<body>
<div id="result">
<h3 class="r"><a href="" id="link">{title}</a></h3>
<span id="url">{url}</span><br />
<span class="st" id="meta">{meta}</span>
</div>
<script src="popup.js"></script>
</body>
</html>
清单.json
{
"name": "Description here",
"version": "1.0",
"permissions": [
"tabs", "http://*/*", "https://*/*"
],
"browser_action": {
"default_title": "Preview",
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"manifest_version": 2
}