Crossrider扩展平台
Crossrider Extension Platform
我正在使用Crossrider创建一个扩展,允许用户将他们正在查看的页面添加书签。
为此,我创建了一个弹出按钮,当单击该按钮时,将打开用于管理书签列表的UI。当用户单击扩展的按钮,我要通过正在查看的弹出页面的URL,以便我可以将其添加到书签列表。我的问题是,我不知道如何通过URL的弹出。有人能给我指个方向吗?
下面的代码片段是该代码的简化版本,用于演示我所拥有的内容:background.js:
appAPI.ready(function($) {
appAPI.browserAction.setResourceIcon('images/icon.png');
appAPI.browserAction.setPopup({
resourcePath:'html/popup.html',
height: 300,
width: 300
});
});
popup.html:
<!DOCTYPE html>
<html>
<head>
<!-- This meta tag is relevant only for IE -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<script type="text/javascript">
function crossriderMain($) {
}
</script>
</head>
<body>
<h1>Bookmark List</h1>
<ul>
<li>1: http://example.com/1.html</html>
<li>2: http://example.com/2.html</html>
</ul>
</body>
</html>
这里的问题是范围。弹出窗口运行的范围没有访问正在查看的页面的URL;因此,要将URL获取到弹出范围,弹出代码必须通过消息传递从另一个范围请求信息。
最简单的方法是让弹出窗口发送一条消息到活动选项卡(扩展页面范围),请求它正在显示的页面的URL。您可以按照以下方式实现这一点,我将让您自己编写代码,将书签添加到列表中。
extension.js :
appAPI.ready(function($) {
// Listener to receive messages
appAPI.message.addListener(function(msg) {
// check if message is requesting page url and respond accordingly
if (msg.type==='get-url')
appAPI.message.toPopup({
url:encodeURIComponent(window.location.href);
});
});
// The rest of your code
...
});
popup.html :
...
function crossriderMain($) {
// Listener to receive messages
appAPI.message.addListener(function(msg) {
// check if message contains a url and call function to process the url
if (msg.url) addBookmark(msg.url);
});
// Request url from active tab
appAPI.message.toActiveTab({
type: 'get-url';
});
function addBookmark(url) {
// Add your code that handles adding the url to the bookmark list
}
}
...
[披露:我是Crossrider员工]
相关文章:
- Node.js v6.2.0类扩展不是函数错误
- 扩展移相器按钮类不工作
- chrome扩展中的navigator.geolocation.getCurrentPosition
- chrome扩展:尽管运行了at:documentidle,js脚本还是过早启动
- chrome扩展更改主机/域警告
- TableExport jquery插件:文件名和扩展名问题
- 如何在chrome扩展中重定向到html页面
- Chrome扩展没有't在重新加载之前考虑期权价值
- 扩展SVGTextElement时出现Typescript Uncaught TypeError
- 如何通过自己获得Chrome扩展的用户反馈/错误报告
- Javascript”;类“;带有参数的扩展
- 试图阻止Chrome通过扩展关闭
- 如何在chrome扩展中存储数据/结果,以及如何使用setTimeout使其只被调用一次
- 表达式引擎扩展开发-向呈现的条目添加自定义javascript
- 从选项页面更新chrome扩展清单权限
- 如何将chrome扩展功能移植到移动设备(特别是jquery和trello)
- Chrome扩展:遍历不同的页面并收集数据
- 使用API平台打开Chrome扩展
- Crossrider扩展平台
- 解析平台-调用JavaScript函数定义在一个扩展字符串对象得到错误对象没有方法