- /**
- * See goodbye to some douban groups.
- * @author D.
- *
- * @usage:
- * 1. login and open "http://www.douban.com/group/people/{your name here}/joins"
- * 2. open the console. run this script once then call it's "init" method and you'll see the interface has changed, it's that easy~
- */
- window.SayGoodBye = window.SayGoodBye || (function(win, undefined){
- /**
- * To extract templates from function comments.
- * @param {Function} template provider function
- */
- function _extractTpl( func ) {
- try {
- return func.toString().replace(/(^[\\s\\S]*\\/\\*\\s*)|(\\s*\\*\\/[\\s\\S]*)/g, "");
- } catch (e) {
- console.log("Failed resolving template from given function, please check.");
- console.log(e);
- }
- }
- var _strActions = _extractTpl(function(){/*
- <ul class="group-actions clearfix">
- <li class="action-backup"><a );
- styleEl.innerhtml = _strCSS;
- document.getElementsByTagName("head")[0].appendChild(styleEl);
- $(".group-list li").append(_strWrapper);
- $(".group-list").before(_strActions);
- $(".group-list").before(_strListPreview);
- /**
- * Get a copy of _originalList for _listPreview, and set gids.
- */
- $(".group-list li").each(function(idx, el){
- var gid = $(this).find(".pic a").attr("href").split("/").slice(-2, -1).pop(),
- name = $(this).find(".pic a img").attr("alt"),
- image = $(this).find(".pic a img").attr("src");
- $(this).find(".checkbox-wrapper input").attr("gid", gid);
- _originalList[gid] = {
- name: name,
- image: image
- };
- });
- /**
- * Select/Unselect all group items.
- */
- $(".group-actions").find(".action-select").bind("click", function(){
- var allInputs = $(".group-list li .checkbox-wrapper input"),
- selected = $(this).hasClass("selected");
- allInputs.each(function(idx, el){
- var gid = $(el).attr("gid");
- if ( selected ) {
- el.checked = false;
- delete _listPreview[ gid ];
- } else {
- el.checked = true;
- if ( !_listPreview[ gid ] ){
- _listPreview[ gid ] = $.extend({}, _originalList[ gid ]);
- }
- }
- });
- if ( selected ) {
- $(this).removeClass("selected");
- $(this).find("a").html("Select All Groups");
- $(".list-preview").html("");
- } else {
- $(this).addClass("selected");
- $(this).find("a").html("Unselect All Groups");
- $(".list-preview").html(_generateListPreviewStr(_listPreview));
- }
- });
- /**
- * Select/Unselect single group.
- */
- $(".group-list li .checkbox-wrapper input").bind("click", function(){
- var gid = $(this).attr("gid");
- if ( this.checked === true ) {
- if ( !_listPreview[ gid ] ) {
- _listPreview[ gid ] = $.extend({}, _originalList[ gid ]);
- }
- } else {
- delete _listPreview[ gid ];
- }
- if ( Object.keys(_listPreview).length > 0 ) {
- $(".list-preview").html(_generateListPreviewStr(_listPreview));
- } else {
- $(".list-preview").html("");
- }
- });
- /**
- * Backup group list
- */
- $(".group-actions").find(".action-backup").bind("click", function(){
- console.log("Save this json to a reliable place, later there will be a feature to batch join groups from this list.");
- console.log(JSON.strinify(_originalList));
- });
- /**
- * Say goodbye to chosen groups.
- */
- $(".group-actions").find(".action-saygoodbye").bind("click", function(){
- if ( !confirm("Are you sure you want to say goodbye to those guys?(Anyway, you can add them back later)") ) {
- return false;
- }
- for ( gid in _listPreview ) {
- console.log("saying goodbye to " + _listPreview[gid].name);
- _sayGoodbyeTo( gid, (function(gid){
- return function(){
- console.log( _listPreview[gid].name + " has already gone home.");
- delete _listPreview[gid];
- $(".group-list").find("li .checkbox-wrapper input[gid=" + gid + "]").parents("li").fadeOut(function(){
- $(this).remove();
- });
- };
- })(gid) );
- }
- });
- /**
- * Actual function to say goodbye to one group.
- * @param {String} gid group id
- * @param {Function|optional} callback
- */
- function _sayGoodbyeTo( gid, then ) {
- // TODO, remove element from DOM tree.
- $.ajax({
- type: "GET",
- url: "/group/" + gid + "/?action=quit&ck=" + get_cookie("ck"),
- data: null,
- success: then || function(){
- console.log( gid + " has already gone home.");
- }
- });
- }
- }
- // expose the APIs
- return {
- init: _init,
- chosenList: _listPreview
- };
- })(window, undefined);
- SayGoodBye.init();
- //该片段来自于http://www.codesnippet.cn/detail/0501201511469.html
来源: http://www.codesnippet.cn/detail/0501201511469.html