Created
January 19, 2016 20:27
-
-
Save oyiptong/380dc060c2d0f8d1ed5b to your computer and use it in GitHub Desktop.
newtaboverride 2.3 index.js using aboutNewTabService
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ONE_SECOND_IN_MILLISECONDS = 1000; | |
const URL_CHARS_LIMIT = 2000; | |
const { PrefsTarget } = require('sdk/preferences/event-target'); | |
const { setInterval, clearInterval } = require('sdk/timers'); | |
const {Cu} = require('chrome'); | |
const clipboard = require('sdk/clipboard'); | |
const preferencesService = require('sdk/preferences/service'); | |
const prefsTarget = PrefsTarget({ branchName: 'browser.startup.'}); | |
const simplePrefs = require('sdk/simple-prefs'); | |
Cu.import("resource://gre/modules/XPCOMUtils.jsm"); | |
XPCOMUtils.defineLazyServiceGetter(this, "aboutNewTabService", | |
"@mozilla.org/browser/aboutnewtab-service;1", | |
"nsIAboutNewTabService"); | |
const newtaboverride = { | |
lastClipboardUrl : false, | |
timer : false, | |
init : function () { | |
newtaboverride.onPrefChange(); | |
}, | |
onPrefChange : function () { | |
var type = simplePrefs.prefs['type']; | |
var newTabUrl; | |
switch (type) { | |
case 'about:blank': | |
case 'about:home': | |
case 'about:newtab': | |
case 'about:sync-tabs': | |
newTabUrl = type; | |
break; | |
case 'clipboard': | |
newTabUrl = 'about:blank'; | |
// unfortunately there is no "clipboard changed" event… | |
newtaboverride.timer = setInterval(newtaboverride.clipboardAction, ONE_SECOND_IN_MILLISECONDS / 2); | |
break; | |
case 'custom_url': | |
var url = simplePrefs.prefs['url']; | |
if (url === '') { | |
newTabUrl = 'about:blank'; | |
} else { | |
newTabUrl = url; | |
} | |
break; | |
case 'homepage': | |
newTabUrl = preferencesService.getLocalized('browser.startup.homepage', 'about:blank').split('|')[0]; | |
break; | |
default: | |
newTabUrl = 'about:newtab'; | |
} | |
if (type !== 'clipboard') { | |
clearInterval(newtaboverride.timer); | |
newtaboverride.lastClipboardUrl = false; | |
} | |
aboutNewTabService.newTabURL = newTabUrl; | |
}, | |
clipboardAction : function () { | |
var clipboardContent = clipboard.get(); | |
if (clipboard.currentFlavors.indexOf('text') === -1) { | |
return; | |
} | |
if (clipboardContent.length > URL_CHARS_LIMIT || !newtaboverride.isValidUri(clipboardContent)) { | |
return; | |
} | |
if (!newtaboverride.lastClipboardUrl || clipboardContent !== newtaboverride.lastClipboardUrl) { | |
newTabUrlJsm.override(clipboardContent); | |
newtaboverride.lastClipboardUrl = clipboardContent; | |
} | |
}, | |
/** | |
* @see http://stackoverflow.com/a/9284473 | |
*/ | |
isValidUri : function (string) { | |
var website = /^(?:(?:https?):\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,}))\.?)(?::\d{2,5})?(?:[/?#]\S*)?$/i; | |
var aboutpage = /^about:(about|accounts|addons|blank|buildconfig|cache|config|crashes|credits|customizing|debugging|downloads|healthreport|home|license|logo|memory|mozilla|networking|newtab|performance|plugins|preferences|privatebrowsing|profiles|remote-newtab|rights|robots|serviceworkers|support|sync-log|sync-tabs|telemetry|webrtc)?$/i; | |
return website.test(string) || aboutpage.test(string); | |
} | |
}; | |
const main = () => { | |
newtaboverride.init(); | |
simplePrefs.on('', newtaboverride.onPrefChange); | |
prefsTarget.on('homepage', newtaboverride.onPrefChange); | |
}; | |
exports.main = main; | |
exports.onUnload = function (reason) { | |
if (reason === 'uninstall' || reason === 'disable') { | |
clearInterval(newtaboverride.timer); | |
newtaboverride.lastClipboardUrl = false; | |
newTabUrlJsm.reset(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment