Chrome plug-in development

Source: Internet
Author: User

I'm reading:

document.body.oncontextmenu=function(){return false;};document.body.ondragstart=function(){return false;};document.body.onselectstart=function(){return false;};document.body.onbeforecopy=function(){return false;};document.body.onselect=function(){document.selection.empty();};document.body.oncopy=function(){document.selection.empty();};

In fact, one of the ways for websites to prevent copying is similar. The same is true for articles in the 306doc Library:


Its key code is:

Document. body. oncopy = function () {var curusernamecookiescgcg = getcookie ("loginname"); If (curusernamecookiescgcg = "" | curusernamecookiescgcg = NULL) {var docarttitle = document. getelementbyid ("docarttitle"); var docencodetitle = ""; if (docarttitle = NULL) {docencodetitle = ""} else {docencodetitle = "& titleencode =" + docarttitle. value} $. ajax ({URL: "http://www.360doc.com/ajax/GetLog Inform20130912.ashx? Artid = "+ ArticleID +" & type = 2 & arttype = "+ curarttype + docencodetitle, cache: false, success: function (result) {$ ("# layerlogin" result .html (result); showbg ("dialog", "dialog_content", "1")}, error: onfailed }); return false} else {var selhtml = ""; var selection; If (window. getselection) {selection = Window. getselection (); If (selection! = NULL) {selhtml = selection. tostring ()} else if (document. Selection) {selection = Document. selection. createRange (); If (selection! = NULL) {selhtml = selection. text} If (selhtml. length & gt; 200) {document. getelementbyid ('fuzhitishidiv '). style. display = 'none'; If (getcookie ("360doc1 ")! = NULL & userid! = 0) {$. Ajax ({URL: "http://www.360doc.com/ajax/GetLoginForm20130912.ashx? Artid = "+ ArticleID +" & type = 5 & arttype = "+ curarttype +" ", cache: false, success: function (result) {If (result = "-1") {return true} else {If (document. getelementbyid ("fuzhitishidiv ")! = NULL) {document. getelementbyid ("fuzhitishidiv "). innerhtml = "<Div style = \" position: absolute; Z-index: 2001; \ ">  <Map Name = \" MAP2 \ "id = \" MAP2 \ "> <Area shape = \ "rect \" coords = \ "288,23, 307,42 \ "href = \" javascript: void (0); \ "onclick = \" document. getelementbyid ('fuzhitishidiv '). innerhtml = ''; document. getelementbyid ('fuzhitishidiv '). Style. display = 'none'; \ "/> </map> </div>"; window. scroll (0, 0); document. getelementbyid ("fuzhitishidiv "). style. display = ''} else {alert (" Tip: click "transfer to my library" under the title to save the article to your personal library, then you can copy the content of the article! ")} Return false }}, error: onfailed})} else {If (document. getelementbyid (" fuzhitishidiv ")! = NULL) {document. getelementbyid ("fuzhitishidiv "). innerhtml = "<Div style = \" position: absolute; Z-index: 2001; \ ">  <Map Name = \" MAP2 \ "id = \" MAP2 \ "> <Area shape = \ "rect \" coords = \ "288,23, 307,42 \ "href = \" javascript: void (0); \ "onclick = \" document. getelementbyid ('fuzhitishidiv '). innerhtml = ''; document. getelementbyid ('fuzhitishidiv '). Style. display = 'none'; \ "/> </map> </div>"; window. scroll (0, 0); document. getelementbyid ("fuzhitishidiv "). style. display = ''} else {alert (" Tip: click "transfer to my library" under the title to save the article to your personal library, then you can copy the content of the article! ")} Return false} else {return true }}}

To solve these problems, the first thought is:

I. Remove the code or external JS links, but it is unrealistic to do so:

  1. In the first case, blocking plug-ins such as scriptblock by using a browser script also fails;
  2. Blocking all JavaScript codes may make the webpage unable to be displayed normally.
Ii. Disable Javascript execution in the browser

Likewise, the above principle 2 cannot work;

3. Develop chrome plug-in + API hook implementation

This method seems to be the most effective;

Plugin creation process:

  1. In the chrome address bar, enter chrome: // extensions/to open the Extended Program window. Select the developer option;
  2. Select the directory of the previously created plug-in
  3. After the code debugging is complete, click the package extension in to package. Drag the generated. CRX file to the browser for installation;


Code:

Manifest. JSON

{"name": "UBC","manifest_version": 2,"version": "1.0.0","description": "UnBlock Copy","browser_action": {"default_icon": "icon.png","default_title": "UnBlock Copy",    "default_popup": "popup.html"},"content_scripts": [{           "matches": ["http://*/*","https://*/*"],            "js": ["js/hookapi.js"],            "run_at": "document_end",           "all_frames": true       }]} 

Note:

"manifest_version": 2,

This line is fixed. The new Chrome plug-in development specifications do not support this line in most online tutorials.

Popup.html

<div style="width:200px;"><center></center> <ol><li>Unblock Copy   Event</li><li>Unblock Paste  Event</li><li>Unblock Select Event</li></ol></div>


Hookapi. js

//UnHook Copy Related Eventsvar arr_hook_event = ["oncontextmenu","ondragstart","onselectstart","onselect","onbeforecopy","oncopy"];function Array_indexOf(arr,item){for(var i = 0;i<arr.length;i++){  if(arr[i] == item){     return i;  }  }   return -1;  }   function UnHookEvent(onevent) {var _event = onevent.substr(2);document.addEventListener(_event, function(e) {e.stopPropagation();}, true);}for (var k in document.body) {if (/^on/.test(k)) {var __index = Array_indexOf(arr_hook_event,k);if(__index!= -1){UnHookEvent(k);}}}

Test results: the two websites can select and copy the content:




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.