【问题标题】:In a Chrome extension, how do I get the URL of the website with browser_action (popup.html)?在 Chrome 扩展程序中,如何使用 browser_action (popup.html) 获取网站的 URL?
【发布时间】:2013-04-02 14:44:22
【问题描述】:

我正在使用以下代码创建一个 Chrome 扩展程序。

文件 manifest.json

{
"name": "Project",
"version": "1.0.0",
"manifest_version": 2,
"description": "Popup when website requires Log in",
"browser_action":{
    "default_icon":"icon_19.png",
    "default_popup":"Popup.html"
 }
}

文件 Popup.html

<html>
<head></head>
<body>
<div class="plus" id="plu"></div>
<script src="inline.js"></script>
</body>
</html>

inline.js:

window.onload = function() {
    document.getElementById('plu').onclick=popup;
}
function popup() {
    var link = document.URL;
    alert("This is the link: ( " +link+ " )");
}

当我单击 ID 为“plu”的 div 时,它会获取文件 popup.html 的 URL,但不会获取网站的 URL。

【问题讨论】:

标签: javascript google-chrome google-chrome-extension popup browser-action


【解决方案1】:

此时的文档对象指的是扩展程序的弹出窗口,因为您将脚本包含在 popup.html 中。

如果您想访问标签页的 DOM,您需要使用内容脚本。看看:

http://developer.chrome.com/extensions/content_scripts.html

【讨论】:

    猜你喜欢
    • 2020-04-19
    • 1970-01-01
    • 1970-01-01
    • 2022-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-21
    • 2012-05-26
    相关资源
    最近更新 更多