极简天气

先上效果图:

下面我们一步步来~

<!-- more -->

创建文件

新建weather文件夹,里面包含manifest.json,popup.html和images文件夹。images文件夹放16,48,128三种不同尺寸的图标

manifest.json内代码如下:

{
  "manifest_version":2,
  "name":"极简天气",
  "description":"极简天气预报",
  "version":"1.0",
  "icons": {
        "16": "images/sun16.png",
        "48": "images/sun48.png",
        "128": "images/sun128.png"
   },
  "browser_action":{
      "default_icon":"images/sun48.png",
      "default_title":"天气预报",
      "default_popup":"popup.html"
  },

}

popup.html的代码如下:

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>天气</title>
</head>
<body>
 <div class="weather">
    Test
 </div>
</body>
</html>

文件说明

manifest.json

必需文件,是整个扩展的入口,每个Chrome扩展都包含一个Manifest文件。Manifest必须包含name、version和manifest_version属性。

属性说明:

  • manifest_version指定文件格式的版本,在Chrome18之后,应该都是2
  • name扩展名称
  • version 扩展版本号
  • version扩展的版本
  • icons扩展列表图标
  • browser_action指定扩展在Chrome工具栏中的显示信息。
  • default_icondefault_titledefault_popup依次指定图标、标题、对应的页面

Popup页面

Popup页面是当用户点击扩展图标时,展示在图标下面的页面。

打开chrome扩展程序界面,勾选"开发者模式",拖入weather文件夹,然后就可以看到weather扩展已经出现在chrome扩展程序列表了

同时,工具栏也出现了weather的图标,点击之后会弹出popup界面:

完善页面和样式

完善静态popup页面,模拟天气数据:

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>天气</title>
</head>
<body>
    <div class="weather">
        <div class="today" id="today">
            <h1 class="city">厦门</h1>
            <div class="row_detail">
                <h1>19<span>℃</span></h1></div>
            <div class="wind">
                <h2>阴</h2>
                <h4>风速 20   湿度 89%</h4></div>
        </div>
        <div class="content">
            <div class="wrap" id="wrap">
                <div class="row">
                    <h4>2016-05-16</h4>
                    <h1>19~24</h1>
                    <h4>阴</h4>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

新建CSS文件,并在popup页面引入

body{
    width:740px;
    height:400px;
    font-family: 'Microsoft Yahei';
    color:#333;
    background:#fefefe;
    text-shadow:1px 1px 6px #333;
}

.city{
    text-align:center
}
.today{
    padding-bottom:30px;
}
.row_detail{
    display: flex;
    direction: row;
    justify-content:center;
    align-items: center;
}
.row_detail img{
    width:80px;
}
.row_detail h1{
    font-size:60px;
}
.wind{
    text-align: center;
}
.content{
    display: flex;
    direction: column
}

.wrap{
    display: flex;
    direction: row;
    flex: 1;
    justify-content:space-around;
    align-items: center;
}
.row{
    background:#fff;
    border:1px solid #ccc;
    padding:10px;
    box-shadow: 0 2px 10px rgba(0,0,0,.3);
}
.row img{
    width:80px;
}
.row h1{
    font-size:18px;
}
h1,h4{
    text-align: center;
    margin:0;
}

点击工具栏weather图标,此时界面如图:

获取真实天气数据

Google允许Chrome扩展应用不必受限于跨域限制。但出于安全考虑,需要在Manifest的permissions属性中声明需要跨域的权限。
这里以和风天气API为例.
首先,在Manifest里添加要请求的API接口:

"permissions":[
     "http://api.openweathermap.org/data/2.5/forecast?q=*",
  ]

然后新建popup.js并在popup页面中引入
简单的ajax函数:

function httpRequest(url,callback) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET',url,true);
    xhr.onreadystatechange = function() {
        if(xhr.readyState === 4){
            callback(xhr.responseText);
        }
    }
    xhr.send();
}

