Skip to content

Commit

Permalink
GreaemonkeyEnhancer 增加工具菜单的支持,支持中键连续点击,后台打开
Browse files Browse the repository at this point in the history
  • Loading branch information
ywzhaiqi committed Sep 15, 2014
1 parent 3516b4e commit 648ae3b
Showing 1 changed file with 129 additions and 112 deletions.
241 changes: 129 additions & 112 deletions GreaemonkeyEnhancer/GreaemonkeyEnhancer.uc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// @include main
// @charset utf-8
// @author ywzhaiqi
// @version 2014.8.3
// @version 2014.9.15
// @homepageURL https://github.com/ywzhaiqi/userChromeJS/tree/master/GreaemonkeyEnhancer
// @downloadURL https://github.com/ywzhaiqi/userChromeJS/raw/master/GreaemonkeyEnhancer/GreaemonkeyEnhancer.uc.js
// @startup window.GreaemonkeyEnhancer.init();
Expand All @@ -14,92 +14,107 @@

(function() {

var prefs = {
inBackground: 1, // 1:后台打开,0:前台打开
};

var ApplyPatchForScript = (function(){
const USO_URL_RE = /(^https?:\/\/userscripts.org.*\/scripts\/source\/\d+)\.\w+\.js$/i;

const GFO_URL_RE_1 = /(^https?:\/\/greasyfork.org\/scripts\/code\/\w+)\.\w+\.js$/i;
const GFO_URL_RE_2 = /(^https?:\/\/greasyfork.org\/scripts\/[^\/]+\/)code[\.\/].*\w+\.js$/i;

// (http://binux.github.io/ThunderLixianExporter/)master/ThunderLixianExporter.user.js
const GITHUB_URL_RE_1 = /(^https?:\/\/\w+.github.io\/\w+\/)master\/.*.*\w+\.js$/i;
// 从 https://raw.githubusercontent.com/ywzhaiqi/userscript/master/noNoticetitleflashOnBBS.user.js
// 转为 https://github.com/ywzhaiqi/userscript/blob/master/noNoticetitleflashOnBBS.user.js
const GITHUB_URL_RE_2 = /(^https?:\/\/raw.githubusercontent.com\/.*?\/master\/.*\.user\.js$)/i;

function getScriptHomeURL(downURL) {
var url;
if (downURL && downURL.startsWith('http')) {
if (USO_URL_RE.test(downURL)) {
url = RegExp.$1.replace(/source/, "show");
} else if (GFO_URL_RE_1.test(downURL)) {
url = RegExp.$1;
} else if (GFO_URL_RE_2.test(downURL)) {
url = RegExp.$1;
} else if (GITHUB_URL_RE_1.test(downURL)) {
url = RegExp.$1;
} else if (GITHUB_URL_RE_2.test(downURL)) {
url = RegExp.$1.replace('raw.githubusercontent.com', 'github.com')
.replace('/master/', '/blob/master/');
}
}
return url ? decodeURIComponent(url) : null;
}

function addHomePage(scripts){
scripts.forEach(function(script){
if (!script.homepageURL) {
var url = script.downloadURL || script.updateURL;
script.homepageURL = getScriptHomeURL(url);
}
});
}

return {
addHomePage: addHomePage,
getHomePageURL: getScriptHomeURL
}
const USO_URL_RE = /(^https?:\/\/userscripts.org.*\/scripts\/source\/\d+)\.\w+\.js$/i;

const GFO_URL_RE_1 = /(^https?:\/\/greasyfork.org\/scripts\/code\/\w+)\.\w+\.js$/i;
const GFO_URL_RE_2 = /(^https?:\/\/greasyfork.org\/scripts\/[^\/]+\/)code[\.\/].*\w+\.js$/i;

// (http://binux.github.io/ThunderLixianExporter/)master/ThunderLixianExporter.user.js
const GITHUB_URL_RE_1 = /(^https?:\/\/\w+.github.io\/\w+\/)master\/.*.*\w+\.js$/i;
// 从 https://raw.githubusercontent.com/ywzhaiqi/userscript/master/noNoticetitleflashOnBBS.user.js
// 转为 https://github.com/ywzhaiqi/userscript/blob/master/noNoticetitleflashOnBBS.user.js
const GITHUB_URL_RE_2 = /(^https?:\/\/raw.githubusercontent.com\/.*?\/master\/.*\.user\.js$)/i;

function getScriptHomeURL(downURL) {
var url;
if (downURL && downURL.startsWith('http')) {
if (USO_URL_RE.test(downURL)) {
url = RegExp.$1.replace(/source/, "show");
} else if (GFO_URL_RE_1.test(downURL)) {
url = RegExp.$1;
} else if (GFO_URL_RE_2.test(downURL)) {
url = RegExp.$1;
} else if (GITHUB_URL_RE_1.test(downURL)) {
url = RegExp.$1;
} else if (GITHUB_URL_RE_2.test(downURL)) {
url = RegExp.$1.replace('raw.githubusercontent.com', 'github.com')
.replace('/master/', '/blob/master/');
}
}
return url ? decodeURIComponent(url) : null;
}

function addHomePage(scripts){
scripts.forEach(function(script){
if (!script.homepageURL) {
var url = script.downloadURL || script.updateURL;
script.homepageURL = getScriptHomeURL(url);
}
});
}

return {
addHomePage: addHomePage,
getHomePageURL: getScriptHomeURL
}
})();

if(window.GreaemonkeyEnhancer){
window.GreaemonkeyEnhancer.uninit();
delete window.GreaemonkeyEnhancer;
window.GreaemonkeyEnhancer.uninit();
delete window.GreaemonkeyEnhancer;
}

window.GreaemonkeyEnhancer = {
_id: "Scriptish-find-script",
init: function(){
var self = this;

var isCN = navigator.language.substr(0, 2) == "zh";
var isCN = navigator.language.substr(0, 2) == "zh";

var menuitem = document.createElement("menuitem");
menuitem.setAttribute("id", this._id);
menuitem.setAttribute("label", isCN ? "为本站搜索脚本(S)" : "find Script");
menuitem.setAttribute("accesskey", "s");
menuitem.setAttribute("accesskey", "s");
menuitem.setAttribute("oncommand", "GreaemonkeyEnhancer.findscripts()");

// Scriptish
var scriptishShow = document.getElementById("scriptish-tb-show-us");
if(scriptishShow){
scriptishShow.parentNode.insertBefore(menuitem, scriptishShow.nextSibling);
scriptishShow.parentNode.insertBefore(
document.createElement("menuseparator"),
scriptishShow.nextSibling
);
scriptishShow.parentNode.insertBefore(
document.createElement("menuseparator"),
scriptishShow.nextSibling
);
}

// Greasemonkey
// 把按钮移到最新版的 pentadactyl 附加组件栏后,一开始找不到 Greasemonkey 菜单
if ("@greasemonkey.mozdev.org/greasemonkey-service;1" in Cc) {

// 工具栏的菜单
var gm_popup = document.querySelector("#gm_general_menu > menupopup");
if (gm_popup) {
// 添加 tooltiptext
gm_popup.addEventListener('popupshowing', self.gmPopupShowing, true);
// 添加中键打开主页
gm_popup.addEventListener('click', self.gmPopupClicked, true);
}

// 图标右键菜单
// 把按钮移到最新版的 pentadactyl 附加组件栏后,一开始找不到 Greasemonkey 菜单
var addToGreasemonkey = function() {
var GM_popup = document.querySelector("#greasemonkey-tbb menupopup");
if (GM_popup) {
GM_popup.insertBefore(menuitem, GM_popup.children[3]);
gm_popup = document.querySelector("#greasemonkey-tbb > menupopup");
if (gm_popup) {
gm_popup.insertBefore(menuitem, gm_popup.children[3]);

// 添加 tooltiptext
GM_popup.addEventListener('popupshowing', self.gmPopupShowing, false);
gm_popup.addEventListener('popupshowing', self.gmPopupShowing, true);
// 添加中键打开主页
GM_popup.addEventListener('click', self.gmPopupClicked, false);
gm_popup.addEventListener('click', self.gmPopupClicked, true);
} else {
setTimeout(addToGreasemonkey, 500);
}
Expand All @@ -108,77 +123,79 @@
addToGreasemonkey();
}
},
uninit: function(){
var menuitem = document.getElementById(this._id);
if(menuitem){
menuitem.parentNode.removeChild(menuitem);
}

var GM_popup = document.querySelector("#greasemonkey-tbb menupopup");
if (GM_popup) {
GM_popup.removeEventListener('popupshowing', this.gmPopupShowing, false);
GM_popup.removeEventListener('click', this.gmPopupClicked, false);
}
},
gmPopupShowing: function(event) {
var popup = event.currentTarget;
var addHomeUrl = function(menuitem) {
var script = menuitem.script;
if (!script) return;
if (menuitem.getAttribute('homeURL')) return;

var downURL = script._updateURL || script._downloadURL;
var homeURL = ApplyPatchForScript.getHomePageURL(downURL) || downURL;
menuitem.setAttribute('homeURL', homeURL);
menuitem.setAttribute('tooltiptext', homeURL);
};

[].forEach.call(popup.children, addHomeUrl);
},
gmPopupClicked: function(event) {
if (event.button != 1) return;

var homeURL = event.target.getAttribute('homeURL');
if (homeURL && homeURL.startsWith('http')) {
openLinkIn(homeURL, 'tab', { inBackground: false });
event.stopPropagation();
event.preventDefault();
}
},
uninit: function(){
var menuitem = document.getElementById(this._id);
if(menuitem){
menuitem.parentNode.removeChild(menuitem);
}

var gm_popups = document.querySelectorAll("#gm_general_menu > menupopup, #greasemonkey-tbb > menupopup");
if (gm_popups) {
[].forEach.call(gm_popups, function(popup){
popup.removeEventListener('popupshowing', this.gmPopupShowing, true);
popup.removeEventListener('click', this.gmPopupClicked, true);
});
}
},
gmPopupShowing: function(event) {
var popup = event.currentTarget;
var addHomeUrl = function(menuitem) {
var script = menuitem.script;
if (!script) return;
if (menuitem.getAttribute('homeURL')) return;

var downURL = script._updateURL || script._downloadURL;
var homeURL = ApplyPatchForScript.getHomePageURL(downURL) || downURL;
menuitem.setAttribute('homeURL', homeURL);
menuitem.setAttribute('tooltiptext', homeURL);
};

[].forEach.call(popup.children, addHomeUrl);
},
gmPopupClicked: function(event) {
if (event.button != 1) return;

var homeURL = event.target.getAttribute('homeURL');
if (homeURL && homeURL.startsWith('http')) {
openLinkIn(homeURL, 'tab', { inBackground: prefs.inBackground });
event.stopPropagation();
event.preventDefault();
}
},
getFocusedWindow: function () {
var win = document.commandDispatcher.focusedWindow;
return (!win || win == window) ? content : win;
},
findscripts: function(){
var wins = this.getFocusedWindow();
var href = wins.location.href;
if(!href) return;
if(!href) return;
var p=0; //for number of "."
var f= new Array();
var q=2;
var t=1;
var a=0;
var y;
var o;
var m=4;
var stringa; //= new Array();
var re = /(?:[a-z0-9-]+\.)+[a-z]{2,4}/;
href=href.match(re); //extract the url part
href=href.toString();
//get the places and nunbers of the "."
for (var i=0;i<href.length;i++){
if (href[i]=="."){
var t=1;
var a=0;
var y;
var o;
var m=4;
var stringa; //= new Array();
var re = /(?:[a-z0-9-]+\.)+[a-z]{2,4}/;
href=href.match(re); //extract the url part
href=href.toString();
//get the places and nunbers of the "."
for (var i=0;i<href.length;i++){
if (href[i]=="."){
f[p]=i;
p++ ;
}
}
}
}
if (p==t){
stringa=href.substring(a,f[0]);
stringa=href.substring(a,f[0]);
}else if (p==q){
stringa=href.substring(++f[0],f[1]);
}
else {
stringa=href.substring(++f[0],f[2]);
stringa=href.substring(++f[0],f[2]);
}

openLinkIn("http://www.google.com/search?q=site:userscripts.org+inurl:scripts+inurl:show+"+ stringa,
Expand Down

0 comments on commit 648ae3b

Please sign in to comment.