diff --git a/plugin/package.json b/plugin/package.json index 568bce1..56e3e04 100644 --- a/plugin/package.json +++ b/plugin/package.json @@ -1,6 +1,6 @@ { "name": "github-tags", - "version": "1.1.8", + "version": "1.1.9", "description": "A Vue.js web extension", "author": "denghaha ", "scripts": { @@ -55,7 +55,7 @@ "file-loader": "^1.1.11", "husky": "^2.4.0", "mini-css-extract-plugin": "^0.4.4", - "node-sass": "^4.9.3", + "node-sass": "^4.14.1", "prettier": "^1.17.1", "pretty-quick": "^1.8.0", "sass-loader": "^7.1.0", diff --git a/plugin/src/content_scripts/js/index.js b/plugin/src/content_scripts/js/index.js index 93e2ca5..aaafb27 100755 --- a/plugin/src/content_scripts/js/index.js +++ b/plugin/src/content_scripts/js/index.js @@ -1,21 +1,21 @@ -"use strict"; +'use strict'; // 标记当前元素已经被创建并绑定 var bindMark = '__bind_remark'; var defalutUserName = 'default_user'; -var initBmob = false +var initBmob = false; function callBmob(callback) { - //初始化Bmob - if (initBmob) { - callback() - } else { - chrome.storage.sync.get(null, function (rsp) { - Bmob.initialize(rsp.appid, rsp.apikey); - initBmob = true - callback() - }); - } + //初始化Bmob + if (initBmob) { + callback(); + } else { + chrome.storage.sync.get(null, function(rsp) { + Bmob.initialize(rsp.appid, rsp.apikey); + initBmob = true; + callback(); + }); + } } /** @@ -23,17 +23,17 @@ function callBmob(callback) { * @returns {string} * @private */ -var _get_github_username = function () { - var metas = document.getElementsByTagName('meta'); - var username = defalutUserName; - for (var i = 0; i < metas.length; i++) { - if (metas[i].getAttribute('name') === 'user-login' && metas[i].getAttribute('content') != '') { - username = metas[i].getAttribute('content'); - break; - } +var _get_github_username = function() { + var metas = document.getElementsByTagName('meta'); + var username = defalutUserName; + for (var i = 0; i < metas.length; i++) { + if (metas[i].getAttribute('name') === 'user-login' && metas[i].getAttribute('content') != '') { + username = metas[i].getAttribute('content'); + break; } + } - return username; + return username; }; /** @@ -43,72 +43,68 @@ var _get_github_username = function () { * @returns * @private */ -var _create_project_remark_dom = function (el, project_name, class_name, insert_before) { - if (project_name != undefined && project_name.indexOf("/") == 0) { - project_name = project_name.substring(1, project_name.length) - } - // 如果 dom 已经被创建则直接返回 true - if (el.getAttribute != null && - el.getAttribute('class') != null && - el.getAttribute('class').indexOf(bindMark) !== -1) - return true; - - // 调整元素位置,以供 input 填充 - //el.style.marginBottom = '30px'; - //el.style.display = 'block'; - - // 打上标记,防止重复绑定 - el.setAttribute('class', el.getAttribute('class') + ' ' + bindMark); - var vue = new Vue({ - data: { - name: project_name, - tags: [], - edit: true - }, - created: function () { - var that = this; - get_tags_by_project_name(project_name, function (tags) { - that.tags = tags; - that.edit = JSON.stringify(tags) == '[]'; - }) - }, - render: function (h) { - var that = this; - return h('tag-group', { - attrs: { - tags: that.tags, - edit: that.edit, - name: project_name, - class: class_name - }, - on: { - change: function (event) { - save_project_tags(project_name, event) - _save_project_remarks(project_name, _get_github_username(), event) - }, - changeEdit: function (edit) { - that.edit = edit - } - }, - key: project_name - }) - } - }) - ; - var input = vue.$mount(); - if (insert_before) { - el.parentNode.insertBefore(input.$el, el); - } else { - insertAfter(input.$el, el) - } - - // 有 fork 时 input 应该更往下 - //console.log(el.getElementsByClassName('fork-flag'), input); - if (el.getElementsByClassName('fork-flag').length > 0) { - input.$el.style.marginTop = '40px'; - } - - return input; +var _create_project_remark_dom = function(el, project_name, class_name, insert_before) { + if (project_name != undefined && project_name.indexOf('/') == 0) { + project_name = project_name.substring(1, project_name.length); + } + // 如果 dom 已经被创建则直接返回 true + if (el.getAttribute != null && el.getAttribute('class') != null && el.getAttribute('class').indexOf(bindMark) !== -1) return true; + + // 调整元素位置,以供 input 填充 + //el.style.marginBottom = '30px'; + //el.style.display = 'block'; + + // 打上标记,防止重复绑定 + el.setAttribute('class', el.getAttribute('class') + ' ' + bindMark); + var vue = new Vue({ + data: { + name: project_name, + tags: [], + edit: true, + }, + created: function() { + var that = this; + get_tags_by_project_name(project_name, function(tags) { + that.tags = tags; + that.edit = JSON.stringify(tags) == '[]'; + }); + }, + render: function(h) { + var that = this; + return h('tag-group', { + attrs: { + tags: that.tags, + edit: that.edit, + name: project_name, + class: class_name, + }, + on: { + change: function(event) { + save_project_tags(project_name, event); + _save_project_remarks(project_name, _get_github_username(), event); + }, + changeEdit: function(edit) { + that.edit = edit; + }, + }, + key: project_name, + }); + }, + }); + var input = vue.$mount(); + if (insert_before) { + el.parentNode.insertBefore(input.$el, el); + } else { + insertAfter(input.$el, el); + } + + // 有 fork 时 input 应该更往下 + //console.log(el.getElementsByClassName('fork-flag'), input); + if (el.getElementsByClassName('fork-flag').length > 0) { + input.$el.style.marginTop = '40px'; + } + + return input; }; /** @@ -118,102 +114,98 @@ var _create_project_remark_dom = function (el, project_name, class_name, insert_ * @returns {boolean} * @private */ -var _create_search_dom = function (el) { - // 如果 dom 已经被创建则直接返回 true - if (el.getAttribute != null && - el.getAttribute('class') != null && - el.getAttribute('class').indexOf(bindMark) !== -1) - return true; - - // 打上标记,防止重复绑定 - el.setAttribute('class', el.getAttribute('class') + ' ' + bindMark); - - var vue = new Vue({ - data: {}, - render: function (h) { - var that = this; - return h('a', { - attrs: { - id: "search_by_tag", - class: "btn", - href: "#", - style: "margin-right:8px;margin-left:8px;float: left;" - }, - domProps: { - innerHTML: "按标签搜索" - }, - on: { - click: function (event) { - _create_search_list_dom(document.getElementById("search_input").value) - }, - } - }) - } - }) - ; - var div = document.createElement("div") - - var input = document.createElement("input") - input.id = "search_input" - input.style = "width:300px;float: left;" - input.type = "search" - input.name = "q" - input.className = "form-control" - input.autocapitalize = "off" - input.autocomplete = "off" - input.value = getUrlParams("q") - - var button = document.createElement("button") - var search_function = function () { - var url = window.location.href - if (url.indexOf("&q") != -1) { - window.location.href = url.substring(0, url.indexOf("&q")) + "&q=" + input.value - } else { - window.location.href = url + "&utf8=✓&q=" + input.value - } +var _create_search_dom = function(el) { + // 如果 dom 已经被创建则直接返回 true + if (el.getAttribute != null && el.getAttribute('class') != null && el.getAttribute('class').indexOf(bindMark) !== -1) return true; + + // 打上标记,防止重复绑定 + el.setAttribute('class', el.getAttribute('class') + ' ' + bindMark); + + var vue = new Vue({ + data: {}, + render: function(h) { + var that = this; + return h('a', { + attrs: { + id: 'search_by_tag', + class: 'btn', + href: '#', + style: 'margin-right:8px;margin-left:8px;float: left;', + }, + domProps: { + innerHTML: '按标签搜索', + }, + on: { + click: function(event) { + _create_search_list_dom(document.getElementById('search_input').value); + }, + }, + }); + }, + }); + var div = document.createElement('div'); + + var input = document.createElement('input'); + input.id = 'search_input'; + input.style = 'width:300px;float: left;'; + input.type = 'search'; + input.name = 'q'; + input.className = 'form-control'; + input.autocapitalize = 'off'; + input.autocomplete = 'off'; + input.value = getUrlParams('q'); + + var button = document.createElement('button'); + var search_function = function() { + var url = window.location.href; + if (url.indexOf('&q') != -1) { + window.location.href = url.substring(0, url.indexOf('&q')) + '&q=' + input.value; + } else { + window.location.href = url + '&utf8=✓&q=' + input.value; } - button.onclick = search_function - button.className = "btn" - button.innerHTML = "搜索" - - input.addEventListener("keypress", function () { - if (event.keyCode == 13) { - search_function() - } - }) - - var vue_all = new Vue({ - data: {}, - render: function (h) { - var that = this; - return h('a', { - attrs: { - class: "btn", - href: "#", - style: "margin-right:8px;" - }, - domProps: { - innerHTML: "查看所有标签" - }, - on: { - click: function (event) { - show_all_tags(div) - }, - } - }) - } - }) + }; + button.onclick = search_function; + button.className = 'btn'; + button.innerHTML = + '搜索'; + + input.addEventListener('keypress', function() { + if (event.keyCode == 13) { + search_function(); + } + }); + + var vue_all = new Vue({ + data: {}, + render: function(h) { + var that = this; + return h('a', { + attrs: { + class: 'btn', + href: '#', + style: 'margin-right:8px;', + }, + domProps: { + innerHTML: '查看所有标签', + }, + on: { + click: function(event) { + show_all_tags(div); + }, + }, + }); + }, + }); - div.appendChild(input) - div.appendChild(vue.$mount().$el) - div.appendChild(vue_all.$mount().$el) - div.appendChild(button) - el.parentNode.insertBefore(div, el) + div.appendChild(input); + div.appendChild(vue.$mount().$el); + div.appendChild(vue_all.$mount().$el); + div.appendChild(button); + el.parentNode.insertBefore(div, el); - return false; + return false; }; - /** * 搜索后的列表 * @param el @@ -221,207 +213,205 @@ var _create_search_dom = function (el) { * @returns {boolean} * @private */ -var _create_search_list_dom = function (keyword) { - var el = document.getElementsByClassName("col-lg-12")[0] - - var vue = new Vue({ - data: { - items: [] +var _create_search_list_dom = function(keyword) { + var el = document.getElementsByClassName('col-lg-12')[0]; + + var vue = new Vue({ + data: { + items: [], + }, + created: function() { + var that = this; + search_project_by_tags(keyword.split(','), function(rsp) { + console.log('搜索结果:\n' + JSON.stringify(rsp, null, 2)); + that.items = rsp; + }); + }, + render: function(h) { + var that = this; + return h('list', { + attrs: { + items: that.items, }, - created: function () { - var that = this; - search_project_by_tags(keyword.split(","), function (rsp) { - console.log("搜索结果:\n" + JSON.stringify(rsp, null, 2)) - that.items = rsp - }) - }, - render: function (h) { - var that = this; - return h('list', { - attrs: { - items: that.items - }, - }) - } - }) - - //计算出已存在item数量 - var childNodes = el.childNodes - var itemCount = 0 - for (var i = 0; i < childNodes.length; i++) { - if (childNodes[i].className != undefined && childNodes[i].className.indexOf("col-12") != -1) { - itemCount++ - } - } - //先清空现有的item - for (var i = 0; i < itemCount; i++) { - el.removeChild(el.getElementsByClassName("col-12")[0]) - } - - var result_container = document.getElementsByClassName("search-result-container") - //如果之前已经有搜索结果 先删除掉之前的 - if (result_container != null && result_container.length > 0) { - el.removeChild(result_container[0]) + }); + }, + }); + + //计算出已存在item数量 + var childNodes = el.childNodes; + var itemCount = 0; + for (var i = 0; i < childNodes.length; i++) { + if (childNodes[i].className != undefined && childNodes[i].className.indexOf('col-12') != -1) { + itemCount++; } - var paginate = document.getElementsByClassName("paginate-container")[0] - el.insertBefore(vue.$mount().$el, paginate) - - return false; + } + //先清空现有的item + for (var i = 0; i < itemCount; i++) { + el.removeChild(el.getElementsByClassName('col-12')[0]); + } + + var result_container = document.getElementsByClassName('search-result-container'); + //如果之前已经有搜索结果 先删除掉之前的 + if (result_container != null && result_container.length > 0) { + el.removeChild(result_container[0]); + } + var paginate = document.getElementsByClassName('paginate-container')[0]; + el.insertBefore(vue.$mount().$el, paginate); + + return false; }; -var get_all_tags = function (callback) { - get_all_project_data(function (all) { - var array = new Array() - //遍历A-Z - for (let k = "a".charCodeAt(); k <= "z".charCodeAt(); k++) { - var letterUpper = String.fromCharCode(k).toUpperCase() - //遍历所有标签 - var letterArray = new Array() - for (let i = 0; i < all.length; i++) { - var tags = JSON.parse(all[i].tag_name_array) - for (let j = 0; j < tags.length; j++) { - var tag = tags[j] - var letter = pinyinUtil.getFirstLetter(tag.charAt(0)); - //如果字母相同 - if (letterUpper == letter.toUpperCase()) { - if (letterArray.indexOf(tag) == -1) { - letterArray.push(tag) - } - } - } - } - if (letterArray.length > 0) { - letterArray.sort( - function compareFunction(param1, param2) { - return param2.localeCompare(param1, "zh"); - } - ); - var item = {} - item.letter = letterUpper - item.tags = letterArray - array.push(item) +var get_all_tags = function(callback) { + get_all_project_data(function(all) { + var array = new Array(); + //遍历A-Z + for (let k = 'a'.charCodeAt(); k <= 'z'.charCodeAt(); k++) { + var letterUpper = String.fromCharCode(k).toUpperCase(); + //遍历所有标签 + var letterArray = new Array(); + for (let i = 0; i < all.length; i++) { + var tags = JSON.parse(all[i].tag_name_array); + for (let j = 0; j < tags.length; j++) { + var tag = tags[j]; + var letter = pinyinUtil.getFirstLetter(tag.charAt(0)); + //如果字母相同 + if (letterUpper == letter.toUpperCase()) { + if (letterArray.indexOf(tag) == -1) { + letterArray.push(tag); } + } } - callback(array) - }) -} + } + if (letterArray.length > 0) { + letterArray.sort(function compareFunction(param1, param2) { + return param2.localeCompare(param1, 'zh'); + }); + var item = {}; + item.letter = letterUpper; + item.tags = letterArray; + array.push(item); + } + } + callback(array); + }); +}; function show_all_tags(container) { - var vue = new Vue({ - data: { - tags: [], - loading: true - }, created: function () { - var that = this; - get_all_tags(function (all_tags) { - console.log(JSON.stringify(all_tags)) - if (all_tags.length <= 0) { - return - } - that.loading = false - that.tags = all_tags - }) - }, render: function (h) { - var that = this; - return h('all-tag', { - attrs: { - tags: that.tags, - dialogVisible: true, - loading: that.loading - } - }) + var vue = new Vue({ + data: { + tags: [], + loading: true, + }, + created: function() { + var that = this; + get_all_tags(function(all_tags) { + console.log(JSON.stringify(all_tags)); + if (all_tags.length <= 0) { + return; } - }) - container.appendChild(vue.$mount().$el) + that.loading = false; + that.tags = all_tags; + }); + }, + render: function(h) { + var that = this; + return h('all-tag', { + attrs: { + tags: that.tags, + dialogVisible: true, + loading: that.loading, + }, + }); + }, + }); + container.appendChild(vue.$mount().$el); } - /** * 绑定组件到相关元素中 * @private */ -var _bind_project_remarks = function () { - var project, projects, project_name, i; - - var repositories = document.getElementsByClassName('h6 text-uppercase') - if (repositories !== null && repositories.length > 0) { - var details = repositories[0].parentNode.getElementsByTagName("details") - _create_search_dom(details[0]) - repositories[0].parentNode.removeChild(repositories[0]) - } - - // 项目详情页 - if (document.getElementsByClassName('repohead-details-container').length === 1 && - document.getElementsByClassName('repohead-details-container')[0].getElementsByTagName('h1').length === 1) { - project = document.getElementsByClassName('repohead-details-container')[0].getElementsByTagName('h1')[0]; - projects = project.getElementsByTagName('a'); - for (i = 0; i < projects.length; i++) { - if (projects[i].getAttribute('data-pjax') !== null) { - var fork = project.getElementsByClassName("fork-flag") - //如果是fork的 拿fork的名字 - if (fork != null && fork.length > 0) { - project_name = fork[0].getElementsByTagName("a")[0].getAttribute('href'); - } else { - //如果fork没有拿到直接取项目名 - project_name = project.getElementsByTagName('a')[i].getAttribute('href'); - } - break; - } +var _bind_project_remarks = function() { + var project, projects, project_name, i; + + var repositories = document.getElementsByClassName('h6 text-uppercase'); + if (repositories !== null && repositories.length > 0) { + var details = repositories[0].parentNode.getElementsByTagName('details'); + _create_search_dom(details[0]); + repositories[0].parentNode.removeChild(repositories[0]); + } + + // 项目详情页 + if ( + document.getElementsByClassName('repohead-details-container').length === 1 && + document.getElementsByClassName('repohead-details-container')[0].getElementsByTagName('h1').length === 1 + ) { + project = document.getElementsByClassName('repohead-details-container')[0].getElementsByTagName('h1')[0]; + projects = project.getElementsByTagName('a'); + for (i = 0; i < projects.length; i++) { + if (projects[i].getAttribute('data-pjax') !== null) { + var fork = project.getElementsByClassName('fork-flag'); + //如果是fork的 拿fork的名字 + if (fork != null && fork.length > 0) { + project_name = fork[0].getElementsByTagName('a')[0].getAttribute('href'); + } else { + //如果fork没有拿到直接取项目名 + project_name = project.getElementsByTagName('a')[i].getAttribute('href'); } - - _create_project_remark_dom(project, project_name, "git_remarks_plugin__input", false); + break; + } } - - // 列表 - if (document.getElementById('js-pjax-container') !== null) { - var list = document.getElementById('js-pjax-container'); - projects = list.getElementsByTagName('h3'); - for (i = 0; i < projects.length; i++) { - //如果有fork的 取fork的名称 - var forkSpan = projects[i].parentNode.getElementsByClassName("f6 text-gray mb-1") - if (forkSpan != null && forkSpan.length > 0) { - var forkA = forkSpan[0].getElementsByTagName("a") - if (forkA != null && forkA.length > 0) { - project_name = forkA[0].getAttribute('href'); - } - } else { - //如果没有fork才取项目名 - if (projects[i].getElementsByTagName('a').length === 1) { - project_name = projects[i].getElementsByTagName('a')[0].getAttribute('href'); - } - } - if (project_name !== null && project_name != undefined && project_name != "") { - _create_project_remark_dom(projects[i], project_name, "repository_detail_tags_container", false); - } + _create_project_remark_dom(project, project_name, 'git_remarks_plugin__input', false); + } + + // 列表 + if (document.getElementById('js-pjax-container') !== null) { + var list = document.getElementById('js-pjax-container'); + projects = list.getElementsByTagName('h3'); + for (i = 0; i < projects.length; i++) { + //如果有fork的 取fork的名称 + var forkSpan = projects[i].parentNode.getElementsByClassName('f6 text-gray mb-1'); + if (forkSpan != null && forkSpan.length > 0) { + var forkA = forkSpan[0].getElementsByTagName('a'); + if (forkA != null && forkA.length > 0) { + project_name = forkA[0].getAttribute('href'); + } + } else { + //如果没有fork才取项目名 + if (projects[i].getElementsByTagName('a').length === 1) { + project_name = projects[i].getElementsByTagName('a')[0].getAttribute('href'); } + } + if (project_name !== null && project_name != undefined && project_name != '') { + _create_project_remark_dom(projects[i], project_name, 'repository_detail_tags_container', false); + } } - - // 个人主页 - if (document.getElementsByClassName('pinned-repos-list').length === 1 && - document.getElementsByClassName('pinned-repo-item').length > 0) { - projects = document.getElementsByClassName('pinned-repo-item'); - for (i = 0; i < projects.length; i++) { - var createClass = "stars_list_container"; - var p = projects[i].getElementsByClassName('pinned-repo-desc'); - //先检查fork的 - var forkTag = projects[i].getElementsByClassName("text-gray text-small mb-2") - if (forkTag != null && forkTag.length > 0) { - var forkA = forkTag[0].getElementsByTagName("a") - if (forkA != null && forkA.length > 0) { - project_name = forkA[0].getAttribute('href'); - createClass = "stars_list_fork"; - } - } else { - project_name = projects[i].getElementsByTagName("a")[0].getAttribute('href'); - } - - _create_project_remark_dom(p[0], project_name, createClass, true); + } + + // 个人主页 + if (document.getElementsByClassName('pinned-repos-list').length === 1 && document.getElementsByClassName('pinned-repo-item').length > 0) { + projects = document.getElementsByClassName('pinned-repo-item'); + for (i = 0; i < projects.length; i++) { + var createClass = 'stars_list_container'; + var p = projects[i].getElementsByClassName('pinned-repo-desc'); + //先检查fork的 + var forkTag = projects[i].getElementsByClassName('text-gray text-small mb-2'); + if (forkTag != null && forkTag.length > 0) { + var forkA = forkTag[0].getElementsByTagName('a'); + if (forkA != null && forkA.length > 0) { + project_name = forkA[0].getAttribute('href'); + createClass = 'stars_list_fork'; } + } else { + project_name = projects[i].getElementsByTagName('a')[0].getAttribute('href'); + } + + _create_project_remark_dom(p[0], project_name, createClass, true); } + } }; - /** * 读取配置项 * @param project_name 项目名称 @@ -429,72 +419,69 @@ var _bind_project_remarks = function () { * @param callback 回调 * @private */ -var _get_project_remarks = function (project_name, username, callback) { - chrome.storage.local.get('items', function (rsp) { - if (JSON.stringify(rsp) != '{}') { - //有数据说明迁移过了 直接取本地 - _get_project_remarks_v2(project_name, username, callback) - } else { - //没有数据说明未迁移 从同步迁移到本地 - chrome.storage.sync.get('items', function (rsp) { - chrome.storage.local.set(rsp); - chrome.storage.sync.set({'items': null}, function () { +var _get_project_remarks = function(project_name, username, callback) { + chrome.storage.local.get('items', function(rsp) { + if (JSON.stringify(rsp) != '{}') { + //有数据说明迁移过了 直接取本地 + _get_project_remarks_v2(project_name, username, callback); + } else { + //没有数据说明未迁移 从同步迁移到本地 + chrome.storage.sync.get('items', function(rsp) { + chrome.storage.local.set(rsp); + chrome.storage.sync.set({ items: null }, function() {}); - }); + _get_project_remarks_v2(project_name, username, callback); + }); + } + }); +}; - _get_project_remarks_v2(project_name, username, callback) - }); - } +var _get_project_remarks_v2 = function(project_name, username, callback) { + chrome.storage.local.get('items', function(rsp) { + if (JSON.stringify(rsp) == '{}') { + var result = {}; + result.project_name = project_name; + result.tags = []; + var items = []; + items.push(result); - }); + rsp.items = items; + } + callback(rsp); + }); }; -var _get_project_remarks_v2 = function (project_name, username, callback) { - chrome.storage.local.get('items', function (rsp) { - if (JSON.stringify(rsp) == '{}') { - var result = {} - result.project_name = project_name - result.tags = [] - var items = []; - items.push(result) - - rsp.items = items +var _get_project_tags = function(project_name, username, callback) { + _get_project_remarks(project_name, username, function(rsp) { + for (var i = 0; i < rsp.items.length; i++) { + if (rsp.items[i].project_name == project_name) { + if (rsp.items[i].tags.length > 0) { + callback(rsp.items[i].tags); + break; } - callback(rsp); - }); -} - -var _get_project_tags = function (project_name, username, callback) { - _get_project_remarks(project_name, username, function (rsp) { - for (var i = 0; i < rsp.items.length; i++) { - if (rsp.items[i].project_name == project_name) { - if (rsp.items[i].tags.length > 0) { - callback(rsp.items[i].tags) - break - } - } - } - }) -} + } + } + }); +}; -var _search_project_by_tag = function (tag_name, callback) { - _get_project_remarks("", _get_github_username(), function (rsp) { - var array = new Array() - for (var i = 0; i < rsp.items.length; i++) { - for (var j = 0; j < rsp.items[i].tags.length; j++) { - var item_tag = rsp.items[i].tags[j] - if (item_tag != null && item_tag != undefined && item_tag != "") { - if (item_tag.toLowerCase().indexOf(tag_name.toLowerCase()) != -1) { - array.push(rsp.items[i]) - break - } - } - } +var _search_project_by_tag = function(tag_name, callback) { + _get_project_remarks('', _get_github_username(), function(rsp) { + var array = new Array(); + for (var i = 0; i < rsp.items.length; i++) { + for (var j = 0; j < rsp.items[i].tags.length; j++) { + var item_tag = rsp.items[i].tags[j]; + if (item_tag != null && item_tag != undefined && item_tag != '') { + if (item_tag.toLowerCase().indexOf(tag_name.toLowerCase()) != -1) { + array.push(rsp.items[i]); + break; + } } + } + } - callback(array) - }) -} + callback(array); + }); +}; /** * 保存用户设置 @@ -503,36 +490,36 @@ var _search_project_by_tag = function (tag_name, callback) { * @param value * @private */ -var _save_project_remarks = function (project_name, username, value) { - _get_project_remarks(null, null, function (rsp) { - if (project_name == null || project_name == '') { - return - } - if (project_name.indexOf("/") == 0) { - project_name = project_name.substring(1, project_name.length) - } +var _save_project_remarks = function(project_name, username, value) { + _get_project_remarks(null, null, function(rsp) { + if (project_name == null || project_name == '') { + return; + } + if (project_name.indexOf('/') == 0) { + project_name = project_name.substring(1, project_name.length); + } - if (rsp.items == undefined) { - rsp.items = [] - } + if (rsp.items == undefined) { + rsp.items = []; + } - var add = false - for (var i = 0; i < rsp.items.length; i++) { - if (rsp.items[i].project_name == project_name) { - rsp.items[i].tags = value - add = true - break - } - } - if (add == false) { - var result = {} - result.project_name = project_name - result.tags = value - rsp.items.push(result) - } - //console.log("保存成功\n" + JSON.stringify(rsp, null, 2)) - chrome.storage.local.set(rsp); - }); + var add = false; + for (var i = 0; i < rsp.items.length; i++) { + if (rsp.items[i].project_name == project_name) { + rsp.items[i].tags = value; + add = true; + break; + } + } + if (add == false) { + var result = {}; + result.project_name = project_name; + result.tags = value; + rsp.items.push(result); + } + //console.log("保存成功\n" + JSON.stringify(rsp, null, 2)) + chrome.storage.local.set(rsp); + }); }; /** @@ -540,24 +527,27 @@ var _save_project_remarks = function (project_name, username, value) { * @param project_name */ function get_tags_by_project_name(project_name, callback) { - if (project_name == null || project_name == undefined || project_name == "") { - return - } - callBmob(function () { - //先查出这个项目的id - const query = Bmob.Query("project_tags"); - query.equalTo("project_name", '==', project_name); - query.select("tag_name_array"); - query.limit(1) - query.find().then(res => { - if (res != null && res.length > 0) { - callback(JSON.parse(res[0].tag_name_array)) - } - }).catch(err => { - console.log(err) - errorInitProjectTags(err, project_name) - }); - }) + if (project_name == null || project_name == undefined || project_name == '') { + return; + } + callBmob(function() { + //先查出这个项目的id + const query = Bmob.Query('project_tags'); + query.equalTo('project_name', '==', project_name); + query.select('tag_name_array'); + query.limit(1); + query + .find() + .then(res => { + if (res != null && res.length > 0) { + callback(JSON.parse(res[0].tag_name_array)); + } + }) + .catch(err => { + console.log(err); + errorInitProjectTags(err, project_name); + }); + }); } /** @@ -566,16 +556,19 @@ function get_tags_by_project_name(project_name, callback) { * @param project_name */ function errorInitProjectTags(err, project_name) { - if (err.code == 101) { - const query = Bmob.Query("project_tags"); - query.set('tag_name_array', "[]") - query.set("project_name", project_name) - query.save().then(obj => { - console.log("手动建表--->" + JSON.stringify(obj)) - }).catch(err => { - console.log("手动建表--->" + err) - }) - } + if (err.code == 101) { + const query = Bmob.Query('project_tags'); + query.set('tag_name_array', '[]'); + query.set('project_name', project_name); + query + .save() + .then(obj => { + console.log('手动建表--->' + JSON.stringify(obj)); + }) + .catch(err => { + console.log('手动建表--->' + err); + }); + } } /** @@ -584,59 +577,64 @@ function errorInitProjectTags(err, project_name) { * @param value */ function save_project_tags(project_name, value) { - callBmob(function () { - //先根据name查找项目 + callBmob(function() { + //先根据name查找项目 + const query = Bmob.Query('project_tags'); + query.equalTo('project_name', '==', project_name); + query + .find() + .then(results => { const query = Bmob.Query('project_tags'); - query.equalTo("project_name", '==', project_name); - query.find().then(results => { - const query = Bmob.Query('project_tags'); - query.set('tag_name_array', JSON.stringify(value)) - if (results.length <= 0) { - //新增 - query.set("project_name", project_name) - } else { - //修改 - query.set("id", results[0].objectId) - } - query.save().then(obj => { - console.log(JSON.stringify(obj)) - }).catch(err => { - console.log(err) - }) - }).catch(err => { - console.log(err) - }) - }) + query.set('tag_name_array', JSON.stringify(value)); + if (results.length <= 0) { + //新增 + query.set('project_name', project_name); + } else { + //修改 + query.set('id', results[0].objectId); + } + query + .save() + .then(obj => { + console.log(JSON.stringify(obj)); + }) + .catch(err => { + console.log(err); + }); + }) + .catch(err => { + console.log(err); + }); + }); } - /** * 根据标签找项目 */ function get_all_project_data(callback) { - callBmob(function () { - //每页数量 - var pageSize = 1000 - const countQuery = Bmob.Query('project_tags'); - countQuery.count().then(count => { - var allData = new Array() - //一共几页 - var pageCount = Math.ceil(count / pageSize) - for (let page = 0; page < pageCount; page++) { - const query = Bmob.Query("project_tags"); - query.skip(page * pageSize); - query.order("-createdAt"); - query.limit(pageSize); - query.find().then(res => { - allData.push.apply(allData, res); - if (page == pageCount - 1) { - //最后一页 请求完就直接返回 - callback(allData) - } - }); - } + callBmob(function() { + //每页数量 + var pageSize = 1000; + const countQuery = Bmob.Query('project_tags'); + countQuery.count().then(count => { + var allData = new Array(); + //一共几页 + var pageCount = Math.ceil(count / pageSize); + for (let page = 0; page < pageCount; page++) { + const query = Bmob.Query('project_tags'); + query.skip(page * pageSize); + query.order('-createdAt'); + query.limit(pageSize); + query.find().then(res => { + allData.push.apply(allData, res); + if (page == pageCount - 1) { + //最后一页 请求完就直接返回 + callback(allData); + } }); - }) + } + }); + }); } /** @@ -645,29 +643,29 @@ function get_all_project_data(callback) { * @param callback */ function search_project_by_tags(keyword_array, callback) { - get_all_project_data(function (all) { - var array = new Array() - for (let i = 0; i < all.length; i++) { - var item = all[i] - var tag_array_json = item.tag_name_array - if (tag_array_json != null && tag_array_json.length > 0) { - var tag_array = JSON.parse(tag_array_json) - for (let j = 0; j < tag_array.length; j++) { - if (tag_array[j] != null && tag_array[j] != undefined && tag_array[j] != "") { - for (let k = 0; k < keyword_array.length; k++) { - //if (tag_array[j].toLowerCase().indexOf(keyword_array[k].toLowerCase()) != -1) { - if (tag_array[j].toLowerCase() == keyword_array[k].toLowerCase()) { - item.tags = tag_array - array.push(item) - break - } - } - } - } + get_all_project_data(function(all) { + var array = new Array(); + for (let i = 0; i < all.length; i++) { + var item = all[i]; + var tag_array_json = item.tag_name_array; + if (tag_array_json != null && tag_array_json.length > 0) { + var tag_array = JSON.parse(tag_array_json); + for (let j = 0; j < tag_array.length; j++) { + if (tag_array[j] != null && tag_array[j] != undefined && tag_array[j] != '') { + for (let k = 0; k < keyword_array.length; k++) { + //if (tag_array[j].toLowerCase().indexOf(keyword_array[k].toLowerCase()) != -1) { + if (tag_array[j].toLowerCase() == keyword_array[k].toLowerCase()) { + item.tags = tag_array; + array.push(item); + break; + } } + } } - callback(array) - }) + } + } + callback(array); + }); } /** @@ -675,41 +673,46 @@ function search_project_by_tags(keyword_array, callback) { * @param dom 需要监听的 dom * @private */ -var _watch_dom_flash = function (dom) { - var MutationObserver = window.MutationObserver || window.WebKitMutationObserver; +var _watch_dom_flash = function(dom) { + var MutationObserver = window.MutationObserver || window.WebKitMutationObserver; - var observer = new MutationObserver(function (mutations) { - _bind_project_remarks() - }); - observer.observe(dom, { - childList: true, - subtree: true, - characterData: true - }); + var observer = new MutationObserver(function(mutations) { + _bind_project_remarks(); + }); + observer.observe(dom, { + childList: true, + subtree: true, + characterData: true, + }); }; /** * 窗口载入完毕执行 */ -document.addEventListener("DOMContentLoaded", function () { +document.addEventListener( + 'DOMContentLoaded', + function() { // 绑定组件 _bind_project_remarks(); // 监听 github dom 刷新 if (document.getElementById('js-pjax-container') != null) { - _watch_dom_flash(document.getElementById('js-pjax-container')); + _watch_dom_flash(document.getElementById('js-pjax-container')); } else if (document.getElementById('js-repo-pjax-container') != null) { - _watch_dom_flash(document.getElementById('js-repo-pjax-container')); + _watch_dom_flash(document.getElementById('js-repo-pjax-container')); } -}, false); + }, + false +); function insertAfter(newNode, referenceNode) { - referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling); + referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling); } function getUrlParams(name) { - var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); - var r = encodeURI(window.location.search).substr(1).match(reg); - if (r != null) - return decodeURI(unescape(r[2])); - return null; -} \ No newline at end of file + var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)'); + var r = encodeURI(window.location.search) + .substr(1) + .match(reg); + if (r != null) return decodeURI(unescape(r[2])); + return null; +} diff --git a/plugin/src/content_scripts/js/inject.js b/plugin/src/content_scripts/js/inject.js index c09a7fc..1050554 100644 --- a/plugin/src/content_scripts/js/inject.js +++ b/plugin/src/content_scripts/js/inject.js @@ -42,10 +42,10 @@ var bindMark = function(el) { * @returns * @private */ -var _create_project_remark_dom = function(el, item, class_name, insert_before) { +var _create_project_remark_dom = function(el, item, class_name, insertElement) { // 调整元素位置,以供 input 填充 - //el.style.marginBottom = '30px'; - //el.style.display = 'block'; + // el.style.marginBottom = '30px'; + // el.style.display = 'block'; if (!item.tags) { item.tags = []; @@ -81,14 +81,10 @@ var _create_project_remark_dom = function(el, item, class_name, insert_before) { }); var input = vue.$mount(); - if (insert_before) { - el.parentNode.insertBefore(input.$el, el); - } else { - insertAfter(input.$el, el); - } + insertElement(input.$el); // 有 fork 时 input 应该更往下 - //console.log(el.getElementsByClassName('fork-flag'), input); + // console.log(el.getElementsByClassName('fork-flag'), input); if (el.getElementsByClassName('fork-flag').length > 0) { input.$el.style.marginTop = '40px'; } @@ -245,7 +241,7 @@ var _create_search_list_dom = function(keyword) { }, }); - //计算出已存在item数量 + // 计算出已存在item数量 var childNodes = el.childNodes; var itemCount = 0; for (var i = 0; i < childNodes.length; i++) { @@ -253,13 +249,13 @@ var _create_search_list_dom = function(keyword) { itemCount++; } } - //先清空现有的item + // 先清空现有的item for (var i = 0; i < itemCount; i++) { el.removeChild(el.getElementsByClassName('col-12')[0]); } var result_container = document.getElementsByClassName('search-result-container'); - //如果之前已经有搜索结果 先删除掉之前的 + // 如果之前已经有搜索结果 先删除掉之前的 if (result_container != null && result_container.length > 0) { el.removeChild(result_container[0]); } @@ -272,16 +268,16 @@ var _create_search_list_dom = function(keyword) { var get_all_tags = function(callback) { getAllTags(function(all) { var array = new Array(); - //遍历A-Z + // 遍历A-Z for (let k = 'a'.charCodeAt(); k <= 'z'.charCodeAt(); k++) { var letterUpper = String.fromCharCode(k).toUpperCase(); - //遍历所有标签 + // 遍历所有标签 var letterArray = new Array(); for (let i = 0; i < all.length; i++) { for (let j = 0; j < all[i].tags.length; j++) { var tag = all[i].tags[j]; var letter = pinyinUtil.getFirstLetter(tag.charAt(0)); - //如果字母相同 + // 如果字母相同 if (letterUpper == letter.toUpperCase()) { if (letterArray.indexOf(tag) == -1) { letterArray.push(tag); @@ -364,11 +360,11 @@ var _bind_project_remarks = function() { for (i = 0; i < projects.length; i++) { if (projects[i].getAttribute('data-pjax') !== null) { var fork = project.getElementsByClassName('fork-flag'); - //如果是fork的 拿fork的名字 + // 如果是fork的 拿fork的名字 if (fork != null && fork.length > 0) { project_name = fork[0].getElementsByTagName('a')[0].getAttribute('href'); } else { - //如果fork没有拿到直接取项目名 + // 如果fork没有拿到直接取项目名 project_name = project.getElementsByTagName('a')[i].getAttribute('href'); } break; @@ -376,7 +372,10 @@ var _bind_project_remarks = function() { } requestTagsByRepo(project_name, function(rsp) { - _create_project_remark_dom(project.parentNode.parentNode, rsp ? rsp : { _id: project_name }, 'git_remarks_plugin__input git_remarks_plugin__detail px-3 px-lg-5', false); + const node = project.parentNode.parentNode; + _create_project_remark_dom(node, rsp || { _id: project_name }, 'git_remarks_plugin__input git_remarks_plugin__detail px-3 px-lg-5', el => { + insertAfter(el, node); + }); }); return; } @@ -400,7 +399,9 @@ var _bind_project_remarks = function() { } requestTagsByRepoList(project_name_array, function(rsp) { for (let j = 0; j < rsp.length; j++) { - _create_project_remark_dom(repoNameList[j], rsp[j] ? rsp[j] : { _id: project_name }, 'repository_detail_tags_container', false); + _create_project_remark_dom(repoNameList[j], rsp[j] ? rsp[j] : { _id: project_name }, 'repository_detail_tags_container', el => { + insertAfter(el, repoNameList[j].parentNode); + }); } }); return; @@ -420,7 +421,7 @@ var _bind_project_remarks = function() { projects = list.getElementsByTagName('h3'); var project_name_array = []; for (i = 0; i < projects.length; i++) { - //如果有fork的 取fork的名称 + // 如果有fork的 取fork的名称 var forkSpan = projects[i].parentNode.getElementsByClassName('f6 text-gray mb-1'); if (forkSpan != null && forkSpan.length > 0) { var forkA = forkSpan[0].getElementsByTagName('a'); @@ -428,7 +429,7 @@ var _bind_project_remarks = function() { project_name = forkA[0].getAttribute('href'); } } else { - //如果没有fork才取项目名 + // 如果没有fork才取项目名 if (projects[i].getElementsByTagName('a').length === 1) { project_name = projects[i].getElementsByTagName('a')[0].getAttribute('href'); } @@ -440,7 +441,9 @@ var _bind_project_remarks = function() { requestTagsByRepoList(project_name_array, function(rsp) { for (let j = 0; j < rsp.length; j++) { - _create_project_remark_dom(projects[j], rsp[j] ? rsp[j] : { _id: project_name }, 'repository_detail_tags_container', false); + _create_project_remark_dom(projects[j], rsp[j] ? rsp[j] : { _id: project_name }, 'repository_detail_tags_container', el => { + insertAfter(el, projects[j]); + }); } }); return; @@ -458,7 +461,7 @@ var _bind_project_remarks = function() { var project_name_array = []; for (i = 0; i < projects.length; i++) { var createClass = 'stars_list_container'; - //先检查fork的 + // 先检查fork的 var forkTag = projects[i].getElementsByClassName('text-gray text-small mb-2'); if (forkTag != null && forkTag.length > 0) { var forkA = forkTag[0].getElementsByTagName('a'); @@ -475,10 +478,11 @@ var _bind_project_remarks = function() { requestTagsByRepoList(project_name_array, function(rsp) { for (let j = 0; j < rsp.length; j++) { var p = projects[j].getElementsByClassName('pinned-item-desc')[0]; - _create_project_remark_dom(p, rsp[j] ? rsp[j] : { _id: project_name }, createClass, true); + _create_project_remark_dom(p, rsp[j] ? rsp[j] : { _id: project_name }, createClass, el => { + p.parentNode.insertBefore(el, p); + }); } }); - return; } }; @@ -531,8 +535,7 @@ function getUrlParams(name) { return null; } -//axios.defaults.baseURL = 'http://localhost:9000/'; -axios.defaults.baseURL = 'https://gt.xiandan.in'; +axios.defaults.baseURL = 'http://localhost:9000/'; chrome.storage.sync.get('user', function(rsp) { console.log('GithubTags', JSON.stringify(rsp, '\t', 2)); axios.interceptors.request.use( diff --git a/plugin/src/manifest.json b/plugin/src/manifest.json index afa2572..8eb4f24 100644 --- a/plugin/src/manifest.json +++ b/plugin/src/manifest.json @@ -1,7 +1,7 @@ { "name": "GithubTags", "description": "给Github项目添加标签,支持按标签搜索,支持数据云同步", - "version": "1.1.8", + "version": "1.1.9", "manifest_version": 2, "icons": { "48": "icons/icon.png", diff --git a/update.json b/update.json index e457150..91a80ea 100644 --- a/update.json +++ b/update.json @@ -1,5 +1,5 @@ { - "version": "1.1.8", + "version": "1.1.9", "message": "适配最新版页面", "link": "https://github.com/xiandanin/github-tags#%E6%9B%B4%E6%96%B0%E8%AF%B4%E6%98%8E" }