和风天气API可以通过城市名称,城市代码,IP三种方式来获取指定城市天气数据,不过经过测试发现,IP获取的方式不准确,城市有偏差,所以,直接用城市名称来获取。这里借用http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json获取城市名称。

httpRequest('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json',function(data) {
    if(!data) return;
    data = JSON.parse(data);
    var city = data.city;
    var url = 'https://api.heweather.com/x3/weather?city='+city+'&key=youkey';
        httpRequest(url,function(data) {
            data = JSON.parse(data);
            var result = data["HeWeather data service 3.0"][0];
            showWeather(city,result);
        });
});

为了方便的解析图片,构建一个json

var cond_info = {
100:"http://files.heweather.com/cond_icon/100.png",
101:"http://files.heweather.com/cond_icon/101.png",
102:"http://files.heweather.com/cond_icon/102.png",
103:"http://files.heweather.com/cond_icon/103.png",
104:"http://files.heweather.com/cond_icon/104.png",
200:"http://files.heweather.com/cond_icon/200.png",
201:"http://files.heweather.com/cond_icon/201.png",
202:"http://files.heweather.com/cond_icon/202.png",
203:"http://files.heweather.com/cond_icon/203.png",
204:"http://files.heweather.com/cond_icon/204.png",
205:"http://files.heweather.com/cond_icon/205.png",
206:"http://files.heweather.com/cond_icon/206.png",
207:"http://files.heweather.com/cond_icon/207.png",
208:"http://files.heweather.com/cond_icon/208.png",
209:"http://files.heweather.com/cond_icon/209.png",
210:"http://files.heweather.com/cond_icon/210.png",
211:"http://files.heweather.com/cond_icon/211.png",
212:"http://files.heweather.com/cond_icon/212.png",
213:"http://files.heweather.com/cond_icon/213.png",
300:"http://files.heweather.com/cond_icon/300.png",
301:"http://files.heweather.com/cond_icon/301.png",
302:"http://files.heweather.com/cond_icon/302.png",
303:"http://files.heweather.com/cond_icon/303.png",
304:"http://files.heweather.com/cond_icon/304.png",
305:"http://files.heweather.com/cond_icon/305.png",
306:"http://files.heweather.com/cond_icon/306.png",
307:"http://files.heweather.com/cond_icon/307.png",
308:"http://files.heweather.com/cond_icon/308.png",
309:"http://files.heweather.com/cond_icon/309.png",
310:"http://files.heweather.com/cond_icon/310.png",
311:"http://files.heweather.com/cond_icon/311.png",
312:"http://files.heweather.com/cond_icon/312.png",
313:"http://files.heweather.com/cond_icon/313.png",
400:"http://files.heweather.com/cond_icon/400.png",
401:"http://files.heweather.com/cond_icon/401.png",
402:"http://files.heweather.com/cond_icon/402.png",
403:"http://files.heweather.com/cond_icon/403.png",
404:"http://files.heweather.com/cond_icon/404.png",
405:"http://files.heweather.com/cond_icon/405.png",
406:"http://files.heweather.com/cond_icon/406.png",
407:"http://files.heweather.com/cond_icon/407.png",
500:"http://files.heweather.com/cond_icon/500.png",
501:"http://files.heweather.com/cond_icon/501.png",
502:"http://files.heweather.com/cond_icon/502.png",
503:"http://files.heweather.com/cond_icon/503.png",
504:"http://files.heweather.com/cond_icon/504.png",
506:"http://files.heweather.com/cond_icon/506.png",
507:"http://files.heweather.com/cond_icon/507.png",
508:"http://files.heweather.com/cond_icon/508.png",
900:"http://files.heweather.com/cond_icon/900.png",
901:"http://files.heweather.com/cond_icon/901.png",
999:"http://files.heweather.com/cond_icon/999.png"
}

showWeather()函数构建DOM;

