Javascript 在弹出窗口中显示当前URL-Safari扩展
我正在尝试创建一个非常基本的Safari扩展,一旦单击工具栏图标,它就会在弹出窗口中显示当前选项卡的URL 到目前为止,我有以下代码: global.html:Javascript 在弹出窗口中显示当前URL-Safari扩展,javascript,html,safari-extension,Javascript,Html,Safari Extension,我正在尝试创建一个非常基本的Safari扩展,一旦单击工具栏图标,它就会在弹出窗口中显示当前选项卡的URL 到目前为止,我有以下代码: global.html: <!doctype html> <html> <head> <script src="global.js"></script> </head> <body> </body> </html> funct
<!doctype html>
<html>
<head>
<script src="global.js"></script>
</head>
<body>
</body>
</html>
function getCurrentURL() {
return safari.application.activeBrowserWindow.activeTab.url;
}
<!doctype html>
<html>
<head>
<script src="popover.js"></script>
</head>
<body>
<div class="bodyContainer" id="status"></div>
</body>
</html>
popover.html:
<!doctype html>
<html>
<head>
<script src="global.js"></script>
</head>
<body>
</body>
</html>
function getCurrentURL() {
return safari.application.activeBrowserWindow.activeTab.url;
}
<!doctype html>
<html>
<head>
<script src="popover.js"></script>
</head>
<body>
<div class="bodyContainer" id="status"></div>
</body>
</html>
我想要的是,当我点击popover图标时,我希望popover显示“Hello world.Link is”
实际发生的情况是,当我在控制台中单击popover图标时,我看到我得到了正确的URL,但是,我得到了错误:
[Error] TypeError: null is not an object (evaluating 'body.innerHTML = "Hello world. Link is " + link')modifyBody (popover.js:9)
扩展的想法是,当我单击popover时,它需要:
global.js
以捕获页面的URLglobal.js的响应
popover.html
global.js
以捕获页面的URLmodifyBody()
(因为我们还没有收到global.js的响应)。所以这一页还是空白的global.js
的响应-但现在已经太晚了global.js的响应,请使用承诺等待更新popover.html
popoverHandler
,并将其添加到事件侦听器中,如下所示:
var popoverHandler = function(event) {
console.debug("Opening popup. Identifier");
var currentURL = safari.extension.globalPage.contentWindow.getURL();
// update my page.html here.
};
safari.application.addEventListener(“popover”,PopoOverHandler,true)代码>
var popoverHandler = function(event) {
console.debug("Opening popup. Identifier");
var currentURL = safari.extension.globalPage.contentWindow.getURL();
// update my page.html here.
};
您还可以在global.js
中注册“激活”
、“打开”
或“导航”
处理程序,然后在触发这些处理程序后调用popover.js
更新html函数
希望这有帮助。您试图从全局页面中访问实际的页面上下文–我想Safari会阻止您这样做。您可以从条形图和弹出框访问全局页面,但不能从实际页面访问
改为使用消息传递,请参见扩展教程中的。您需要两个简单的函数来询问URL并返回其值。不,不是这样。我只更新了popover.js中的popover.html,但它不起作用。我发现了这个问题,我正在编辑我的问题来添加它。好的,听起来不错。不过,您也可以使用消息传递——如果您需要同时将代码注入到实际页面中,您可能不得不这样做。谢谢你的更新。