diff --git a/couchpotato/core/plugins/userscript/bookmark.js b/couchpotato/core/plugins/userscript/bookmark.js new file mode 100644 index 0000000..3e0e517 --- /dev/null +++ b/couchpotato/core/plugins/userscript/bookmark.js @@ -0,0 +1,43 @@ +var includes = {{includes|tojson}}; +var excludes = {{excludes|tojson}}; + +var specialChars = '\\{}+.():-|^$'; +var makeRegex = function(pattern) { + pattern = pattern.split(''); + var i, len = pattern.length; + for( i = 0; i < len; i++) { + var character = pattern[i]; + if(specialChars.indexOf(character) > -1) { + pattern[i] = '\\' + character; + } else if(character === '?') { + pattern[i] = '.'; + } else if(character === '*') { + pattern[i] = '.*'; + } + } + return new RegExp('^' + pattern.join('') + '$'); +}; + +var isCorrectUrl = function() { + for(i in includes) { + var reg = includes[i] + if (makeRegex(reg).test(document.location.href)) + return true; + } + return false; +} +var addUserscript = function() { + // Add window param + document.body.setAttribute('cp_auto_open', true) + + // Load userscript + var e = document.createElement('script'); + e.setAttribute('type', 'text/javascript'); + e.setAttribute('charset', 'UTF-8'); + e.setAttribute('src', '{{host}}/userscript.get/couchpotato.js?r=' + Math.random() * 99999999); + document.body.appendChild(e) +} +if(isCorrectUrl()) + addUserscript() +else + alert('Can\'t find a proper movie on this page..') diff --git a/couchpotato/core/plugins/userscript/main.py b/couchpotato/core/plugins/userscript/main.py index 98c959b..1f9c0eb 100644 --- a/couchpotato/core/plugins/userscript/main.py +++ b/couchpotato/core/plugins/userscript/main.py @@ -22,9 +22,20 @@ class Userscript(Plugin): addApiView('userscript.get/', self.getUserScript, static = True) addApiView('userscript', self.iFrame) addApiView('userscript.add_via_url', self.getViaUrl) + addApiView('userscript.bookmark', self.bookmark) addEvent('userscript.get_version', self.getVersion) + def bookmark(self): + + params = { + 'includes': fireEvent('userscript.get_includes', merge = True), + 'excludes': fireEvent('userscript.get_excludes', merge = True), + 'host': fireEvent('app.api_url', single = True) + } + + return self.renderTemplate(__file__, 'bookmark.js', **params) + def getUserScript(self, filename = ''): params = { diff --git a/couchpotato/core/plugins/userscript/static/userscript.js b/couchpotato/core/plugins/userscript/static/userscript.js index f167739..f47825e 100644 --- a/couchpotato/core/plugins/userscript/static/userscript.js +++ b/couchpotato/core/plugins/userscript/static/userscript.js @@ -63,15 +63,48 @@ var UserscriptSettingTab = new Class({ self.settings = App.getPage('Settings') self.settings.addEvent('create', function(){ + // See if userscript can be installed + var userscript = false; + try { + if(Components.interfaces.gmIGreasemonkeyService) + userscript = true + } + catch(e){ + userscript = Browser.chrome === true; + } + self.settings.createGroup({ - 'label': 'Install the Userscript' + 'name': 'userscript', + 'label': 'Install the bookmarklet' + (userscript ? ' or userscript' : ''), + 'description': 'Easily add movies via imdb.com, appletrailers and more' }).inject(self.settings.tabs.automation.content, 'top').adopt( - new Element('a', { + (userscript ? [new Element('a.userscript.button', { 'text': 'Install userscript', 'href': Api.createUrl('userscript.get')+'couchpotato.user.js', 'target': '_self' - }) - ); + }), new Element('span.or[text=or]')] : null), + new Element('span.bookmarklet').adopt( + new Element('a.button.green', { + 'text': '+CouchPotato', + 'href': "javascript:void((function(){var e=document.createElement('script');e.setAttribute('type','text/javascript');e.setAttribute('charset','UTF-8');e.setAttribute('src','" + + Api.getOption('host') + '/userscript.bookmark/' + + "?r='+Math.random()*99999999);document.body.appendChild(e)})());", + 'target': '', + 'events': { + 'click': function(e){ + (e).stop() + alert('Drag it to your bookmark ;)') + } + } + }), + new Element('span', { + 'text': '⇽ Drag this to your bookmarks' + }) + ) + ).setStyles({ + 'background-image': "url('"+Api.createUrl('static/userscript/userscript.png')+"')" + }); + }); } diff --git a/couchpotato/core/plugins/userscript/static/userscript.png b/couchpotato/core/plugins/userscript/static/userscript.png new file mode 100644 index 0000000..c8e7657 Binary files /dev/null and b/couchpotato/core/plugins/userscript/static/userscript.png differ diff --git a/couchpotato/core/plugins/userscript/template.js b/couchpotato/core/plugins/userscript/template.js index e2e9b85..158638e 100644 --- a/couchpotato/core/plugins/userscript/template.js +++ b/couchpotato/core/plugins/userscript/template.js @@ -85,35 +85,42 @@ var osd = function(){ var popup = create('div', { 'id': 'cp_popup' }); + + var onclick = function(){ + + // Try and get imdb url + try { + var regex = new RegExp(/tt(\d+)/); + var imdb_id = document.body.innerHTML.match(regex)[0]; + if (imdb_id) + iframe.setAttribute('src', createApiUrl('http://imdb.com/title/'+imdb_id+'/')) + } + catch(e){} + + popup.innerHTML = ''; + popup.appendChild(create('a', { + 'innerHTML': '', + 'id': 'close_button', + 'onclick': function(){ + popup.innerHTML = ''; + popup.appendChild(add_button); + } + })); + popup.appendChild(iframe) + } + var add_button = create('a', { 'innerHTML': '', 'id': 'add_to', - 'onclick': function(){ - - // Try and get imdb url - try { - var regex = new RegExp(/tt(\d+)/); - var imdb_id = document.body.innerHTML.match(regex)[0]; - if (imdb_id) - iframe.setAttribute('src', createApiUrl('http://imdb.com/title/'+imdb_id+'/')) - } - catch(e){} - - popup.innerHTML = ''; - popup.appendChild(create('a', { - 'innerHTML': '', - 'id': 'close_button', - 'onclick': function(){ - popup.innerHTML = ''; - popup.appendChild(add_button); - } - })); - popup.appendChild(iframe) - } + 'onclick': onclick }); popup.appendChild(add_button); document.body.parentNode.insertBefore(popup, document.body); + + // Auto fold open + if(document.body.getAttribute('cp_auto_open')) + onclick() }; var setVersion = function(){ diff --git a/couchpotato/static/style/page/settings.css b/couchpotato/static/style/page/settings.css index 98ac033..361a2e3 100644 --- a/couchpotato/static/style/page/settings.css +++ b/couchpotato/static/style/page/settings.css @@ -1,3 +1,6 @@ +/* @override + http://127.0.0.1:5000/_api_/static/style/page/settings.css */ + .page.settings:after { content: "."; display: block; @@ -550,4 +553,41 @@ .page .tab_about .group_actions a { margin: 0 10px; font-size: 20px; + } + +.group_userscript { + background: center bottom no-repeat; + min-height: 360px; + font-size: 20px; + font-weight: normal; +} + + .group_userscript h2 .hint { + display: block; + margin: 0 !important; + } + + .group_userscript .userscript { + float: left; + margin: 14px 0 0 25px; + height: 36px; + line-height: 25px; + } + + .group_userscript .or { + float: left; + margin: 20px 10px; + } + + .group_userscript .bookmarklet { + display: block; + display: block; + float: left; + padding: 20px 15px 0 0 ; + border-radius: 5px; + } + + .group_userscript .bookmarklet span { + margin-left: 10px; + display: inline-block; } \ No newline at end of file diff --git a/couchpotato/templates/_desktop.html b/couchpotato/templates/_desktop.html index 751850f..63aa4dd 100644 --- a/couchpotato/templates/_desktop.html +++ b/couchpotato/templates/_desktop.html @@ -51,6 +51,7 @@ new Uniform(); Api.setup({ + 'host': {{ fireEvent('app.api_url', single = True)|tojson|safe }}, 'url': {{ url_for('api.index')|tojson|safe }}, 'path_sep': {{ sep|tojson|safe }}, 'is_remote': false