function showWeather(city,result) {
    var daily = result.daily_forecast;
    var now = result.now;
    var dailyDom='';
    for(var i=0;i<daily.length;i++){
        var day = daily[i];
         dailyDom += '<div class="row">'
            +'<h4>'+day.date+'</h4>'
            +'[站外图片上传中……(3)]'
            +'<h1>'+day.tmp.min+'~'+day.tmp.max+'</h1>'
            +'<h4>'+day.cond.txt_d+'</h4>'
        +'</div>'
    }
    var today = '<h1 class="city">'+city+'</h1>'
                +'<div class="row_detail">'
                    +'[站外图片上传中……(4)]'
                    +'<h1>'+now.tmp+'<span>℃</span></h1>'
                +'</div>'
                +'<div class="wind">'
                    +'<h2>'+now.cond.txt+'</h2>'
                    +'<h4>风速 '+now.wind.spd+'   湿度 '+now.hum+'%</h4>'
                +'</div>'

    document.getElementById('today').innerHTML = today;
    document.getElementById('wrap').innerHTML = dailyDom;
}

这时,再点击weather图标,天气扩展基本上就完成了,不过因为和风API有请求次数限制,也为了减少请求,这里做一下数据缓存。

var _time = new Date().getTime()-(60601000*2); //接口次数有限,两小时请求一次
var storageTime = localStorage.updateTime||0;

httpRequest('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json',function(data) {
    if(!data) return;
    data = JSON.parse(data);
    var city = data.city;
    var url = 'https://api.heweather.com/x3/weather?city='+city+'&key=youkey';
    if(_time>storageTime){
        httpRequest(url,function(data) {
            data = JSON.parse(data);
            var result = data["HeWeather data service 3.0"][0];
            showWeather(city,result);
            localStorage.updateTime = new Date().getTime();
            localStorage.data = JSON.stringify(result);
        });
    }else{
        var result = JSON.parse(localStorage.data);
        showWeather(city,result);
    }

});

至此,一个简单的chrome天气扩展就完成了,是不是比想象中更简单?

github源码 https://github.com/yohnz/weather

如果觉得本文有帮助,请github赏个star~

时间: 2022-12-07

极简天气的相关文章

移动开发中的极简设计

本文讲的是移动开发中的极简设计, 设计是一件用户驱动很强的工作.随着用户越来越偏好更简洁的交互界面,如何剔除多余的元素,保留最基础最重要的元素是极简设计的关键.极简设计形式和功能完美结合.它最大的优点是极简的表现形式,简洁的线条,大方的留白,简约的图形化元素,就算是很复杂的内容,在这样的设计下也会显得很简洁和干练.当然,如果能有效的利用这些元素. 极简设计必须要 简洁明了和一致的可用性 .你的交互系统应该通过 清晰的视觉传达(clear visual communication) 来解决用户的问

百度新首页上线 引领极简与智能化搜索潮流

中介交易 http://www.aliyun.com/zixun/aggregation/6858.html">SEO诊断 淘宝客 云主机 技术大厅 百度极简首页 新浪科技讯 7月31日凌晨消息 今天早上百度上线极简首页,令网友耳目一新.此前的百度首页自2007年开始使用,7年来为亿万网友熟悉.今天百度首页"变脸",从新首页可以看出,百度正在实践极简风格与智能化搜索之路. 作为百度面向用户的核心入口,百度此次首页改版万众瞩目.从整体结构看,页面中央仅保留了百度logo与

苹果iOS7设计细节曝光 黑白两色极简风格

[科技讯]5月28日消息,距离苹果WWDC大会正式开幕还有半个月时间(6/10-6/14),究竟苹果会发表什么新鲜产品呢?是新一代iPad?是iOS7?还是iPhone5S呢?暂时抛开硬件设备不谈,目前网络上被炒得沸沸扬扬的消息就是iOS7的全新设计,因此经常会有迫不及待的果粉,将自己理想中的iOS7设计视频或图片放置在网络上,替大家望梅止渴一番!       伴随著时间的逼近,消息泄露越来越多,现在大家无需再止渴了!根据国外媒体报道,有可靠消息人士指出,在过去的几月之中苹果不断的测试iOS7的

百度新版首页上线,走极简路线

重剑无锋百度新首页极致至简 7月31日,甲午年,辛未月,癸卯日,宜起基.上柱.上梁.安门.这一天,百度新版首页上线,走极简路线. 从2002年增加MP3搜索,到2003年上线新闻.图片搜索与贴吧,百度的首页搜索框,一直在不断完善,做着加法.伴随搜索功能完善的,是百度创业期的超高速增长.此前,百度首页定稿于2007年,7年来,百度公司业务上快速扩张,但百度首页却是波澜不惊. 佛家说,人生有三重境界:看山是山,看水是水;看山不是山,看水不是水;看山还是山,看水还是水.这一次,百度新版首页的极简路线,

百度上线极简首页

"百度首页变了!"今天早上百度上线极简首页,令亿万网友耳目一新.此前的百度首页自2007年开始使用,7年来为亿万网友熟悉.今天百度首页"变脸",从新首页可以看出,百度正在实践极简风格与智能化搜索之路. 作为百度面向用户的核心入口,百度此次首页改版万众瞩目.从整体结构看,页面中央仅保留了百度logo与搜索框,网友可以通过首页右上角访问新闻.hao123.地图.视频.贴吧和知道.音乐.百度等垂直搜索产品.从视觉风格看,调整过比例的搜索框以及定制的"百度蓝&qu

“呵擦么”,之所赌“呵一下”的极简社交

摘要: 任何一种暂时还看不太透的新社交方式,我都希望能更多的时间能极尽其可能性,比如在微信.Facebook等产品信息噪音越来越大的语境下,极简社交的思路能带给人们什么?除了Yo,我们 任何一种暂时还看不太透的新社交方式,我都希望能更多的时间能极尽其可能性,比如在微信.Facebook等产品信息噪音越来越大的语境下,极简社交的思路能带给人们什么?除了Yo,我们之前报道过的一款产品"呵擦么"也在朝着这个方向思考. "呵擦么"刚刚发布了2.0版本,相较之前这一版更强化了

百度新首页上线:主打极简与智能化搜索

摘要: 百度极简首页 7月31日凌晨消息 百度 今日上线极简版首页,此前的百度首页自2007年开始使用,从新首页可以看出,百度正在实践极简风格与智能化搜索之路. 从整体结构看,页面中央 百度极简首页 7月31日凌晨消息 百度 今日上线极简版首页,此前的百度首页自2007年开始使用,从新首页可以看出,百度正在实践极简风格与智能化搜索之路. 从整体结构看,页面中央仅保留了百度logo与搜索框,网友可以通过首页右上角访问新闻.hao123.地图.视频.贴吧和知道.音乐.百度等垂直搜索产品.从视觉风格看

百度极简首页上线半年百度蓝引领搜索新趋势

今年7月,百度首页进行了7年以来最大的变革,由"百度蓝"引领的新版极简首页正式上线.半年过去了,近日,在一项针对"首页改版"的最新在线调查当中,我们得以再次探寻极简首页上线运营的用户实际反馈.在调研中,用户对极简首页的搜索体验.右上角层级操作.用户习惯的记忆定位等部分均给出较高分数.而出乎意外的是,对于一般调研中往往广受争议.重口难调的视觉风格和体验部分,极简首页收获了绝大多数 网友(92%)的赞赏.此部分调查结果显示,58%的网友表示最喜欢百度新页面的"

大数据的设计师帮你快速搞定一个极简风格网站

  如何快速做出一个极简风格网站?国外某个网站统计分析了112个明显带有极简风格的网站后,发布了这篇超级干货.全程都是以数据来说话,从占比最大的设计要素开始说起,有案例有分析,把极简风格的设计彻底发挥到了术的地步. 最小化设计的目标是移除界面当中不必要的元素或内容,减少干扰,使界面最大程度的支持用户的任务流程. 要将界面以恰当的方式简化到只保留必要元素的程度,设计师需要对一系列与最小化策略高度相关的设计模式有所了解.界面设计,就像人类的语言一样,最终是由人们的使用方式所定义的.如今,虽然"最小化